Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# CryptoStream实例关闭时引发异常_C#_Rijndaelmanaged_Cryptostream - Fatal编程技术网

C# CryptoStream实例关闭时引发异常

C# CryptoStream实例关闭时引发异常,c#,rijndaelmanaged,cryptostream,C#,Rijndaelmanaged,Cryptostream,我有一个使用RijndaelManaged密码的DecryptString函数。它在99.999%的时间内工作,但当它试图关闭finally块中的加密流时,偶尔会抛出一个“IndexOutofFrangeException”异常,消息为“Index超出了数组的边界” 当它停止工作时,它将停止工作20分钟左右,然后开始工作 在没有明显解释的情况下再次工作。我确实有一个线索,那就是 public string DecryptString(string InputText, string Passwo

我有一个使用RijndaelManaged密码的DecryptString函数。它在99.999%的时间内工作,但当它试图关闭finally块中的加密流时,偶尔会抛出一个“IndexOutofFrangeException”异常,消息为“Index超出了数组的边界”

当它停止工作时,它将停止工作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();
            }
        }

我注意到的一件事是,在您的
finally
块中关闭加密流之前,您正在关闭加密流的底层流-也许您应该反转这两个语句?

Jonathan-您的问题不完整…文本和代码似乎都被切断了…谢谢,我的浏览器正在播放。我已经添加了代码的其余部分。问题仍然被截断though@Jonathan斯坦顿-我在一条类似的船上,同一个代码会产生随机错误。“我有一个潜在客户(被截断)”是什么?你找到解决方案了吗?可能。使用块将它们包装(嵌套)也可以防止这种混淆和错误。