A first chance exception of type 'System.NotSupportedException' occurred in mscorlib?

Hi, I'm using this code to do encryption. However in VS Express 2005, it generates below exceptions. In 2003, I never noticed that this generated exception. Any1 knows why and how to fix it? weird thing is: although it generates exception, seems it encrypted/decrypted data correctly.

========================================
A first chance exception of type 'System.ObjectDisposedException' occurred in mscorlib.dll
System.ObjectDisposedException: Cannot access a closed Stream.
at System.IO.__Error.StreamIsClosed()
at System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count)
at System.Security.Cryptography.CryptoStream.FlushFinalBlock()
at System.Security.Cryptography.CryptoStream.Dispose(Boolean disposing)
at System.IO.Stream.Close()
at test.clsEncryption.EncryptString128Bit(String vstrTextToBeEncrypted, String vstrEncryptionKey) in C:\Modules\clsDecryption.vb:line 82

========================================
A first chance exception of type 'System.NotSupportedException' occurred in mscorlib.dll
System.NotSupportedException: FlushFinalBlock() method was called twice on a CryptoStream. It can only be called once.
at System.Security.Cryptography.CryptoStream.FlushFinalBlock()
at test.clsEncryption.DecryptString128Bit(String vstrStringToBeDecrypted, String vstrDecryptionKey) in C:\Modules\clsDecryption.vb:line 166

this happens at :
objCryptoStream.FlushFinalBlock()
objMemoryStream.Close()
objCryptoStream.Close()
========================================

visual basic code:

Imports System.Security.Cryptography
Imports System.IO
Imports System.Text

Module mod_Encryption
Public Function EncryptString128Bit(ByVal vstrTextToBeEncrypted As String, _
ByVal vstrEncryptionKey As String) As String

Dim bytValue() As Byte
Dim bytKey() As Byte
Dim bytEncoded() As Byte
Dim bytIV() As Byte = {121, 241, 10, 1, 132, 74, 11, 39, 255, 91, 45, 78, 14, 211, 22, 62}
Dim intLength As Integer
Dim intRemaining As Integer
Dim objMemoryStream As New MemoryStream
Dim objCryptoStream As CryptoStream
Dim objRijndaelManaged As RijndaelManaged

vstrTextToBeEncrypted = StripNullCharacters(vstrTextToBeEncrypted)
bytValue = Encoding.ASCII.GetBytes(vstrTextToBeEncrypted.ToCharArray)
intLength = Len(vstrEncryptionKey)

If intLength >= 32 Then
vstrEncryptionKey = Strings.Left(vstrEncryptionKey, 32)
Else
intLength = Len(vstrEncryptionKey)
intRemaining = 32 - intLength
vstrEncryptionKey = vstrEncryptionKey & Strings.StrDup(intRemaining, "X")
End If

bytKey = Encoding.ASCII.GetBytes(vstrEncryptionKey.ToCharArray)
objRijndaelManaged = New RijndaelManaged

Try

objCryptoStream = New CryptoStream(objMemoryStream, _
objRijndaelManaged.CreateEncryptor(bytKey, bytIV), _
CryptoStreamMode.Write)
objCryptoStream.Write(bytValue, 0, bytValue.Length)

objCryptoStream.FlushFinalBlock()

bytEncoded = objMemoryStream.ToArray
objMemoryStream.Close()
objCryptoStream.Close()
Catch

End Try

Return Convert.ToBase64String(bytEncoded)

End Function

Public Function DecryptString128Bit(ByVal vstrStringToBeDecrypted As String, _
ByVal vstrDecryptionKey As String) As String

Dim bytDataToBeDecrypted() As Byte
Dim bytTemp() As Byte
Dim bytIV() As Byte = {121, 241, 10, 1, 132, 74, 11, 39, 255, 91, 45, 78, 14, 211, 22, 62}
Dim objRijndaelManaged As New RijndaelManaged
Dim objMemoryStream As MemoryStream
Dim objCryptoStream As CryptoStream
Dim bytDecryptionKey() As Byte

Dim intLength As Integer
Dim intRemaining As Integer
Dim intCtr As Integer
Dim strReturnString As String = String.Empty
Dim achrCharacterArray() As Char
Dim intIndex As Integer

bytDataToBeDecrypted = Convert.FromBase64String(vstrStringToBeDecrypted)
intLength = Len(vstrDecryptionKey)

If intLength >= 32 Then
vstrDecryptionKey = Strings.Left(vstrDecryptionKey, 32)
Else
intLength = Len(vstrDecryptionKey)
intRemaining = 32 - intLength
vstrDecryptionKey = vstrDecryptionKey & Strings.StrDup(intRemaining, "X")
End If

bytDecryptionKey = Encoding.ASCII.GetBytes(vstrDecryptionKey.ToCharArray)

ReDim bytTemp(bytDataToBeDecrypted.Length)

objMemoryStream = New MemoryStream(bytDataToBeDecrypted)

Try

objCryptoStream = New CryptoStream(objMemoryStream, _
objRijndaelManaged.CreateDecryptor(bytDecryptionKey, bytIV), _
CryptoStreamMode.Read)

objCryptoStream.Read(bytTemp, 0, bytTemp.Length)

objCryptoStream.FlushFinalBlock()
objMemoryStream.Close()
objCryptoStream.Close()

Catch

End Try

Return StripNullCharacters(Encoding.ASCII.GetString(bytTemp))
End Function


Public Function StripNullCharacters(ByVal vstrStringWithNulls As String) As String

Dim intPosition As Integer
Dim strStringWithOutNulls As String

intPosition = 1
strStringWithOutNulls = vstrStringWithNulls

Do While intPosition > 0
intPosition = InStr(intPosition, vstrStringWithNulls, vbNullChar)

If intPosition > 0 Then
strStringWithOutNulls = Left$(strStringWithOutNulls, intPosition - 1) & _
Right$(strStringWithOutNulls, Len(strStringWithOutNulls) - intPosition)
End If

If intPosition > strStringWithOutNulls.Length Then
Exit Do
End If
Loop

Return strStringWithOutNulls

End Function
End Module

[5728 byte] By [temp12000] at [2007-12-17]