【发布时间】:2021-10-10 22:39:47
【问题描述】:
我编写了一个程序,它遍历一个目录,找到具有特定扩展名的文件,然后对其进行加密。虽然它没有加密实际文件,但它正在加密文件目录。 它是一个控制台应用程序,显示加密消息和解密消息。 这是代码的核心部分
static void Main(string[] args)
{
string root = @"C:\Users\Owner\Documents";
var files = from file in Directory.EnumerateFiles(root) select file;
foreach (var file in files)
{
if (file.EndsWith(".txt"))
{
try
{
// Create Aes that generates a new key and initialization vector (IV).
// Same key must be used in encryption and decryption
using (AesManaged aes = new AesManaged())
{
// Encrypt string
byte[] encrypted = Encrypt(file, aes.Key, aes.IV);
// Print encrypted string
Console.WriteLine($"Encrypted data: {System.Text.Encoding.UTF8.GetString(encrypted)}");
// Decrypt the bytes to a string.
string decrypted = Decrypt(encrypted, aes.Key, aes.IV);
// Print decrypted string. It should be same as raw data
Console.WriteLine($"Decrypted data: {decrypted}");
}
}
catch (Exception exp)
{
Console.WriteLine(exp.Message);
}
Console.ReadKey();
}
输出:
加密数据:?U↔C?♣#Y?f"?8?a?*????♦????&?,¶?
解密数据:C:\Users\Owner\Documents\script.txt
我进入文件,里面没有加密
【问题讨论】:
-
您没有在任何地方写入加密数据。
-
@mjwills:他当然是。到控制台:-)
标签: c# encryption aes filestream