Re: Encryption / string encoding
From: Sparky (mark.smith_at_nospam.farmade.com)
Date: 05/23/03
- Next message: J-P Meunier: "Xml signature: Adding reference on Object child"
- Previous message: Paul: "Sending email via System.Web.Mail.SmtpMail"
- In reply to: Rich Bernstein: "Re: Encryption / string encoding"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Date: Fri, 23 May 2003 11:40:14 +0100
Hi Rich
I take everything back in my last post - it still doesn't work! I had
commented out my calls to the encryption stuff.... I get the "Stupid Idiot
Of The Day" award but nevermind!
Anyway, in _DecryptString, I can strip out the null chars on the end and all
works ok now (honest!)
Return BytesToString(msPlain.GetBuffer).TrimEnd(Chr(0))
Thanks again for the input :-)
"Rich Bernstein" <rab38@cornell.edu> wrote in message
news:baj5ii$b3e$1@news01.cit.cornell.edu...
> Looks like you're leaving off the last byte of data in the byte array
during
> decryption. Change "EncryptedStream.Length - 1" in DecryptStream to
> "EncryptedStream.Length".
>
> --
> -Rich
> "Sparky" <mark.smith@nospam.farmade.com> wrote in message
> news:#lytGgHIDHA.612@TK2MSFTNGP11.phx.gbl...
> > Hi
> >
> > I'm using Visual Studio Magazines Crypto class, as described in Jan
2003.
> > The problem that I'm having is that I am encrypting a string, and then
at
> a
> > later date decrypting the string. If I debug.print the decrypted string,
> it
> > is correct but has no end quote displayed.
> >
> > ie. in the output window, rather than "hello" being displayed, "hello is
> > displayed. It looks like the string is now longer than it ought to be
but
> > has funny characters appended to the end. This affects webservices,. ie.
I
> > can't pass the string, and also affects sql as I can't use that string
in
> an
> > sql statement. I have tried Trim'ming etc but to no avail. All I want is
> my
> > original string back....
> >
> > I've pasted below the encryption / decrption code. I think it's just a
> case
> > of doing something like Convert.ToString (although that doesn't work but
I
> > suspect something similar will!)
> >
> > Any help much appreciated!
> >
> > Mark
> >
> >
> > ==========================
> > Public Function DecryptString(ByVal EncryptedString As String) As
String
> > If mbKeyIsSet Then
> > Return _DecryptString(EncryptedString, True)
> > Else
> > Throw New Exception(mksKeyNotSetException)
> > End If
> > End Function
> >
> >
> > Private Function _DecryptString(ByVal EncryptedString As String, ByVal
> > Base64 As Boolean) As String
> > Try
> > 'put string in byte array depending on Base64 flag
> > Dim byteArray() As Byte
> > If Base64 Then
> > byteArray = Convert.FromBase64String(EncryptedString)
> > Else
> > byteArray = BytesFromString(EncryptedString)
> > End If
> > 'create the streams, decrypt and return a string
> > Dim msEnc As New MemoryStream(byteArray)
> > Dim msPlain As MemoryStream = DecryptStream(msEnc)
> > Return BytesToString(msPlain.GetBuffer)
> > Catch ex As Exception
> > 'Debug.WriteLine("_DecryptString Error: " & ex.ToString)
> > 'Return String.Empty
> > cFmsException.ThrowFMS(Me.GetType.FullName, ex)
> > End Try
> > End Function
> >
> > Private Function BytesToString(ByVal byteArray() As Byte) As String
> > Return (New UnicodeEncoding).GetString(byteArray)
> > 'Return (New ASCIIEncoding).GetString(byteArray)
> > End Function
> >
> >
> >
> > 'encrypt a string - wrapper without Base64 flag (True by default)
> > Public Function EncryptString(ByVal PlainText As String) As String
> > If mbKeyIsSet Then
> > Return _EncryptString(PlainText, True)
> > Else
> > Throw New Exception(mksKeyNotSetException)
> > End If
> > End Function
> >
> >
> > Private Function _EncryptString(ByVal PlainText As String, ByVal Base64
> As
> > Boolean) As String
> > Try
> > 'put string in byte array
> > Dim byteArray() As Byte = BytesFromString(PlainText)
> > 'create streams and encrypt
> > Dim msPlain As New MemoryStream(byteArray)
> > Dim msEnc As MemoryStream = EncryptStream(msPlain)
> > 'return string depending on Base64 flag
> > If Base64 Then
> > Return Convert.ToBase64String(msEnc.ToArray)
> > Else
> > Return BytesToString(msEnc.ToArray)
> > End If
> > Catch ex As Exception
> > 'Debug.WriteLine("_EncryptString Error: " & ex.ToString)
> > 'Return String.Empty
> > cFmsException.ThrowFMS(Me.GetType.FullName, ex)
> > End Try
> > End Function
> >
> >
> > Public Function EncryptStream(ByVal PlainStream As MemoryStream) As
> > MemoryStream
> > If mbKeyIsSet Then
> > Try
> > 'open stream for encrypted data
> > Dim encStream As New MemoryStream
> > 'create Crypto Service Provider, set key, transform and crypto
stream
> > Dim oCSP As New RijndaelManaged
> > oCSP.Key = mbytKey
> > oCSP.IV = mbytIV
> > Dim ct As ICryptoTransform = oCSP.CreateEncryptor()
> > Dim cs As CryptoStream = New CryptoStream(encStream, ct,
> > CryptoStreamMode.Write)
> > 'get input stream into byte array
> > Dim byteArray() As Byte = PlainStream.ToArray()
> > 'write input bytes to crypto stream and close up
> > cs.Write(byteArray, 0, CType(PlainStream.Length, Integer))
> > cs.FlushFinalBlock()
> > cs.Close()
> > Return encStream
> > Catch ex As Exception
> > 'Debug.WriteLine("EncryptStream Error: " & ex.ToString)
> > 'Return Stream.Null
> > cFmsException.ThrowFMS(Me.GetType.FullName, ex)
> > End Try
> > Else
> > Throw New Exception(mksKeyNotSetException)
> > End If
> > End Function
> >
> >
> > Public Function DecryptStream(ByVal EncryptedStream As MemoryStream) As
> > MemoryStream
> > If mbKeyIsSet Then
> > Try
> > 'create Crypto Service Provider, set key, transform and crypto
stream
> > Dim oCSP As New RijndaelManaged
> > oCSP.Key = mbytKey
> > oCSP.IV = mbytIV
> > Dim ct As ICryptoTransform = oCSP.CreateDecryptor()
> > Dim cs As CryptoStream = New CryptoStream(EncryptedStream, ct,
> > CryptoStreamMode.Read)
> > 'get bytes from encrypted stream
> > Dim byteArray(CType(EncryptedStream.Length - 1, Integer)) As Byte
> > Dim iBytesIn As Integer = cs.Read(byteArray, 0,
> > CType(EncryptedStream.Length, Integer))
> > cs.Close()
> > 'create and write the decrypted output stream
> > Dim plainStream As New MemoryStream
> > plainStream.Write(byteArray, 0, iBytesIn)
> > Return plainStream
> > Catch ex As Exception
> > 'Debug.WriteLine("DecryptStream Error: " & ex.ToString)
> > 'Return Stream.Null
> > cFmsException.ThrowFMS(Me.GetType.FullName, ex)
> > End Try
> > Else
> > Throw New Exception(mksKeyNotSetException)
> > End If
> > End Function
> >
> >
>
>
- Next message: J-P Meunier: "Xml signature: Adding reference on Object child"
- Previous message: Paul: "Sending email via System.Web.Mail.SmtpMail"
- In reply to: Rich Bernstein: "Re: Encryption / string encoding"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Relevant Pages
|