开发者

How to System.OutOfMemoryException when loading from IsolatedStorageFileStream in Windows Phone 7?

I have an app which store some files in the isolated storage and need to do decryption when the file is being opened. I tried to load the IsolatedStorageFileStream into a byte array and start the AES256 decryption. When the file size is small, it works fine. However, when the file size is huge, say about 120MB, it will throw System.OutOfMemoryException. The following is part of my code:

                        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;

    }

How can I avoid to get OutOfMemory exception? Thanks.


It rather looks like you're loading the entire encrypted file into memory - and if that is 120MB, it takes a lot of memory.

You should find a way of loading and decrypting the file which reads and decrypts the data in smaller chunks of data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜