title: exception when decrypting with Rijndael
body: <p>I have some code that decrypts a password with Rijndael</p>
  
  <pre><code>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);
      }
  }
  </code></pre>
  
  <p>The problem is that disposing the cryptostream causes an exception</p>
  
  <pre><code>System.IndexOutOfRangeException : Index was outside the bounds of the array.
  at System.Security.Cryptography.RijndaelManagedTransform.DecryptData(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount, Byte[]&amp; 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()  
  </code></pre>
  
  <p>I found some links to similar problems but no solutions.</p>
  
  <p>Is it safe to just remove the Disposing of the cryptostream or will that just cause the finalizer to blow up at a later time?</p>