I'm using CryptoStream
for decrypting encrypted text in my app. But when I get to the point of either flushing the final block from the stream or closing it (which should do the same), the app closes.
There is no error message, nothing. Even in debug mode, it just ends. In Windows Event Viewer, I get .NET Runtime version 2.0.50727.5485 - Fatal Execution Engine Error (000007FEECCD600A) (80131506)
. I'm targeting framework 2.0 with the app.
Here's my code:
Public Function DecryptData(ByVal encryptedtext As String) As String
Try
' Convert the encrypted text string to a byte array.
Dim encryptedBytes() As Byte = Convert.FromBase64String(encryptedtext)
' Create the stream.
Dim ms As New System.IO.MemoryStream
' Create the decoder to write to the stream.
Dim decStream As New CryptoStream(ms, TripleDes.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write)
' Use the crypto stream to write the byte array to the stream.
decStream.Write(encryptedBytes, 0, encryptedBytes.Length)
decStream.FlushFinalBlock()
' Convert the plaintext stream to a string.
Return System.Text.Encoding.Unicode.GetString(ms.ToArray)
Catch ex As System.Security.Cryptography.CryptographicException
Return ""
End Try
End Function
I tried replacing decStream.FlushFinalBlock()
with decStream.Close()
, and got the same thing. When I comment it out, the string it returns contains only a multiple of 8 characters. The remainder, stuck in that final block, disappears.
So I'm trying to figure out why grabbing the last of the stream is shutting the application down. I put a breakpoint at the source location and used F11 to step into it, but the shutting down is immediate.
I'm using VS 2008 on Windows 7 Professional 64 bit.