我正在尝试解密一个文件,仅用于过程存储器。我不希望将实际文件发送为纯文本,因为它将存储敏感数据。我根本不希望将原始文本放在系统中。
我目前正在使用C:\中的eula文件进行测试,但是无论我使用哪个文件,都会出现相同的问题。
我正在将AES与盐混合使用。现在,解密文件确实可以正常工作,我将解密后的数据转储到文本文档中,但是当我尝试将decrpytedBytes编译为字符串时,它仅输出3个字符,这些字符在该文档内的任何位置均不存在。
https://i.imgur.com/WAQ2njB.png
使用System.Text.Encoding.UTF8.GetString(bytesDecrypted, 0, bytesDecrypted.Length)
将字节数组编译为字符串时,将显示这2个字符。
我只尝试了基本的.ToString()
,但是返回了System.Byte[]
,仅此而已
https://i.imgur.com/Gg5Et72.png
使用var str = System.Text.Encoding.Default.GetString(bytesDecrypted)
时仅输出outputs *
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;
}
如果您对我如何能够成功进行工作有任何想法/线索,我将不胜感激!
答案 0 :(得分:3)
您使用了不正确的编码来解码解密的字节数组。原始文本文件的编码很可能是Unicode / UTF-16。因此,使用Encoding.Unicode
编码将解密的字节数组解码回文本:
var str = System.Text.Encoding.Unicode.GetString(bytesDecrypted);
一些背景信息
那么,是什么让我认为原始文本文件的编码为UTF-16 / Unicode? 来自问题的信息提供了关键提示:
使用var str = System.Text.Encoding.Default.GetString(bytesDecrypted)时,仅输出ÿþ*
请注意ÿþ
。如果使用ISO / IEC 8859-1(或CP-1252)代码页(通常是许多代码中使用的默认代码页)对具有此BOM表的文本数据进行解码/显示,这就是UTF-16 LE BOM表(*)出现的方式(英语/非本地化)Windows安装。
(*)UTF-16 LE BOM(UTF-16 Little-Endian字节顺序标记)是两个字节0xFF,0xFE。要了解有关BOM表及其用途的更多信息,我建议这篇Wikipedia文章:https://en.wikipedia.org/wiki/Byte_order_mark
答案 1 :(得分:-1)
找到了我认为适用于您的问题的答案。特别注意“ encryptedData = output.ToArray();”
Reading from a cryptostream to the end of the stream
byte[] encryptedData;
rijCrypto.Padding = System.Security.Cryptography.PaddingMode.ISO10126;
rijCrypto.KeySize = 256;
using (var input = new MemoryStream(Encoding.Unicode.GetBytes(tempData)))
using (var output = new MemoryStream())
{
var encryptor = rijCrypto.CreateEncryptor();
using (var cryptStream = new CryptoStream(output, encryptor, CryptoStreamMode.Write))
{
var buffer = new byte[1024];
var read = input.Read(buffer, 0, buffer.Length);
while (read > 0)
{
cryptStream.Write(buffer, 0, read);
read = input.Read(buffer, 0, buffer.Length);
}
cryptStream.FlushFinalBlock();
encryptedData = output.ToArray();
}
}