Re: Encryption / string encoding

From: Sparky (mark.smith_at_nospam.farmade.com)
Date: 05/23/03


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
> >
> >
>
>



Relevant Pages

  • Re: Encryption/Decryption Changes File Size
    ... I was able to get rid of the extra bytes in the encryption by changing ... ' Write the byte array to the crypto stream and flush it. ... Dim bufferAs Byte ... ' Read a chunk of data from the MemoryStream ...
    (microsoft.public.dotnet.languages.vb)
  • Re: encrypting/ decrypting with RSA
    ... should not be able to distinguish encryption of two messages of his/her own ... otherwise semantic security is totally compromised*. ... > Function RSADecrypt(ByVal instring As String) As String ... > Dim RSA As RSACryptoServiceProvider = New ...
    (microsoft.public.dotnet.security)
  • Re: Encrypt My.Settings setting?
    ... dim EncClass as new Encryption ... dim txtPlainTextPassword as string = "ThisIsMyNewPasswordSoThere!" ... Here's the Encryption Class... ... ' Create the encoder to write to the stream. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Encrypt / Sign ? Not really sure
    ... you may be better off with symmetric encryption which uses the same key for both encryption and decryption. ... I remember learning about Public-Private Key encryption at Uni I remember that once a private and public key are created, they act more or less like the ying and yang of each other. ... Private Shared mPrivateKey As String ... Dim RSA As RSACryptoServiceProvider = New RSACryptoServiceProvider ...
    (microsoft.public.dotnet.security)
  • Re: Asymetric Encryption - What am I doing wrong?
    ... > Encryption works fine, but decryption fails with 'Bad Key' every time. ... > Public Function EncryptText(ByVal sToEncrypt As String) As String ... > Dim RSA As New RSACryptoServiceProvider ...
    (microsoft.public.dotnet.security)