C# 文件夹加密

可以加密文件内容,也可以对文件夹本身进行加密,本文对文件夹加密。

一、指定或生成一个密钥

  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     }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想在C#加密和隐藏文件夹,可以使用以下步骤: 1. 选择要加密和隐藏的文件夹。 2. 使用C#中的AES加密算法文件夹进行加密。 3. 将加密后的文件夹移动到一个隐藏文件夹中。 4. 使用C#中的文件系统对象隐藏该文件夹,使其在操作系统中不可见。 以下是一些示例代码,可以帮助你实现这些步骤: 使用AES加密算法加密文件夹: ``` string password = "myPassword"; string folderPath = @"C:\MyFolder"; // Create a new AES object Aes aes = Aes.Create(); // Generate a random initialization vector byte[] iv = aes.IV; // Create a new encryption key using the password and IV Rfc2898DeriveBytes keyDerivation = new Rfc2898DeriveBytes(password, iv); byte[] key = keyDerivation.GetBytes(32); // Create a new encryption stream using (FileStream inputFileStream = new FileStream(folderPath, FileMode.Open)) using (FileStream outputFileStream = new FileStream(folderPath + ".encrypted", FileMode.Create)) using (ICryptoTransform encryptor = aes.CreateEncryptor(key, iv)) using (CryptoStream cryptoStream = new CryptoStream(outputFileStream, encryptor, CryptoStreamMode.Write)) { // Encrypt the input file stream and write it to the output file stream inputFileStream.CopyTo(cryptoStream); } // Delete the original folder Directory.Delete(folderPath, true); ``` 将加密后的文件夹移动到一个隐藏文件夹中: ``` string hiddenFolderPath = @"C:\MyHiddenFolder"; // Create the hidden folder if it doesn't exist if (!Directory.Exists(hiddenFolderPath)) { Directory.CreateDirectory(hiddenFolderPath); } // Move the encrypted folder to the hidden folder string encryptedFolderPath = folderPath + ".encrypted"; File.Move(encryptedFolderPath, hiddenFolderPath + "\\" + Path.GetFileName(encryptedFolderPath)); ``` 使用文件系统对象隐藏该文件夹: ``` string folderName = Path.GetFileName(folderPath); string hiddenFolderPath = @"C:\MyHiddenFolder"; // Hide the folder by setting its attributes to hidden File.SetAttributes(hiddenFolderPath + "\\" + folderName, FileAttributes.Hidden); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值