C# 试图解密文件以仅处理内存

标签 c# arrays encryption memory

我正在尝试将文件解密为仅进程内存。我不希望将实际文件发送为纯文本,因为它将存储敏感数据。我根本不希望系统中存在原始文本。

我目前正在使用 C:\中的 eula 文件进行测试,但无论我使用什么文件,都会遇到同样的问题。

我正在使用加盐的 AES。解密文件确实有效,因为我现在正在将解密的数据转储到文本文档中,但是当我试图将 decrpytedBytes 编译成一个字符串时,它只输出 3 个字符,这些字符在文档内的任何地方都不存在。

https://i.imgur.com/WAQ2njB.png

这 2 个字符在使用 System.Text.Encoding.UTF8.GetString(bytesDecrypted, 0, bytesDecrypted.Length) 将字节数组编译为字符串时出现。

我只尝试了一个基本的 .ToString() 但它返回了 System.Byte[] 仅此而已

https://i.imgur.com/Gg5Et72.png

在使用 var str = System.Text.Encoding.Default.GetString(bytesDecrypted) 时,它只输出 ÿþ*

https://i.imgur.com/94hMuB3.png

这是我用来加密和解密的代码

 public static byte[] AES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes)
 {
     byte[] encryptedBytes = null;
     byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };

     using (MemoryStream ms = new MemoryStream())
     {
          using (RijndaelManaged AES = new RijndaelManaged())
          {
              AES.KeySize = 256;
              AES.BlockSize = 128;

              var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
              AES.Key = key.GetBytes(AES.KeySize / 8);
              AES.IV = key.GetBytes(AES.BlockSize / 8);
              AES.Mode = CipherMode.CBC;

              using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
              {
                   cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
                   cs.Close();
              }

              encryptedBytes = ms.ToArray();
          }
     }

     return encryptedBytes;
 }

 public static byte[] AES_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes)
 {
     byte[] decryptedBytes = null;
     byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };

     using (MemoryStream ms = new MemoryStream())
     {
          using (RijndaelManaged AES = new RijndaelManaged())
          {
              AES.KeySize = 256;
              AES.BlockSize = 128;

              var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
              AES.Key = key.GetBytes(AES.KeySize / 8);
              AES.IV = key.GetBytes(AES.BlockSize / 8);
              AES.Mode = CipherMode.CBC;

              using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
              {
                   cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
                   cs.Close();
              }

              decryptedBytes = ms.ToArray();
          }
     }

     return decryptedBytes;
 }

 public void EncryptFile(string file, string fileEncrypted, string password)
 {
     byte[] bytesToBeEncrypted = File.ReadAllBytes(file);
     byte[] passwordBytes = Encoding.UTF8.GetBytes(password);

     passwordBytes = SHA256.Create().ComputeHash(passwordBytes);

     byte[] bytesEncrypted = AES_Encrypt(bytesToBeEncrypted, passwordBytes);

     File.WriteAllBytes(fileEncrypted, bytesEncrypted);
     listBox1.Items.Add("Enrypted the file");
 }

 public void DecryptFile(string fileEncrypted, string file, string password)
 {
     byte[] bytesToBeDecrypted = File.ReadAllBytes(fileEncrypted);
     byte[] passwordBytes = Encoding.UTF8.GetBytes(password);

     passwordBytes = SHA256.Create().ComputeHash(passwordBytes);

     byte[] bytesDecrypted = AES_Decrypt(bytesToBeDecrypted, passwordBytes);

     listBox1.Items.Add("Attempting Decryption");
     File.WriteAllBytes(file, bytesDecrypted);

     var str = System.Text.Encoding.Default.GetString(bytesDecrypted);

     richTextBox1.Text = str;
 }

如果您对我如何设法使这项工作有任何想法/线索,我将不胜感激!

最佳答案

您使用不正确的编码来解码解密的字节数组。原始文本文件的编码很可能是 Unicode/UTF-16。因此,使用Encoding.Unicode编码将解密后的字节数组解码回文本:

var str = System.Text.Encoding.Unicode.GetString(bytesDecrypted);



一些背景信息

那么,是什么让我认为原始文本文件的编码是UTF-16/Unicode? 这个问题的信息给出了一个重要的提示:

While using var str = System.Text.Encoding.Default.GetString(bytesDecrypted) it only outputs ÿþ*

注意 ÿþ。如果使用 ISO/IEC 8859-1(或 CP-1252)代码页解码/显示具有此 BOM 的文本数据,这就是 UTF-16 LE BOM (*) 的显示方式,这通常是许多应用程序中使用的默认代码页(英语/非本地化)Windows 安装。

(*) UTF-16 LE BOM(UTF-16 Little-Endian Byte Order Mark)是两个字节 0xFF,0xFE。要了解更多关于 BOM 是什么及其用途的信息,我建议阅读这篇维基百科文章:https://en.wikipedia.org/wiki/Byte_order_mark

关于C# 试图解密文件以仅处理内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53125662/

相关文章:

c# - 如何从 getter 或 setter 调用异步方法?

javascript - 如何通过在 Javascript 中使用两个普通数组来创建具有相同键的关联数组

python-3.x - 如何从文件中解码 utf-8

python - sha1 在 go 中与在 python 和 openssl 中不同

c# - 日期 SOAP Web 服务

c# - 在队列中添加数据时通知线程

c# - 控制台应用程序中的全局热键

c - 从函数内编辑 C 数组

php - 在 PHP 中合并两个数组

c# - 何时使用非对称 key 与数字证书