C# 文件夹加密
可以加密文件內(nèi)容,也可以對文件夾本身進(jìn)行加密,本文對文件夾加密。
一、指定或生成一個密鑰
? 1)指定的密鑰
/// <summary>/// 密鑰,這個密碼可以隨便指定/// </summary>public static string sSecretKey = "?\a??64(?";?2) 也可以生成密鑰
/// <summary> /// 生成一個64位的密鑰 /// </summary> /// <returns>string</returns> public static string GenerateKey() {//創(chuàng)建對稱算法的一個實例。自動生成的密鑰和IV。DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();// 使用自動生成的密鑰進(jìn)行加密。return ASCIIEncoding.ASCII.GetString(desCrypto.Key);}二、調(diào)用ZeroMemory 函數(shù)從內(nèi)存中刪除Key
///<summary> /// 調(diào)用該函數(shù)從內(nèi)存中刪除的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>/// 調(diào)用該函數(shù)從內(nèi)存中刪除的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(){//創(chuàng)建對稱算法的一個實例。自動生成的密鑰和IV。DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();// 使用自動生成的密鑰進(jìn)行加密。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();}}?
轉(zhuǎn)載于:https://www.cnblogs.com/bohetang/p/4025836.html
總結(jié)
- 上一篇: 链表相交以及找循环链表入口
- 下一篇: ACM如何产生各种随机数