可以加密文件内容,也可以对文件夹本身进行加密,本文对文件夹加密。
一、指定或生成一个密钥
1)指定的密钥
1 /// <summary> 2 /// 密钥,这个密码可以随便指定 3 /// </summary> 4 public static string sSecretKey = "?\a??64(?";
2) 也可以生成密钥
1 /// <summary> 2 /// 生成一个64位的密钥 3 /// </summary> 4 /// <returns>string</returns> 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
1 /// <summary> 2 /// 调用该函数从内存中删除的Key后使用 3 /// </summary> 4 [DllImport("KERNEL32.DLL", EntryPoint = "RtlZeroMemory")] 5 public static extern bool ZeroMemory(IntPtr Destination, int Length);
三、加密文件
1 /// <summary> 2 /// 加密文件 3 /// </summary> 4 /// <param name="sInputFilename">待加密的文件的完整路径</param> 5 /// <param name="sOutputFilename">加密后的文件的完整路径</param> 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 }
四、解密文件
1 /// <summary> 2 /// 解密文件 3 /// </summary> 4 /// <param name="sInputFilename">待解密的文件的完整路径</param> 5 /// <param name="sOutputFilename">解密后的文件的完整路径</param> 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 }
五、完整代码
1 /// <summary> 2 /// 文件加密 3 /// </summary> 4 public class FileSecretHelper 5 { 6 /// <summary> 7 /// 密钥,这个密码可以随便指定 8 /// </summary> 9 public static string sSecretKey = "?\a??64(?"; 10 11 /// <summary> 12 /// 调用该函数从内存中删除的Key后使用 13 /// </summary> 14 [DllImport("KERNEL32.DLL", EntryPoint = "RtlZeroMemory")] 15 public static extern bool ZeroMemory(IntPtr Destination, int Length); 16 17 18 /// <summary> 19 /// 生成一个64位的密钥 20 /// </summary> 21 /// <returns>string</returns> 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 /// <summary> 32 /// 加密文件 33 /// </summary> 34 /// <param name="sInputFilename">待加密的文件的完整路径</param> 35 /// <param name="sOutputFilename">加密后的文件的完整路径</param> 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 /// <summary> 60 /// 解密文件 61 /// </summary> 62 /// <param name="sInputFilename">待解密的文件的完整路径</param> 63 /// <param name="sOutputFilename">解密后的文件的完整路径</param> 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 }
时间: 2024-10-05 08:19:59