Android Question AES Encrypt

bashka_abdyli

Member
Licensed User
Longtime User
Hello,

Can someone provide a conversion of this .NET Function in B4A. We cannot change the Function in .NET, we need equivalent in B4A.

.NET:
  Public Function AES_Encrypt(ByVal input As String, ByVal pass As String) As String
        Dim AES As New System.Security.Cryptography.RijndaelManaged
        Dim Hash_AES As New System.Security.Cryptography.MD5CryptoServiceProvider
        Dim encrypted As String = ""
        Try
            Dim hash(31) As Byte
            Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))
            Array.Copy(temp, 0, hash, 0, 16)
            Array.Copy(temp, 0, hash, 15, 16)
            AES.Key = hash
            AES.Mode = Security.Cryptography.CipherMode.ECB
            Dim DESEncrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateEncryptor
            Dim Buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(input)
            encrypted = Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
            Return encrypted
        Catch ex As Exception
        End Try
    End Function
 

Num3

Active Member
Licensed User
Longtime User
I use these procedures in B4A/B4J, i copy pasted from one of my projects, please note I output a base64 result :


B4X:
'Requires Encryptation and StringUtils libraries
'These subs where compiles from code found on the forums
'Credit to the authors

Sub test
    Dim public_key As String = Generate_Token
    Dim msg As String = "I am a message"
    Dim private_key As String = Generate_Token  
   
    Dim enc As String = AES_Encrypt(msg, public_key, private_key)
    Log(enc)
    Log(AES_Decrypt(enc, public_key, private_key))  
   
End Sub

Public Sub Generate_Token() As String

    Dim stp As String = "0000000000000000" '16 CHARS / change here according to needs

    Dim sb As String = ""



    For Index = 0 To stp.Length-1

        If stp.CharAt(Index)="0" Then

            sb=sb & "0123456789abcdefghizklmnopqrstuvwzABCDEFGHIJKLMNOPQRSTUVWZ".CharAt(Rnd(0, 57))

        Else

            sb=sb & stp.CharAt(Index)

        End If

    Next

    Return sb

End Sub





private Sub AES_Encrypt(input As String, token As String, pass As String) As String



    If token.Length <> 16 Or pass.Length <> 16 Then

        Log($"AES ERROR: ${input}/${token}"$)

        Return ""

    End If



    Dim su As StringUtils

    Dim inputB() As Byte = input.GetBytes("UTF8")

    Dim passB() As Byte = pass.GetBytes("UTF8")

    Dim IVb() As Byte = token.GetBytes("UTF8")

 

    Dim kg As KeyGenerator

    Dim C As Cipher

 

    kg.Initialize("AES")

    kg.KeyFromBytes(passB)

 

    C.Initialize("AES/CBC/PKCS5Padding")

    C.InitialisationVector = IVb

 

    Dim datas() As Byte = C.Encrypt(inputB, kg.Key, True)

 

    Return su.EncodeBase64(datas) 'Return Base64 Encoded string

End Sub



private Sub AES_Decrypt(input As String, token As String, pass As String) As String

   

    Dim su As StringUtils

    Dim inputB() As Byte = su.DecodeBase64(input) ' from String utilities

    Dim passB() As Byte = pass.GetBytes("UTF8")

    Dim IVb() As Byte = token.GetBytes("UTF8")

 

    Dim kg As KeyGenerator

    Dim C As Cipher

 

    kg.Initialize("AES")

    kg.KeyFromBytes(passB)

 

    C.Initialize("AES/CBC/PKCS5Padding")

    C.InitialisationVector = IVb

 

    Try

        Dim datas() As Byte = C.Decrypt(inputB, kg.Key, True)

        Return BytesToString(datas, 0, datas.Length, "UTF8")

    Catch

        Log($"AES: ${LastException}"$)

        Return ""

    End Try    

End Sub
 
Upvote 0

bashka_abdyli

Member
Licensed User
Longtime User
Hi thanks for the code provided, however in .NET I send only 2 parameters, here I need 3

In .NET I send AES_Encrypt("test string", "Password")
 
Upvote 0

PassionDEV

Well-Known Member
Licensed User
You can look over this thread it would help you

 
Upvote 0
Top