[C#] C#对文件进行加密解密代码 →→→→→进入此内容的聊天室

来自 , 2019-03-19, 写在 C#, 查看 129 次.
URL http://www.code666.cn/view/0bfce127
  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4.  
  5. public class Example19_9
  6. {
  7.     public static void Main()
  8.     {
  9.  
  10.         // Create a new file to work with
  11.         FileStream fsOut = File.Create(@"c:\temp\encrypted.txt");
  12.  
  13.         // Create a new crypto provider
  14.         TripleDESCryptoServiceProvider tdes =
  15.             new TripleDESCryptoServiceProvider();
  16.  
  17.         // Create a cryptostream to encrypt to the filestream
  18.         CryptoStream cs = new CryptoStream(fsOut, tdes.CreateEncryptor(),
  19.             CryptoStreamMode.Write);
  20.  
  21.         // Create a StreamWriter to format the output
  22.         StreamWriter sw = new StreamWriter(cs);
  23.  
  24.         // And write some data
  25.         sw.WriteLine("'Twas brillig, and the slithy toves");
  26.         sw.WriteLine("Did gyre and gimble in the wabe.");
  27.         sw.Flush();
  28.         sw.Close();
  29.  
  30.         // save the key and IV for future use
  31.         FileStream fsKeyOut = File.Create(@"c:\\temp\encrypted.key");
  32.  
  33.         // use a BinaryWriter to write formatted data to the file
  34.         BinaryWriter bw = new BinaryWriter(fsKeyOut);
  35.  
  36.         // write data to the file
  37.         bw.Write( tdes.Key );
  38.         bw.Write( tdes.IV );
  39.  
  40.         // flush and close
  41.         bw.Flush();
  42.         bw.Close();
  43.  
  44.     }
  45.  
  46. }
  47.  
  48.  
  49. //csharp/7644

回复 "C#对文件进行加密解密代码"

这儿你可以回复上面这条便签

captcha