Still having trouble with encryption. I need to be able to encrypt/decrypt the same string in both B4A and VB.NET and get the same result.
I am using the same input string and key in the code below but getting two different results. I don't know enough about encryption to understand why.
B4A:
VB.NET
I am using the same input string and key in the code below but getting two different results. I don't know enough about encryption to understand why.
B4A:
B4X:
Sub AESEncrypt(dataToEncrypt As String, MyAESPW As String) As String
Dim SU As StringUtils
Dim kg As KeyGenerator
Dim C As Cipher
Dim md As MessageDigest
Dim encrypted() As Byte
kg.Initialize("AES")
kg.KeyFromBytes(md.GetMessageDigest(MyAESPW.GetBytes("UTF8"), "MD5"))
C.Initialize("AES/ECB/PKCS5Padding")
encrypted = C.Encrypt(dataToEncrypt.GetBytes("UTF8"), kg.Key, False)
Return SU.EncodeBase64(encrypted)
End Sub
VB.NET
B4X:
Public Function AES_Encrypt(ByVal input As String, ByVal pass As String) As String
Dim AES As New RijndaelManaged
Dim Hash_AES As New MD5CryptoServiceProvider
Dim encrypted As String = ""
Try
Dim hash(31) As Byte
Dim temp As Byte() = Hash_AES.ComputeHash(ASCIIEncoding.ASCII.GetBytes(pass))
Array.Copy(temp, 0, hash, 0, 16)
Array.Copy(temp, 0, hash, 15, 16)
AES.Key = hash
AES.Mode = CipherMode.ECB '' I have tried every option here
Dim DESEncrypter As ICryptoTransform = AES.CreateEncryptor
Dim Buffer As Byte() = ASCIIEncoding.ASCII.GetBytes(input)
encrypted = Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
Return encrypted
Catch ex As Exception
Debug.Print(ex.Message)
Return "Error"
End Try
End Function