OK, so having just got the key to be accepted and have an encrypted string out which is 44 char long, I now am unable to decrypt (aarrgghh):
Length of the data to decrypt is invalid.
Having looked around and read various posts it looks as though the conversion to a Base64String may be the problem but I can't see where it is wrong - many of the solutions I have seen appear to be identical to what I have. Again, I'd really appreciate any help - extracts below:
Encryption/Decryption Function
Private byteKey As Byte() = Encoding.UTF8.GetBytes("B499F4BF48242E05548D1E4C8BB26A2E")
Private byteIV As Byte() = Encoding.UTF8.GetBytes(",%u'm&'CXSy/T7x;4")
Private Function Rijndael(ByVal sInput As String, ByVal bEncrypt As Boolean) As String
' Create an instance of encyrption algorithm.
Dim _rijndael As New RijndaelManaged()
' Create an encryptor using key and IV - already available in byte() as byteKey and byteIV
Dim transform As ICryptoTransform
If bEncrypt Then
transform = _rijndael.CreateEncryptor(byteKey, byteIV)
Else
transform = _rijndael.CreateDecryptor(byteKey, byteIV)
End If
' Create streams for input and output
Dim msOutput As New System.IO.MemoryStream()
Dim msInput As New CryptoStream(msOutput, transform, CryptoStreamMode.Write)
' Feed data into the crypto stream.
msInput.Write(Encoding.UTF8.GetBytes(sInput), 0, Encoding.UTF8.GetBytes(sInput).Length)
' Flush crypto stream.
msInput.FlushFinalBlock()
Dim byteOutput As Byte() = msOutput.ToArray
Return Convert.ToBase64String(byteOutput)
End Function
Usage:
Dim sEncrypted As String = Rijndael("This is a test", True)
Dim sDecrypted As String = Rijndael(sEncrypted, False) **This is the line where it is crashing**
EDIT - Final, seemingly working pair of functions (see comment) for ref:
Private byteKey As Byte() = Encoding.UTF8.GetBytes("B499F4BF48242E05548D1E4C8BB26A2E")
Private byteIV As Byte() = Encoding.UTF8.GetBytes(",%u'm&'CXSy/T7x;4")
Public Function Encrypt(ByVal sInput As String) As String
' Create an instance of our encyrption algorithm.
Dim _rijndael As New RijndaelManaged()
' Create an encryptor using our key and IV
Dim transform As ICryptoTransform
transform = _rijndael.CreateEncryptor(byteKey, byteIV)
' Create the streams for input and output
Dim msOutput As New System.IO.MemoryStream()
Dim msInput As New CryptoStream(msOutput, transform, CryptoStreamMode.Write)
' Feed our data into the crypto stream
msInput.Write(Encoding.UTF8.GetBytes(sInput), 0, Encoding.UTF8.GetBytes(sInput).Length)
msInput.FlushFinalBlock()
Return Convert.ToBase64String(msOutput.ToArray)
End Function
Public Function Decrypt(ByVal sInput As String) As String
' Create an instance of our encyrption algorithm.
Dim _rijndael As New RijndaelManaged()
' Create an encryptor using our key and IV
Dim transform As ICryptoTransform
transform = _rijndael.CreateDecryptor(byteKey, byteIV)
' Create the streams for input and output
Dim msOutput As New System.IO.MemoryStream()
Dim msInput As New CryptoStream(msOutput, transform, CryptoStreamMode.Write)
' Feed our data into the crypto stream.
msInput.Write(Convert.FromBase64String(sInput), 0, Convert.FromBase64String(sInput).Length)
msInput.FlushFinalBlock()
Return Encoding.UTF8.GetString(msOutput.ToArray)
End Function
Usage
Dim sEncrypted As String = Encrypt("This is a test")
Dim sDecrypted As String = Decrypt(sEncrypted)