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