CryptoStream 实例关闭时抛出异常

发布于 2024-12-26 23:17:24 字数 2269 浏览 0 评论 0原文

我有一个使用 RijndaelManaged Cipher 的 DecryptString 函数。它在 99.999% 的情况下都能工作,但偶尔会抛出“IndexOutOfRangeException”异常,并显示“索引超出数组范围”的消息。当它尝试关闭finally块中的cryptoStream时。

当它停止工作时,它将停止工作 20 分钟左右,然后再启动 再次工作,没有明显的解释。我确实有一个线索,那就是它只能

public string DecryptString(string InputText, string Password)
        {
            //checkParamSupplied("InputText", InputText);
            checkParamSupplied("Password", Password);

            MemoryStream memoryStream = null;
            CryptoStream cryptoStream = null;

            try
            {
                RijndaelManaged RijndaelCipher = new RijndaelManaged();

                byte[] EncryptedData = Convert.FromBase64String(InputText);

                byte[] Salt = Encoding.ASCII.GetBytes(Password.Length.ToString());

                PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(Password, Salt);

                // Create a decryptor from the existing SecretKey bytes.
                ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16));

                memoryStream = new MemoryStream(EncryptedData);

                // Create a CryptoStream. (always use Read mode for decryption).
                cryptoStream = new CryptoStream(memoryStream, Decryptor, CryptoStreamMode.Read);

                // Since at this point we don't know what the size of decrypted data
                // will be, allocate the buffer long enough to hold EncryptedData;
                // DecryptedData is never longer than EncryptedData.
                byte[] PlainText = new byte[EncryptedData.Length];

                // Start decrypting.
                int DecryptedCount = cryptoStream.Read(PlainText, 0, PlainText.Length);

                // Convert decrypted data into a string. 
                string DecryptedData = Encoding.Unicode.GetString(PlainText, 0, DecryptedCount);

                // Return decrypted string.   
                return DecryptedData;
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                //Close both streams.
                memoryStream.Close();
                cryptoStream.Close();
            }
        }

I have a DecryptString function that uses the RijndaelManaged Cipher. It works 99.999% of the time but very occationally it throws an "IndexOutOfRangeException" exception with the message of "Index was outside the bounds of the array." when it tries to close the cryptoStream in the finally block.

When it does stop working it will stop working for a period of 20 minutes or so and then start
working again with not obvious explination. One lead I do have is that it only st

public string DecryptString(string InputText, string Password)
        {
            //checkParamSupplied("InputText", InputText);
            checkParamSupplied("Password", Password);

            MemoryStream memoryStream = null;
            CryptoStream cryptoStream = null;

            try
            {
                RijndaelManaged RijndaelCipher = new RijndaelManaged();

                byte[] EncryptedData = Convert.FromBase64String(InputText);

                byte[] Salt = Encoding.ASCII.GetBytes(Password.Length.ToString());

                PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(Password, Salt);

                // Create a decryptor from the existing SecretKey bytes.
                ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16));

                memoryStream = new MemoryStream(EncryptedData);

                // Create a CryptoStream. (always use Read mode for decryption).
                cryptoStream = new CryptoStream(memoryStream, Decryptor, CryptoStreamMode.Read);

                // Since at this point we don't know what the size of decrypted data
                // will be, allocate the buffer long enough to hold EncryptedData;
                // DecryptedData is never longer than EncryptedData.
                byte[] PlainText = new byte[EncryptedData.Length];

                // Start decrypting.
                int DecryptedCount = cryptoStream.Read(PlainText, 0, PlainText.Length);

                // Convert decrypted data into a string. 
                string DecryptedData = Encoding.Unicode.GetString(PlainText, 0, DecryptedCount);

                // Return decrypted string.   
                return DecryptedData;
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                //Close both streams.
                memoryStream.Close();
                cryptoStream.Close();
            }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝礼 2025-01-02 23:17:24

我注意到的一件事是,在您的 finally 块中关闭 cryptoStream 本身之前,您要关闭 cryptoStream 的基础流 - 也许您应该颠倒这两个语句?

One thing I do notice is that you're closing the cryptoStream's underlying stream before you close the cryptoStream itself in your finally block - perhaps you should reverse these two statements?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文