我有一个使用RijndaelManaged密码的DecryptString函数。它的工作时间为99.999%,但非常偶然,它会抛出“IndexOutOfRangeException”异常,并显示消息“索引超出了数组边界”。当它试图关闭finally块中的cryptoStream时。当CryptoStream实例关闭时抛出异常
当它停止工作时,它会停止工作20分钟左右,然后开始 再次工作,但不明显的爆炸。其中铅我有是,它只有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();
}
}
乔纳森 - 你的问题是不完整的... – 2012-01-12 10:09:27
文本和代码似乎都被切断了... – 2012-01-12 10:12:36
感谢你的,我的浏览器正在玩。我已经添加了其余的代码。 – 2012-01-12 10:23:02