我的代码是
private byte[] Invoke(Stream inputFileStream, CryptoAction action)
{
var msData = new MemoryStream();
CryptoStream cs = null;
try
{
long inputFileLength = inputFileStream.Length;
var byteBuffer = new byte[4096];
long bytesProcessed = 0;
int bytesInCurrentBlock = 0;
var csRijndael = new RijndaelManaged();
switch (action)
{
case CryptoAction.Encrypt:
cs = new CryptoStream(msData, csRijndael.CreateEncryptor(this.Key, this.IV), CryptoStreamMode.Write);
break;
case CryptoAction.Decrypt:
cs = new CryptoStream(msData, csRijndael.CreateDecryptor(this.Key, this.IV), CryptoStreamMode.Write);
break;
}
while (bytesProcessed < inputFileLength)
{
bytesInCurrentBlock = inputFileStream.Read(byteBuffer, 0, 4096);
cs.Write(byteBuffer, 0, bytesInCurrentBlock);
bytesProcessed += bytesInCurrentBlock;
}
cs.FlushFinalBlock();
return msData.ToArray();
}
catch
{
return null;
}
}
如果加密大小为 60mb 的大文件 System.OutOfMemoryException 被抛出并且程序崩溃。我的操作系统是 64 位并且有 8Gb 的内存。