Try adding a encStream.FlushFinalBlock() just before the close() method. -----Original Message----- From: Moderated discussion of advanced .NET topics. [mailto:[EMAIL PROTECTED]] On Behalf Of Paul Ballard Sent: Tuesday, June 11, 2002 7:48 AM To: [EMAIL PROTECTED] Subject: [ADVANCED-DOTNET] Encryption and Decryption using MemoryStream
I am trying to do a quick encryption/decryption on strings. All of the examples I've seen involve encrypting whole files using FileStream. I have been attempting to simulate that functionality using MemoryStream. However, if I use an exact copy of the MemoryStream that I have encrypted as the source stream for decryption, the result is garbage. Is there a difference in the implementation of MemoryStream compared to FileStream that would cause this, or is there something that you can see in the following code snippet that I am doing wrong? (Always a possibility :-)) Dim memStr As New System.IO.MemoryStream() Private Function Encrypt(ByVal input As String) As String Dim bin As Byte() = New UnicodeEncoding().GetBytes(input) Dim des As New DESCryptoServiceProvider() Dim encStream As New CryptoStream(memStr, des.CreateEncryptor(Key, IV), CryptoStreamMode.Write) encStream.Write(bin, 0, bin.Length) encStream.Close() Return BitConverter.ToString(memStr.ToArray()) End Function Private Function Decrypt(ByVal input As String) 'Make a new copy of the encrypted memory stream Dim memStr2 As New System.IO.MemoryStream(memStr.ToArray()) Dim des2 As New DESCryptoServiceProvider() Dim encStream2 As New CryptoStream(memStr2, des2.CreateDecryptor(Key, IV), CryptoStreamMode.Read) Dim strRdr As New StreamReader(encStream2, True) Dim retStr As String = strRdr.ReadToEnd() encStream2.Close() Return retStr End Function Thanks for the help, Paul Ballard [EMAIL PROTECTED] _________________________________________________________________ MSN Photos is the easiest way to share and print your photos: http://photos.msn.com/support/worldwide.aspx You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced DOTNET, or subscribe to other DevelopMentor lists at http://discuss.develop.com. You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced DOTNET, or subscribe to other DevelopMentor lists at http://discuss.develop.com.