相关示例代码如下:
1 using System; 2 using System.IO; 3 using System.Security; 4 using System.Security.Cryptography; 5 using System.Runtime.InteropServices; 6 using System.Text; 7 8 namespace CSEncryptDecrypt 9 { 10 class Class1 11 { 12 // Call this function to remove the key from memory after use for security 13 [System.Runtime.InteropServices.DllImport("KERNEL32.DLL", EntryPoint="RtlZeroMemory")] 14 public static extern bool ZeroMemory(IntPtr Destination, int Length); 15 16 // Function to Generate a 64 bits Key. 17 static string GenerateKey() 18 { 19 // Create an instance of Symetric Algorithm. Key and IV is generated automatically. 20 DESCryptoServiceProvider desCrypto =(DESCryptoServiceProvider)DESCryptoServiceProvider.Create(); 21 22 // Use the Automatically generated key for Encryption. 23 return ASCIIEncoding.ASCII.GetString(desCrypto.Key); 24 } 25 26 static void EncryptFile(string sInputFilename, 27 string sOutputFilename, 28 string sKey) 29 { 30 FileStream fsInput = new FileStream(sInputFilename, 31 FileMode.Open, 32 FileAccess.Read); 33 34 FileStream fsEncrypted = new FileStream(sOutputFilename, 35 FileMode.Create, 36 FileAccess.Write); 37 DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); 38 DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey); 39 DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey); 40 ICryptoTransform desencrypt = DES.CreateEncryptor(); 41 CryptoStream cryptostream = new CryptoStream(fsEncrypted, 42 desencrypt, 43 CryptoStreamMode.Write); 44 45 byte[] bytearrayinput = new byte[fsInput.Length]; 46 fsInput.Read(bytearrayinput, 0, bytearrayinput.Length); 47 cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length); 48 cryptostream.Close(); 49 fsInput.Close(); 50 fsEncrypted.Close(); 51 } 52 53 static void DecryptFile(string sInputFilename, 54 string sOutputFilename, 55 string sKey) 56 { 57 DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); 58 //A 64 bit key and IV is required for this provider. 59 //Set secret key For DES algorithm. 60 DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey); 61 //Set initialization vector. 62 DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey); 63 64 //Create a file stream to read the encrypted file back. 65 FileStream fsread = new FileStream(sInputFilename, 66 FileMode.Open, 67 FileAccess.Read); 68 //Create a DES decryptor from the DES instance. 69 ICryptoTransform desdecrypt = DES.CreateDecryptor(); 70 //Create crypto stream set to read and do a 71 //DES decryption transform on incoming bytes. 72 CryptoStream cryptostreamDecr = new CryptoStream(fsread, 73 desdecrypt, 74 CryptoStreamMode.Read); 75 //Print the contents of the decrypted file. 76 StreamWriter fsDecrypted = new StreamWriter(sOutputFilename); 77 fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd()); 78 fsDecrypted.Flush(); 79 fsDecrypted.Close(); 80 } 81 82 static void Main() 83 { 84 // Must be 64 bits, 8 bytes. 85 // Distribute this key to the user who will decrypt this file. 86 string sSecretKey; 87 88 // Get the Key for the file to Encrypt. 89 sSecretKey = GenerateKey(); 90 91 // For additional security Pin the key. 92 GCHandle gch = GCHandle.Alloc( sSecretKey,GCHandleType.Pinned ); 93 94 // Encrypt the file. 95 EncryptFile(@"C:MyData.txt", 96 @"C:Encrypted.txt", 97 sSecretKey); 98 99 // Decrypt the file. 100 DecryptFile(@"C:Encrypted.txt", 101 @"C:Decrypted.txt", 102 sSecretKey); 103 104 // Remove the Key from memory. 105 ZeroMemory(gch.AddrOfPinnedObject(), sSecretKey.Length * 2); 106 gch.Free(); 107 } 108 } 109 }