欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實現(xiàn)對文件進行加密解密的方法

 更新時間:2015年04月08日 12:25:14   作者:heishui  
這篇文章主要介紹了C#實現(xiàn)對文件進行加密解密的方法,涉及C#加密與解密的技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了C#實現(xiàn)對文件進行加密解密的方法。分享給大家供大家參考。具體如下:

using System;
using System.IO;
using System.Security.Cryptography;
public class Example19_9
{
 public static void Main()
 {
  // Create a new file to work with
  FileStream fsOut = File.Create(@"c:\temp\encrypted.txt");
  // Create a new crypto provider
  TripleDESCryptoServiceProvider tdes =
   new TripleDESCryptoServiceProvider();
  // Create a cryptostream to encrypt to the filestream
  CryptoStream cs = new CryptoStream(fsOut, tdes.CreateEncryptor(),
   CryptoStreamMode.Write);
  // Create a StreamWriter to format the output
  StreamWriter sw = new StreamWriter(cs);
  // And write some data
  sw.WriteLine("'Twas brillig, and the slithy toves");
  sw.WriteLine("Did gyre and gimble in the wabe.");
  sw.Flush();
  sw.Close();
  // save the key and IV for future use
  FileStream fsKeyOut = File.Create(@"c:\\temp\encrypted.key");
  // use a BinaryWriter to write formatted data to the file
  BinaryWriter bw = new BinaryWriter(fsKeyOut);
  // write data to the file
  bw.Write( tdes.Key );
  bw.Write( tdes.IV );
  // flush and close
  bw.Flush();
  bw.Close();
 }
}

解密代碼如下:

using System;
using System.IO;
using System.Security.Cryptography;
public class Example19_10
{
 public static void Main()
 {
  // Create a new crypto provider
  TripleDESCryptoServiceProvider tdes =
   new TripleDESCryptoServiceProvider();
  // open the file containing the key and IV
  FileStream fsKeyIn = File.OpenRead(@"c:\temp\encrypted.key");
  // use a BinaryReader to read formatted data from the file
  BinaryReader br = new BinaryReader(fsKeyIn);
  // read data from the file and close it
  tdes.Key = br.ReadBytes(24);
  tdes.IV = br.ReadBytes(8);
  // Open the encrypted file
  FileStream fsIn = File.OpenRead(@"c:\\temp\\encrypted.txt");
  // Create a cryptostream to decrypt from the filestream
  CryptoStream cs = new CryptoStream(fsIn, tdes.CreateDecryptor(),
   CryptoStreamMode.Read);
  // Create a StreamReader to format the input
  StreamReader sr = new StreamReader(cs);
  // And decrypt the data
  Console.WriteLine(sr.ReadToEnd());
  sr.Close();
 }
}

希望本文所述對大家的C#程序設計有所幫助。

相關文章

  • c#唯一值渲染實例代碼

    c#唯一值渲染實例代碼

    這篇文章主要介紹了c#唯一值渲染實例代碼,有需要的朋友可以參考一下
    2013-12-12
  • C#實現(xiàn)簡單的天氣預報示例代碼

    C#實現(xiàn)簡單的天氣預報示例代碼

    這篇文章主要介紹了C#實現(xiàn)簡單的天氣預報示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-06-06
  • C#控制臺進行文件讀寫的方法

    C#控制臺進行文件讀寫的方法

    這篇文章主要介紹了C#控制臺進行文件讀寫的方法,涉及C#操作文件讀寫的相關技巧,非常具有實用價值,需要的朋友可以參考下
    2015-04-04
  • 提取HTML代碼中文字的C#函數

    提取HTML代碼中文字的C#函數

    提取HTML代碼中文字的C#函數...
    2007-03-03
  • C#中抽象方法與虛擬方法的區(qū)別

    C#中抽象方法與虛擬方法的區(qū)別

    這篇文章主要介紹了C#中抽象方法與虛擬方法的區(qū)別,對于C#初學者來說可以深入理解抽象方法與虛擬方法,需要的朋友可以參考下
    2014-08-08
  • C#中的矩形數組(多維數組)和鋸齒數組的實現(xiàn)

    C#中的矩形數組(多維數組)和鋸齒數組的實現(xiàn)

    本文主要介紹了C#中的矩形數組(多維數組)和鋸齒數組的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-04-04
  • C#實現(xiàn)記事本查找與替換功能

    C#實現(xiàn)記事本查找與替換功能

    這篇文章主要為大家詳細介紹了C#實現(xiàn)記事本查找與替換功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-03-03
  • WPF基礎教程之形狀畫刷與變換詳解

    WPF基礎教程之形狀畫刷與變換詳解

    這篇文章主要給大家介紹了關于WPF基礎教程之形狀畫刷與變換的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-01-01
  • C#獲取指定目錄最后寫入時間的方法

    C#獲取指定目錄最后寫入時間的方法

    這篇文章主要介紹了C#獲取指定目錄最后寫入時間的方法,涉及C#中LastWriteTime屬性的使用技巧,非常具有實用價值,需要的朋友可以參考下
    2015-04-04
  • 關于C#中yield?return用法的思考

    關于C#中yield?return用法的思考

    在這篇文章中,我們將深入討論?C#?中yield?return的機制和用法,幫助您更好地理解這個強大的功能,并在實際開發(fā)中靈活使用它,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-05-05

最新評論