我正在玩加密,我发现这会修改我的缓冲区。我没想到会这样。
如果您使用 aesbuf 查看 MemoryStream,您可以清楚地看到我将 CanWrite 设置为 false。然而 aesbuf 被修改了。我知道,因为 b1 是真的,但 b2 是假的。
哇!?这是怎么回事?
byte[] unencrypted_array;
var a1 = (byte[])aesbuf.Clone();
var b1 = a1.ArraysEqual(aesbuf);
using (MemoryStream ms2 = new MemoryStream(aesbuf))
using (var aes = new RijndaelManaged())
{
using (var dec = aes.CreateDecryptor(aeskey, aesiv))
using (MemoryStream ms = new MemoryStream(aesbuf, false))
using (CryptoStream sr = new CryptoStream(ms, dec, CryptoStreamMode.Read))
{
byte[] buf = new byte[1024 * 4];
int l;
while ((l = sr.Read(buf, 0, buf.Length)) != 0)
{
ms2.Write(buf, 0, l);
}
}
ms2.Flush();
unencrypted_array = ms2.ToArray();
}
var b2 = a1.ArraysEqual(aesbuf);