using System; using System.IO; using System.Text; using System.Security.Cryptography; namespace ConsoleApplication { public class EncryptKit { /// <summary> /// Call this function to remove the key from memory after use for security /// </summary> /// <param name="Destination"></param> /// <param name="Length"></param> /// <returns></returns> [System.Runtime.InteropServices.DllImport("KERNEL32.DLL", EntryPoint = "RtlZeroMemory")] public static extern bool ZeroMemory(IntPtr Destination, int Length); /// <summary> /// Function to Generate a 64 bits Key. /// </summary> /// <returns>返回生成的密钥</returns> public static string GenerateKey() { // Create an instance of Symetric Algorithm. Key and IV is generated automatically. DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create(); // Use the Automatically generated key for Encryption. return ASCIIEncoding.ASCII.GetString(desCrypto.Key); } /// <summary> /// 加密文件 /// </summary> /// <param name="sInputFilename">要加密的文件</param> /// <param name="sOutputFilename">加密后保存的文件</param> /// <param name="sKey">密钥</param> public static void EncryptFile(string sInputFilename, string sOutputFilename, string sKey) { using (FileStream fsInput = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read)) { byte[] bytearrayinput = new byte[fsInput.Length]; fsInput.Read(bytearrayinput, 0, bytearrayinput.Length); fsInput.Close(); FileStream fsEncrypted = new FileStream(sOutputFilename, FileMode.OpenOrCreate, FileAccess.Write); DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey); DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey); ICryptoTransform desencrypt = DES.CreateEncryptor(); CryptoStream cryptostream = new CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write); cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length); cryptostream.Close(); fsEncrypted.Close(); } } /// <summary> /// /// </summary> /// <param name="sInputFilename">要解密的文件</param> /// <param name="sOutputFilename">解决后保存的文件</param> /// <param name="sKey">密钥</param> public static void DecryptFile(string sInputFilename, string sOutputFilename, string sKey) { DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); //A 64 bit key and IV is required for this provider. //Set secret key For DES algorithm. DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey); //Set initialization vector. DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey); //Create a file stream to read the encrypted file back. using (FileStream fsread = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read)) { //Create a DES decryptor from the DES instance. ICryptoTransform desdecrypt = DES.CreateDecryptor(); //Create crypto stream set to read and do a //DES decryption transform on incoming bytes. CryptoStream cryptostreamDecr = new CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read); //Print the contents of the decrypted file. StreamWriter fsDecrypted = new StreamWriter(sOutputFilename); fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd()); fsDecrypted.Flush(); fsDecrypted.Close(); } } /// <summary> /// /// </summary> /// <param name="sInputFilename">要解密的文件路径</param> /// <param name="sKey">密钥</param> /// <returns>返回内容</returns> public static string DecryptFile(string sInputFilename, string sKey) { DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); //A 64 bit key and IV is required for this provider. //Set secret key For DES algorithm. DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey); //Set initialization vector. DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey); //Create a file stream to read the encrypted file back. using (FileStream fsread = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read)) { byte[] byt = new byte[fsread.Length]; fsread.Read(byt, 0, byt.Length); fsread.Flush(); fsread.Close(); //Create a DES decryptor from the DES instance. ICryptoTransform desdecrypt = DES.CreateDecryptor(); MemoryStream ms = new MemoryStream(); CryptoStream cryptostreamDecr = new CryptoStream(ms, desdecrypt, CryptoStreamMode.Write); cryptostreamDecr.Write(byt, 0, byt.Length); cryptostreamDecr.FlushFinalBlock(); cryptostreamDecr.Close(); return Encoding.UTF8.GetString(ms.ToArray()).Trim(); } } } }
调用如下
static void Main(string[] args) { sSecretKey = EncryptKit.GenerateKey(); GCHandle gch = GCHandle.Alloc(sSecretKey, GCHandleType.Pinned); EncryptKit.EncryptFile(@"config.dat", @"config.dat", sSecretKey); EncryptKit.DecryptFile(@"config.dat", @"Decrypted.dat", sSecretKey); string str = EncryptKit.DecryptFile("config.dat", sSecretKey); Console.WriteLine(sSecretKey); EncryptKit.ZeroMemory(gch.AddrOfPinnedObject(), sSecretKey.Length * 2); gch.Free(); Console.ReadKey(); }