可以加密文件内容,也可以对文件夹本身进行加密,本文对文件夹加密。
一、指定或生成一个密钥
1)指定的密钥
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 ///2 /// 密钥,这个密码可以随便指定3 /// 4 public static string sSecretKey = "?\a??64(?";
2) 也可以生成密钥
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 ///2 /// 生成一个64位的密钥 3 /// 4 ///string 5 public static string GenerateKey() 6 { 7 //创建对称算法的一个实例。自动生成的密钥和IV。 8 DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create(); 9 10 // 使用自动生成的密钥进行加密。11 return ASCIIEncoding.ASCII.GetString(desCrypto.Key);12 }
二、调用ZeroMemory 函数从内存中删除Key
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 ///2 /// 调用该函数从内存中删除的Key后使用3 /// 4 [DllImport("KERNEL32.DLL", EntryPoint = "RtlZeroMemory")]5 public static extern bool ZeroMemory(IntPtr Destination, int Length);
三、加密文件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 ///2 /// 加密文件 3 /// 4 /// 待加密的文件的完整路径 5 /// 加密后的文件的完整路径 6 public static void EncryptFile(string sInputFilename, string sOutputFilename) 7 { 8 FileStream fsInput = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read); 9 10 FileStream fsEncrypted = new FileStream(sOutputFilename, FileMode.Create, FileAccess.Write);11 DESCryptoServiceProvider DES = new DESCryptoServiceProvider();12 DES.Key = ASCIIEncoding.ASCII.GetBytes(sSecretKey);13 DES.IV = ASCIIEncoding.ASCII.GetBytes(sSecretKey);14 ICryptoTransform desencrypt = DES.CreateEncryptor();15 CryptoStream cryptostream = new CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write);16 17 byte[] bytearrayinput = new byte[fsInput.Length];18 fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);19 cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);20 21 cryptostream.Flush();22 fsInput.Flush();23 fsEncrypted.Flush();24 cryptostream.Close();25 fsInput.Close();26 fsEncrypted.Close();27 }
四、解密文件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 ///2 /// 解密文件 3 /// 4 /// 待解密的文件的完整路径 5 /// 解密后的文件的完整路径 6 public static void DecryptFile(string sInputFilename, string sOutputFilename) 7 { 8 DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); 9 DES.Key = ASCIIEncoding.ASCII.GetBytes(sSecretKey);10 DES.IV = ASCIIEncoding.ASCII.GetBytes(sSecretKey);11 12 FileStream fsread = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);13 ICryptoTransform desdecrypt = DES.CreateDecryptor();14 CryptoStream cryptostreamDecr = new CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read);15 StreamWriter fsDecrypted = new StreamWriter(sOutputFilename);16 fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());17 fsDecrypted.Flush();18 fsDecrypted.Close();19 }
五、完整代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 ///2 /// 文件加密 3 /// 4 public class FileSecretHelper 5 { 6 ///7 /// 密钥,这个密码可以随便指定 8 /// 9 public static string sSecretKey = "?\a??64(?";10 11 ///12 /// 调用该函数从内存中删除的Key后使用13 /// 14 [DllImport("KERNEL32.DLL", EntryPoint = "RtlZeroMemory")]15 public static extern bool ZeroMemory(IntPtr Destination, int Length);16 17 18 ///19 /// 生成一个64位的密钥20 /// 21 ///string 22 public static string GenerateKey()23 {24 //创建对称算法的一个实例。自动生成的密钥和IV。25 DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();26 27 // 使用自动生成的密钥进行加密。28 return ASCIIEncoding.ASCII.GetString(desCrypto.Key);29 }30 31 ///32 /// 加密文件33 /// 34 /// 待加密的文件的完整路径35 /// 加密后的文件的完整路径36 public static void EncryptFile(string sInputFilename, string sOutputFilename)37 {38 FileStream fsInput = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);39 40 FileStream fsEncrypted = new FileStream(sOutputFilename, FileMode.Create, FileAccess.Write);41 DESCryptoServiceProvider DES = new DESCryptoServiceProvider();42 DES.Key = ASCIIEncoding.ASCII.GetBytes(sSecretKey);43 DES.IV = ASCIIEncoding.ASCII.GetBytes(sSecretKey);44 ICryptoTransform desencrypt = DES.CreateEncryptor();45 CryptoStream cryptostream = new CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write);46 47 byte[] bytearrayinput = new byte[fsInput.Length];48 fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);49 cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);50 51 cryptostream.Flush();52 fsInput.Flush();53 fsEncrypted.Flush();54 cryptostream.Close();55 fsInput.Close();56 fsEncrypted.Close();57 }58 59 ///60 /// 解密文件61 /// 62 /// 待解密的文件的完整路径63 /// 解密后的文件的完整路径64 public static void DecryptFile(string sInputFilename, string sOutputFilename)65 {66 DESCryptoServiceProvider DES = new DESCryptoServiceProvider();67 DES.Key = ASCIIEncoding.ASCII.GetBytes(sSecretKey);68 DES.IV = ASCIIEncoding.ASCII.GetBytes(sSecretKey);69 70 FileStream fsread = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);71 ICryptoTransform desdecrypt = DES.CreateDecryptor();72 CryptoStream cryptostreamDecr = new CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read);73 StreamWriter fsDecrypted = new StreamWriter(sOutputFilename);74 fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());75 fsDecrypted.Flush();76 fsDecrypted.Close();77 }78 }