Rijndael でパスワードを解読するコードがあります
public static string DecryptPassword(string encrypted) {
using (MemoryStream ms = new MemoryStream())
using (RijndaelManaged rijndaelManaged = new RijndaelManaged())
using (ICryptoTransform cryptoTransform = rijndaelManaged.CreateDecryptor(mGlobalKey, mGlobalVector))
using (CryptoStream cs = new CryptoStream(ms, cryptoTransform, CryptoStreamMode.Read)) {
byte[] encryptedBytes = Convert.FromBase64String(encrypted);
cs.Write(encryptedBytes, 0, encryptedBytes.Length);
cs.FlushFinalBlock();
return Encoding.Unicode.GetString(ms.GetBuffer(), 0, (int)ms.Length);
}
}
問題は、暗号ストリームを破棄すると例外が発生することです
System.IndexOutOfRangeException : Index was outside the bounds of the array.
at System.Security.Cryptography.RijndaelManagedTransform.DecryptData(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount, Byte[]& outputBuffer, Int32 outputOffset, PaddingMode paddingMode, Boolean fLast)
at System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)
at System.Security.Cryptography.CryptoStream.FlushFinalBlock()
at System.Security.Cryptography.CryptoStream.Dispose(Boolean disposing)
at System.IO.Stream.Close()
at System.IO.Stream.Dispose()
同様の問題へのリンクがいくつか見つかりましたが、解決策はありません。
暗号ストリームの破棄を削除するだけで安全ですか、それとも後でファイナライザーが爆発する原因になりますか?