Re: Encryption / string encoding

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

  • Next message: James: "Re: Loading a Textfile public key"
    Date: Thu, 22 May 2003 14:26:14 -0700
    
    

    Extremely well spotted - works as it should do now, many
    thanks!

    >-----Original Message-----
    >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: James: "Re: Loading a Textfile public key"

    Relevant Pages

    • Re: Question about design, defmacro, macrolet, and &environment
      ... "Expects to find the literal string on the stream." ... (defun send (string &optional stream) ... (declaim (inline make-adjustable-string)) ...
      (comp.lang.lisp)
    • Re: Strange problem when not in debugger
      ... private string huidigWeb; ... int buffLength = 2048; ... // Opens a file stream to read the file to be uploaded ...
      (microsoft.public.dotnet.languages.csharp)
    • Re: ReplacerStream
      ... string, do a replace on that string and create a stream again to be ... If those are problems, and you are looking just for a single string, it seems to me that you could just read the stream one character at a time, checking to see if it matches the current character in your search string. ...
      (microsoft.public.dotnet.framework)
    • Re: Strange problem when not in debugger
      ... private string huidigWeb; ... int buffLength = 2048; ... // Opens a file stream to read the file to be uploaded ...
      (microsoft.public.dotnet.languages.csharp)
    • Re: HTTP Object and Retrieving HTML Programatically
      ... I had to hardcode some query string and form post values, ... chunks defined by the buffer size ... //create a stream reader grabbing text we get over HTTP ... while (workingbuffersize> 0) ...
      (microsoft.public.dotnet.framework.aspnet)