我试图加密RijndaelManaged类缓冲区没有任何成功。它总是返回byte[0]。下面是代码:
public byte[] Encrypt(byte[] data, byte[] key)
{
using (var ms = new MemoryStream())
{
using (var aes = RijndaelManaged.Create())
{
aes.Key = _checksumProvider.CalculateChecksum(key);
aes.IV = _checksumProvider.CalculateChecksum(key);
var stream = new CryptoStream(ms, aes.CreateEncryptor(aes.Key, aes.IV), CryptoStreamMode.Write);
stream.Write(data, 0, data.Length);
return ms.ToArray();
}
}
}
Key和IV被正确分配。知道代码有什么问题吗?谢谢。
您需要呼叫stream.FlushFinalBlock()
这将执行加密中的任何最后步骤,并将CryptoStream的内部缓冲区刷新到底层内存流中。