Android Question AES Encryption Equivalent in B4A

Jakes72

Active Member
Licensed User
Longtime User
Hi Experts / Awesome people,

If someone could please help me.
I am looking for the equivalent code in B4A for this VB.Net code:

B4X:
 Public Function EncryptAES(ByVal stringToEncrypt As String, ByVal sEncryptionKey As String) As String

        Dim EncryptionKey As String = sEncryptionKey

        Dim clearBytes As Byte() = Encoding.ASCII.GetBytes(stringToEncrypt)

        Using encryptor As Aes = Aes.Create()

            Dim pdb As New Rfc2898DeriveBytes(EncryptionKey, New Byte() {211, 5, 233, 24, 55, 166, 7, 88, 2, 1, 4, 5, 6, 7, 8, 0})

            encryptor.Key = pdb.GetBytes(32)
            encryptor.IV = pdb.GetBytes(16)

            Using ms As New MemoryStream()
                Using cs As New CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write)
                    cs.Write(clearBytes, 0, clearBytes.Length)
                    cs.Close()
                End Using
                stringToEncrypt = Convert.ToBase64String(ms.ToArray())
            End Using

        End Using

        Return stringToEncrypt

End Function

Thank you in advance for your time!

Regards,
Jacques.
 

Jakes72

Active Member
Licensed User
Longtime User
Hi Mcqueccu,

No it is not different, but I have tried this and I do not get the same result:

B4X:
Sub EncryptAES(strDataToEncrypt As String, strKey As String) As String

    Dim kg As KeyGenerator
    Dim c As Cipher
    Dim B64 As Base64
    Dim bconv As ByteConverter

    Dim data(0) As Byte
    Dim iv(0) As Byte
    iv = Array As Byte(211, 5, 233, 24, 55, 166, 7, 88, 2, 1, 4, 5, 6, 7 ,8, 0) ' 16 bytes for AES
     
    c.Initialize("AES/CBC/PKCS5Padding")
    c.InitialisationVector = iv
    kg.Initialize("AES")
 
    kg.KeyFromBytes(bconv.StringToBytes(strKey,"ASCII"))
    data = bconv.StringToBytes(strDataToEncrypt, "ASCII")
    data = c.Encrypt(data, kg.Key, True)

    Return B64.EncodeBtoS(data, 0, data.Length)
 
End Sub

I have basically no knowledge of this so I am kinda 'winging' it here and just trying random stuff, can you possibly see what I am doing wrong?
 
Upvote 0

mcqueccu

Well-Known Member
Licensed User
Try the one in this thread and possibly the solution given

 
Upvote 0

Jakes72

Active Member
Licensed User
Longtime User
Thank you Sir for that link you are awesome!

I did not have the Decrypt methods for .NET & B4A, but I managed to figure them out with trial and error.

For anyone else battling with this and needing to pass Encrypted data between your Apps & and VB.Net program, here is the complete code:

VB.NET:
B4X:
Public Function EncryptAES(ByVal stringToEncrypt As String, ByVal sEncryptionKey As String) As String

        Dim AES As New RijndaelManaged
        Dim Hash_AES As New MD5CryptoServiceProvider
        Dim encrypted As String = ""

        Try

            Dim temp As Byte() = Hash_AES.ComputeHash(ASCIIEncoding.ASCII.GetBytes(sEncryptionKey))
            AES.Key = temp
            AES.Mode = CipherMode.ECB

            Dim AESEncrypter As ICryptoTransform = AES.CreateEncryptor

            Dim Buffer As Byte() = ASCIIEncoding.ASCII.GetBytes(stringToEncrypt)
            encrypted = Convert.ToBase64String(AESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
            Return encrypted

        Catch ex As Exception
        End Try

    End Function


    Public Function DecryptAES(ByVal stringToDecrypt As String, ByVal sEncryptionKey As String) As String

        Dim AES As New RijndaelManaged
        Dim Hash_AES As New MD5CryptoServiceProvider
        Dim Decrypted As String = ""

        stringToDecrypt = stringToDecrypt.Replace(" ", "+")

        Try

            Dim temp As Byte() = Hash_AES.ComputeHash(ASCIIEncoding.ASCII.GetBytes(sEncryptionKey))
            AES.Key = temp
            AES.Mode = CipherMode.ECB

            Dim AESDecrypter As ICryptoTransform = AES.CreateDecryptor

            Dim Buffer As Byte() = Convert.FromBase64String(stringToDecrypt)
            Decrypted = System.Text.Encoding.ASCII.GetString(AESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
            Return Decrypted

        Catch ex As Exception
        End Try


    End Function


B4A:
B4X:
Sub EncryptAES(strDataToEncrypt As String, strKey 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(strKey.GetBytes("UTF8"), "MD5"))
    
    C.Initialize("AES/ECB/PKCS5Padding")
    encrypted = C.Encrypt(strDataToEncrypt.GetBytes("UTF8"), kg.Key, False)
    Return SU.EncodeBase64(encrypted)

End Sub


Sub DecryptAES(strDataToDecrypt As String, strKey As String) As String
  
    Dim SU As StringUtils
    Dim kg As KeyGenerator
    Dim C As Cipher
    Dim md As MessageDigest
    Dim Decrypted() As Byte
    kg.Initialize("AES")
    kg.KeyFromBytes(md.GetMessageDigest(strKey.GetBytes("UTF8"), "MD5"))
    
    C.Initialize("AES/ECB/PKCS5Padding")
  
    Dim Bytes() As Byte = SU.DecodeBase64(strDataToDecrypt)
    Decrypted = C.Decrypt(Bytes, kg.Key, False)
    Return BytesToString(Decrypted, 0, Decrypted.Length, "UTF8")

End Sub
 
Last edited:
Upvote 0
Top