Strange decrypted character

R

Richard

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
 
J

Joe Kaplan \(MVP - ADSI\)

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

Richard

Excellent! Your changes did it. Thanks Joe.

Joe Kaplan (MVP - ADSI) said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top