Have been trying to encrypt an xml file to a string so that I may transfer it over a service. The transmission is from server to server using a symetric key compiled into the code.
I have been using the AES sample from MSDN and then converting the byte arry to and from a string like so:
' Encrypt the string to an array of bytes.
Dim encrypted As Byte() = crypto.EncryptString(original, _key, _iv)
Dim encrypStr As String = Encoding.Unicode.GetString(encrypted)
'''' >>> Transmit...
Dim postTrans As Byte() = Encoding.Unicode.GetBytes(encrypStr)
' Decrypt the bytes to a string.
Dim roundtrip As String = crypto.DecryptString(postTrans, _key, _iv)
Without the middle two line the encrypt/decrypt works fine, with the middle two lines included I either recieve a badly formed xml document that cannot be parsed or a “Padding is invalid and cannot be removed” error.
Is this not a good method for string encryption? It works perfectly without converting the byte() to string ad back.
Do not use Encoding.GetString(), will not always work. Use ToBase64()
Thanks to Henk Holterman for the answer.