У меня есть приложение, которое хранит некоторые файлы в изолированном хранилище и нуждается в расшифровке при открытии файла. Я попытался загрузить IsolatedStorageFileStream в байтовый массив и запустить расшифровку AES256. Когда размер файла небольшой, он работает нормально. Однако, когда размер файла огромен, скажем, около 120 МБ, он вызывает исключение 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? Спасибо.