加密例程不工作,无法弄清楚,其中有细微的错误
Encryption Routine Not working, Can't figure out, subtle bug in it
全部,
请看下面的代码。加密、解密代码无法正常工作。
当我传入字符串“Hello world”时,我返回“Hello wo”。我总是丢失字节并且无法弄清楚为什么。这一定是有原因的,微妙的错误?
private static void _TestEncryption2()
{
var testText = "Hello world";
var plainBytes = Encoding.Unicode.GetBytes(testText);
var cipher = Rijndael.Create();
cipher.Padding = PaddingMode.PKCS7;
//var _key = Convert.FromBase64String(KEY);
//var _IV = Convert.FromBase64String(IV);
var _key = cipher.Key;
var _IV = cipher.IV;
byte[] cipherBytes;
byte[] decryptedBytes;
var enc = cipher.CreateEncryptor(_key, _IV);
using (var memoryStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(memoryStream, enc, CryptoStreamMode.Write))
{
cryptoStream.Write(plainBytes, 0, plainBytes.Length);
}
cipherBytes = memoryStream.ToArray();
}
var dec = cipher.CreateDecryptor(_key, _IV);
using (var memoryStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(memoryStream, dec, CryptoStreamMode.Write))
{
cryptoStream.Write(cipherBytes, 0, cipherBytes.Length);
decryptedBytes = memoryStream.ToArray();
}
}
var decryptedText = Encoding.Unicode.GetString(decryptedBytes);
}
在执行 memoryStream.ToArray()
:
之前处理你的第二个 cryptoStream
var dec = cipher.CreateDecryptor(_key, _IV);
using (var memoryStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(memoryStream, dec, CryptoStreamMode.Write))
{
cryptoStream.Write(cipherBytes, 0, cipherBytes.Length);
}
// Here
decryptedBytes = memoryStream.ToArray();
}
问题是第二个 CryptoStream
在你读取它的输出之前没有被刷新,它仍然在内部保留最后一个块。
您的块大小为 16 个字节,并且您使用的是 UTF-16,每个字符使用 2 个字节,因此您得到的是第一个 8 个字符(16 个字节)的块,而不是最后一个块,在这里有意义。
此外,Encoding.Unicode
是 UTF-16,这在当今时代是一个有点奇怪的选择。您的默认值应该是 Encoding.UTF8
.
全部,
请看下面的代码。加密、解密代码无法正常工作。 当我传入字符串“Hello world”时,我返回“Hello wo”。我总是丢失字节并且无法弄清楚为什么。这一定是有原因的,微妙的错误?
private static void _TestEncryption2()
{
var testText = "Hello world";
var plainBytes = Encoding.Unicode.GetBytes(testText);
var cipher = Rijndael.Create();
cipher.Padding = PaddingMode.PKCS7;
//var _key = Convert.FromBase64String(KEY);
//var _IV = Convert.FromBase64String(IV);
var _key = cipher.Key;
var _IV = cipher.IV;
byte[] cipherBytes;
byte[] decryptedBytes;
var enc = cipher.CreateEncryptor(_key, _IV);
using (var memoryStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(memoryStream, enc, CryptoStreamMode.Write))
{
cryptoStream.Write(plainBytes, 0, plainBytes.Length);
}
cipherBytes = memoryStream.ToArray();
}
var dec = cipher.CreateDecryptor(_key, _IV);
using (var memoryStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(memoryStream, dec, CryptoStreamMode.Write))
{
cryptoStream.Write(cipherBytes, 0, cipherBytes.Length);
decryptedBytes = memoryStream.ToArray();
}
}
var decryptedText = Encoding.Unicode.GetString(decryptedBytes);
}
在执行 memoryStream.ToArray()
:
cryptoStream
var dec = cipher.CreateDecryptor(_key, _IV);
using (var memoryStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(memoryStream, dec, CryptoStreamMode.Write))
{
cryptoStream.Write(cipherBytes, 0, cipherBytes.Length);
}
// Here
decryptedBytes = memoryStream.ToArray();
}
问题是第二个 CryptoStream
在你读取它的输出之前没有被刷新,它仍然在内部保留最后一个块。
您的块大小为 16 个字节,并且您使用的是 UTF-16,每个字符使用 2 个字节,因此您得到的是第一个 8 个字符(16 个字节)的块,而不是最后一个块,在这里有意义。
此外,Encoding.Unicode
是 UTF-16,这在当今时代是一个有点奇怪的选择。您的默认值应该是 Encoding.UTF8
.