可以加密文件内容,也可以对文件夹本身进行加密,本文对文件夹加密。
一、指定或生成一个密钥
1)指定的密钥
/// <summary> /// 密钥,这个密码可以随便指定 /// </summary> public static string sSecretKey = "?\a??64(?";
2) 也可以生成密钥
/// <summary> /// 生成一个64位的密钥 /// </summary> /// <returns>string</returns> public static string GenerateKey() { //创建对称算法的一个实例。自动生成的密钥和IV。 DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create(); // 使用自动生成的密钥进行加密。 return ASCIIEncoding.ASCII.GetString(desCrypto.Key); }
二、调用ZeroMemory 函数从内存中删除Key
///<summary> /// 调用该函数从内存中删除的Key后使用 /// </summary> [DllImport("KERNEL32.DLL", EntryPoint = "RtlZeroMemory")] public static extern bool ZeroMemory(IntPtr Destination, int Length);
三、加密文件
/// <summary> /// 加密文件 /// </summary> /// <param name="sInputFilename">待加密的文件的完整路径</param> /// <param name="sOutputFilename">加密后的文件的完整路径</param> public static void EncryptFile(string sInputFilename, string sOutputFilename) { FileStream fsInput = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read); FileStream fsEncrypted = new FileStream(sOutputFilename, FileMode.Create, FileAccess.Write); DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); DES.Key = ASCIIEncoding.ASCII.GetBytes(sSecretKey); DES.IV = ASCIIEncoding.ASCII.GetBytes(sSecretKey); ICryptoTransform desencrypt = DES.CreateEncryptor(); CryptoStream cryptostream = new CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write); byte[] bytearrayinput = new byte[fsInput.Length]; fsInput.Read(bytearrayinput, 0, bytearrayinput.Length); cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length); cryptostream.Flush(); fsInput.Flush(); fsEncrypted.Flush(); cryptostream.Close(); fsInput.Close(); fsEncrypted.Close(); }
四、解密文件
/// <summary> /// 解密文件 /// </summary> /// <param name="sInputFilename">待解密的文件的完整路径</param> /// <param name="sOutputFilename">解密后的文件的完整路径</param> public static void DecryptFile(string sInputFilename, string sOutputFilename) { DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); DES.Key = ASCIIEncoding.ASCII.GetBytes(sSecretKey); DES.IV = ASCIIEncoding.ASCII.GetBytes(sSecretKey); FileStream fsread = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read); ICryptoTransform desdecrypt = DES.CreateDecryptor(); CryptoStream cryptostreamDecr = new CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read); StreamWriter fsDecrypted = new StreamWriter(sOutputFilename); fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd()); fsDecrypted.Flush(); fsDecrypted.Close(); }
五、完整代码
/// <summary> /// 文件加密 /// </summary> public class FileSecretHelper { /// <summary> /// 密钥,这个密码可以随便指定 /// </summary> public static string sSecretKey = "?\a??64(?"; /// <summary> /// 调用该函数从内存中删除的Key后使用 /// </summary> [DllImport("KERNEL32.DLL", EntryPoint = "RtlZeroMemory")] public static extern bool ZeroMemory(IntPtr Destination, int Length); /// <summary> /// 生成一个64位的密钥 /// </summary> /// <returns>string</returns> public static string GenerateKey() { //创建对称算法的一个实例。自动生成的密钥和IV。 DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create(); // 使用自动生成的密钥进行加密。 return ASCIIEncoding.ASCII.GetString(desCrypto.Key); } /// <summary> /// 加密文件 /// </summary> /// <param name="sInputFilename">待加密的文件的完整路径</param> /// <param name="sOutputFilename">加密后的文件的完整路径</param> public static void EncryptFile(string sInputFilename, string sOutputFilename) { FileStream fsInput = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read); FileStream fsEncrypted = new FileStream(sOutputFilename, FileMode.Create, FileAccess.Write); DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); DES.Key = ASCIIEncoding.ASCII.GetBytes(sSecretKey); DES.IV = ASCIIEncoding.ASCII.GetBytes(sSecretKey); ICryptoTransform desencrypt = DES.CreateEncryptor(); CryptoStream cryptostream = new CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write); byte[] bytearrayinput = new byte[fsInput.Length]; fsInput.Read(bytearrayinput, 0, bytearrayinput.Length); cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length); cryptostream.Flush(); fsInput.Flush(); fsEncrypted.Flush(); cryptostream.Close(); fsInput.Close(); fsEncrypted.Close(); } /// <summary> /// 解密文件 /// </summary> /// <param name="sInputFilename">待解密的文件的完整路径</param> /// <param name="sOutputFilename">解密后的文件的完整路径</param> public static void DecryptFile(string sInputFilename, string sOutputFilename) { DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); DES.Key = ASCIIEncoding.ASCII.GetBytes(sSecretKey); DES.IV = ASCIIEncoding.ASCII.GetBytes(sSecretKey); FileStream fsread = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read); ICryptoTransform desdecrypt = DES.CreateDecryptor(); CryptoStream cryptostreamDecr = new CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read); StreamWriter fsDecrypted = new StreamWriter(sOutputFilename); fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd()); fsDecrypted.Flush(); fsDecrypted.Close(); } }