My problem is actually a bit more complicated than just using AES in VB.NET, because what I'm really trying to do is use AES in VB.NET from a Java application via JACOB. But for now, I need to focus on the implementation of AES itself.
Here is my encryption code
Public Function EncryptAES(ByVal toEncrypt As String, ByVal key As String) As Byte()
Dim keyArray = Convert.FromBase64String(key)
Dim toEncryptArray = Encoding.Unicode.GetBytes(toEncrypt)
Dim aes = New AesCryptoServiceProvider
aes.Key = keyArray
aes.Mode = CipherMode.ECB
aes.Padding = PaddingMode.ISO10126
Dim encryptor = aes.CreateEncryptor()
Dim encrypted = encryptor.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length)
aes.Clear()
Return encrypted
End Function
Back in Java code, I turn the byte array into a hexadecimal string.
Now, to reverse the process, here is my decryption code
Public Function DecryptAES(ByVal toDecrypt As String, ByVal key As String) As Byte()
Dim keyArray = Convert.FromBase64String(key)
Dim toDecryptArray = Convert.FromBase64String(toDecrypt)
Dim aes = New AesCryptoServiceProvider
aes.Key = keyArray
aes.Mode = CipherMode.ECB
aes.Padding = PaddingMode.ISO10126
Dim decryptor = aes.CreateDecryptor()
Dim decrypted = decryptor.TransformFinalBlock(toDecryptArray, 0, toDecryptArray.Length)
aes.Clear()
Return decrypted
End Function
When I run the decryption code, I get the following error message
Filling is invalid and cannot be deleted.
Collegeman
source
share