Android Question [SOLVED] VB.NET and B4A AES Encryption Problem

mmieher

Active Member
Licensed User
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:
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
 

mmieher

Active Member
Licensed User
Erel, changing code as specified above produces a different result, but VB and B4A still do not match. The length of temp is 16.

VB: VciXMq2Bki7m6gM2lB4OZg==

B4A: cHY48ab60xfup5cDLxYxng==
 
Upvote 0
Top