如何系统.在Windows Phone 7从IsolatedStorageFileStream加载时出现OutOfMem
本文关键字:加载 IsolatedStorageFileStream OutOfMem Phone 何系统 系统 Windows | 更新日期: 2023-09-27 18:06:55
我有一个应用程序,在隔离的存储中存储一些文件,需要在文件打开时进行解密。我尝试将IsolatedStorageFileStream加载到字节数组中并启动AES256解密。当文件大小很小时,它工作得很好。但是,当文件大小很大时,比如大约120MB,它将抛出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异常?谢谢。
看起来像是将整个加密文件加载到内存中—如果这是120MB,则需要大量内存。
你应该找到一种加载和解密文件的方法,它可以读取和解密较小的数据块