0
我有一个应用程序,它将某些文件存储在独立存储中,并且在文件打开时需要解密。我试图将IsolatedStorageFileStream加载到一个字节数组中并启动AES256解密。当文件大小很小时,它工作正常。但是,当文件大小很大时(比如说大约120MB),它会抛出System.OutOfMemoryException。以下是我的部分代码:如何从Windows Phone 7中的IsolatedStorageFileStream加载时System.OutOfMemoryException?
IsolatedStorageFileStream m_rawStream =
IsolatedStorageFile.GetUserStoreForApplication().OpenFile("shared\\transfers\\" +
DownloadFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
byte[] m_rawByte = new byte[_totalByte];
m_rawStream.Read(m_rawByte, 0, _totalByte);
m_rawStream.Close();
//Decrypte downloaded epub
byte[] m_decryptedBytes = _decryptStringFromBytesAES(m_rawByte,
_hexStringToByteArray(_decryptKey),
_hexStringToByteArray(_decryptIV));
//Store on the upper layer of isolated storage
using (var isfs = new IsolatedStorageFileStream(DownloadFileName,
FileMode.Create,
IsolatedStorageFile.GetUserStoreForApplication()))
{
isfs.Write(m_decryptedBytes, 0, m_decryptedBytes.Length);
isfs.Flush();
}
//AES Decrypt helper
private byte[] _decryptStringFromBytesAES(byte[] cipherText, byte[] Key, byte[] IV)
{
// TDeclare the streams used
// to decrypt to an in memory
// array of bytes.
MemoryStream msDecrypt = null;
CryptoStream csDecrypt = null;
// Declare the RijndaelManaged object
// used to decrypt the data.
AesManaged aesAlg = null;
// Declare the byte[] used to hold
// the decrypted byte.
byte[] resultBytes = null;
try
{
// Create a RijndaelManaged object
// with the specified key and IV.
aesAlg = new AesManaged();
aesAlg.KeySize = 256;
aesAlg.Key = Key;
aesAlg.IV = IV;
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for decryption.
msDecrypt = new MemoryStream();
csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Write);
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
csDecrypt.Write(cipherText, 0, cipherText.Length);
//csDecrypt.FlushFinalBlock();
resultBytes = msDecrypt.ToArray();
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
MessageBoxResult _result = MessageBox.Show("無法開啟檔案,請重試或聯絡技術人員。", "錯誤", MessageBoxButton.OK);
if (_result == MessageBoxResult.OK)
{
new SimpleNavigationService().Navigate("/View/MainPange.xaml");
}
}
return resultBytes;
}
如何避免获得OutOfMemory异常?谢谢。
我第二。几年前,我们在流上实现了AES解密,因此可以完成。我不记得细节了。 –
顺便说一句,市场测试应用程序使用少于90 MB的内存。 –