Strange decrypted character

Discussion in 'ASP .Net Security' started by Richard, Jul 12, 2005.

  1. Richard

    Richard Guest

    I'm using the RijndaelManaged example from MSDN, tweaked slightly to return a
    string. Encryption goes well, but when I use the Decrypt function, the return
    value is in the format of "1234
    Notice there is no ending quotation mark, so when the value is used in a
    report, it looks like 1234☺, whatever that last character is. It looks like a
    "y" with 2 dots over it. I've tried ASCIIEncoding, and UTF8Encoding, but both
    return the same results. The value should just be "1234".

    Here is the function:
    Public Shared Function Decrypt(ByVal stringToDecrypt As String) As String

    Dim myRijndael As New RijndaelManaged
    'KEY_STRING and INIT_VECTOR are constants with string values.
    Dim key() As Byte = Convert.FromBase64String(KEY_STRING)
    Dim IV() As Byte = Convert.FromBase64String(INIT_VECTOR)
    Dim encrypted() As Byte = Convert.FromBase64String(stringToDecrypt)
    Dim fromEncrypt() As Byte = New Byte(encrypted.Length) {}
    Dim encryptedString As String
    Dim textConverter As New UTF8Encoding
    Dim finalString As String

    Dim decryptor As ICryptoTransform = myRijndael.CreateDecryptor(key,
    IV)
    Dim msDecrypt As New MemoryStream(encrypted)
    Dim csDecrypt As New CryptoStream(msDecrypt, decryptor,
    CryptoStreamMode.Read)

    'Read the data out of the crypto stream.
    csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length)

    finalString = textConverter.GetString(fromEncrypt)
    Return finalString
    'I've also tried finalString.Trim, finalString.TrimEnd,
    finalString.ToString.Trim

    End Function

    Here is the Encrypt function if it helps:
    Public Shared Function Encrypt(ByVal stringToEncrypt As String) As String

    Dim textConverter As New UTF8Encoding
    Dim myRijndael As New RijndaelManaged
    Dim toEncrypt() As Byte
    Dim key() As Byte = Convert.FromBase64String(KEY_STRING)
    Dim IV() As Byte = Convert.FromBase64String(INIT_VECTOR)

    'Get an encryptor.
    Dim encryptor As ICryptoTransform = myRijndael.CreateEncryptor(key,
    IV)

    'Encrypt the data.
    Dim msEncrypt As New MemoryStream
    Dim csEncrypt As New CryptoStream(msEncrypt, encryptor,
    CryptoStreamMode.Write)

    'Convert the data to a byte array.
    toEncrypt = textConverter.GetBytes(stringToEncrypt)

    'Write all data to the crypto stream and flush it.
    csEncrypt.Write(toEncrypt, 0, toEncrypt.Length)
    csEncrypt.FlushFinalBlock()

    'Get encrypted array of bytes.
    Return Convert.ToBase64String(msEncrypt.ToArray)

    End Function
    Richard, Jul 12, 2005
    #1
    1. Advertising

  2. I changed part of your decrypt function to this:

    Dim msDecrypt As New MemoryStream()
    Dim csDecrypt As New CryptoStream(msDecrypt, decryptor,
    CryptoStreamMode.Write)

    csDecrypt.Write(encrypted, 0, encrypted.Length)
    csDecrypt.FlushFinalBlock()

    finalString = textConverter.GetString(msDecrypt.ToArray())

    There is a good reference implementation here that demonstrates this (in
    C#):
    http://www.dotnetthis.com/Articles/Crypto.htm

    Joe K.

    "Richard" <> wrote in message
    news:...
    > I'm using the RijndaelManaged example from MSDN, tweaked slightly to
    > return a
    > string. Encryption goes well, but when I use the Decrypt function, the
    > return
    > value is in the format of "1234
    > Notice there is no ending quotation mark, so when the value is used in a
    > report, it looks like 1234?, whatever that last character is. It looks
    > like a
    > "y" with 2 dots over it. I've tried ASCIIEncoding, and UTF8Encoding, but
    > both
    > return the same results. The value should just be "1234".
    >
    > Here is the function:
    > Public Shared Function Decrypt(ByVal stringToDecrypt As String) As
    > String
    >
    > Dim myRijndael As New RijndaelManaged
    > 'KEY_STRING and INIT_VECTOR are constants with string values.
    > Dim key() As Byte = Convert.FromBase64String(KEY_STRING)
    > Dim IV() As Byte = Convert.FromBase64String(INIT_VECTOR)
    > Dim encrypted() As Byte = Convert.FromBase64String(stringToDecrypt)
    > Dim fromEncrypt() As Byte = New Byte(encrypted.Length) {}
    > Dim encryptedString As String
    > Dim textConverter As New UTF8Encoding
    > Dim finalString As String
    >
    > Dim decryptor As ICryptoTransform = myRijndael.CreateDecryptor(key,
    > IV)
    > Dim msDecrypt As New MemoryStream(encrypted)
    > Dim csDecrypt As New CryptoStream(msDecrypt, decryptor,
    > CryptoStreamMode.Read)
    >
    > 'Read the data out of the crypto stream.
    > csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length)
    >
    > finalString = textConverter.GetString(fromEncrypt)
    > Return finalString
    > 'I've also tried finalString.Trim, finalString.TrimEnd,
    > finalString.ToString.Trim
    >
    > End Function
    >
    > Here is the Encrypt function if it helps:
    > Public Shared Function Encrypt(ByVal stringToEncrypt As String) As
    > String
    >
    > Dim textConverter As New UTF8Encoding
    > Dim myRijndael As New RijndaelManaged
    > Dim toEncrypt() As Byte
    > Dim key() As Byte = Convert.FromBase64String(KEY_STRING)
    > Dim IV() As Byte = Convert.FromBase64String(INIT_VECTOR)
    >
    > 'Get an encryptor.
    > Dim encryptor As ICryptoTransform = myRijndael.CreateEncryptor(key,
    > IV)
    >
    > 'Encrypt the data.
    > Dim msEncrypt As New MemoryStream
    > Dim csEncrypt As New CryptoStream(msEncrypt, encryptor,
    > CryptoStreamMode.Write)
    >
    > 'Convert the data to a byte array.
    > toEncrypt = textConverter.GetBytes(stringToEncrypt)
    >
    > 'Write all data to the crypto stream and flush it.
    > csEncrypt.Write(toEncrypt, 0, toEncrypt.Length)
    > csEncrypt.FlushFinalBlock()
    >
    > 'Get encrypted array of bytes.
    > Return Convert.ToBase64String(msEncrypt.ToArray)
    >
    > End Function
    >
    Joe Kaplan \(MVP - ADSI\), Jul 14, 2005
    #2
    1. Advertising

  3. Richard

    Richard Guest

    Excellent! Your changes did it. Thanks Joe.

    "Joe Kaplan (MVP - ADSI)" wrote:

    > I changed part of your decrypt function to this:
    >
    > Dim msDecrypt As New MemoryStream()
    > Dim csDecrypt As New CryptoStream(msDecrypt, decryptor,
    > CryptoStreamMode.Write)
    >
    > csDecrypt.Write(encrypted, 0, encrypted.Length)
    > csDecrypt.FlushFinalBlock()
    >
    > finalString = textConverter.GetString(msDecrypt.ToArray())
    >
    > There is a good reference implementation here that demonstrates this (in
    > C#):
    > http://www.dotnetthis.com/Articles/Crypto.htm
    >
    > Joe K.
    >
    > "Richard" <> wrote in message
    > news:...
    > > I'm using the RijndaelManaged example from MSDN, tweaked slightly to
    > > return a
    > > string. Encryption goes well, but when I use the Decrypt function, the
    > > return
    > > value is in the format of "1234
    > > Notice there is no ending quotation mark, so when the value is used in a
    > > report, it looks like 1234?, whatever that last character is. It looks
    > > like a
    > > "y" with 2 dots over it. I've tried ASCIIEncoding, and UTF8Encoding, but
    > > both
    > > return the same results. The value should just be "1234".
    > >
    > > Here is the function:
    > > Public Shared Function Decrypt(ByVal stringToDecrypt As String) As
    > > String
    > >
    > > Dim myRijndael As New RijndaelManaged
    > > 'KEY_STRING and INIT_VECTOR are constants with string values.
    > > Dim key() As Byte = Convert.FromBase64String(KEY_STRING)
    > > Dim IV() As Byte = Convert.FromBase64String(INIT_VECTOR)
    > > Dim encrypted() As Byte = Convert.FromBase64String(stringToDecrypt)
    > > Dim fromEncrypt() As Byte = New Byte(encrypted.Length) {}
    > > Dim encryptedString As String
    > > Dim textConverter As New UTF8Encoding
    > > Dim finalString As String
    > >
    > > Dim decryptor As ICryptoTransform = myRijndael.CreateDecryptor(key,
    > > IV)
    > > Dim msDecrypt As New MemoryStream(encrypted)
    > > Dim csDecrypt As New CryptoStream(msDecrypt, decryptor,
    > > CryptoStreamMode.Read)
    > >
    > > 'Read the data out of the crypto stream.
    > > csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length)
    > >
    > > finalString = textConverter.GetString(fromEncrypt)
    > > Return finalString
    > > 'I've also tried finalString.Trim, finalString.TrimEnd,
    > > finalString.ToString.Trim
    > >
    > > End Function
    > >
    > > Here is the Encrypt function if it helps:
    > > Public Shared Function Encrypt(ByVal stringToEncrypt As String) As
    > > String
    > >
    > > Dim textConverter As New UTF8Encoding
    > > Dim myRijndael As New RijndaelManaged
    > > Dim toEncrypt() As Byte
    > > Dim key() As Byte = Convert.FromBase64String(KEY_STRING)
    > > Dim IV() As Byte = Convert.FromBase64String(INIT_VECTOR)
    > >
    > > 'Get an encryptor.
    > > Dim encryptor As ICryptoTransform = myRijndael.CreateEncryptor(key,
    > > IV)
    > >
    > > 'Encrypt the data.
    > > Dim msEncrypt As New MemoryStream
    > > Dim csEncrypt As New CryptoStream(msEncrypt, encryptor,
    > > CryptoStreamMode.Write)
    > >
    > > 'Convert the data to a byte array.
    > > toEncrypt = textConverter.GetBytes(stringToEncrypt)
    > >
    > > 'Write all data to the crypto stream and flush it.
    > > csEncrypt.Write(toEncrypt, 0, toEncrypt.Length)
    > > csEncrypt.FlushFinalBlock()
    > >
    > > 'Get encrypted array of bytes.
    > > Return Convert.ToBase64String(msEncrypt.ToArray)
    > >
    > > End Function
    > >

    >
    >
    >
    Richard, Jul 19, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Velvet
    Replies:
    9
    Views:
    14,761
    Joerg Jooss
    Jan 19, 2006
  2. raavi
    Replies:
    2
    Views:
    891
    raavi
    Mar 2, 2006
  3. Ranginald
    Replies:
    5
    Views:
    431
    Nobody
    Nov 25, 2006
  4. Steve
    Replies:
    3
    Views:
    2,904
    marss
    Aug 28, 2007
  5. Anders Bergkvist

    Decrypted licensekey

    Anders Bergkvist, Jun 2, 2007, in forum: ASP .Net Security
    Replies:
    1
    Views:
    104
    PlatinumBay
    Jun 4, 2007
Loading...

Share This Page