Encryption / string encoding

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


Date: Thu, 22 May 2003 16:37:17 +0100


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: Encrypt/hide Password
    ... Public Sub New(ByVal strCryptoName As String) ... ' instantiated crypto class. ... Dim fsKey As New FileStream(strSaveToPath, FileMode.OpenOrCreate, _ ...
    (microsoft.public.scripting.wsh)
  • Search pattern
    ... Dim strfile As String ... Dim bAddressFound As Boolean ... Dim strCurrentChar As String ...
    (comp.databases.ms-access)
  • Auto Write Name and Merge across
    ... Dim Sheetname01 As String ... Dim WeekName01 As String ...
    (microsoft.public.excel.misc)
  • Re: multiplatform (pocketPC & desktopPC) (Daniel !!)
    ... Friend Versione As String ... Public Sub GetMyConnectionPalmare() ... Dim errorMessages As String ... Private Function GetDS_Desktop(ByVal SQL As String) As DataSet ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: multiplatform (pocketPC & desktopPC) (Daniel !!)
    ... Friend Versione As String ... Public Sub GetMyConnectionPalmare() ... Dim errorMessages As String ... Private Function GetDS_Desktop(ByVal SQL As String) As DataSet ...
    (microsoft.public.dotnet.framework.compactframework)