iOS Question [SOLVED] [B4X] Decode AES encoded STRING

Mike1970

Well-Known Member
Licensed User
Longtime User
Hi everyone, I need to crypt a string with a tool (not the app) and then be able to decode it back from the app.
example tool: https://encode-decode.com/aes256-encrypt-online/

1. Crypt a text with the above tool
2. Copy the output string (e.g. Kj7ytYpr5Z/g93JaBEUo9Q==)
3. Use that string in the APP to decode it back to its original form

Crypted text = "test"
Password = "pwd"
Algo: AES256

It is not important that is AES256 or AES128 etc... my goal is to be able to decode an encrypted string.


I already saw this thread
but if I try something like:
B4X:
Log(DecryptText("Kj7ytYpr5Z/g93JaBEUo9Q==".GetBytes("UTF8"), "pwd"))
it return empty string

Thanks in advance.
 
Solution
I'm using this two websites to generate the AES256 encrypted text (two because in this way I can compare them)
Starting from the code suggested by @aeric in post #7 I changed one single thing, that was not clear to me: why do we need to hash the passwords bytes?
Infact the only edit i made is that one... so this is the final code:


Encrypter

B4X:
Sub Encrypt(data() As Byte, passB() As Byte, EncodeToBase64 As Boolean) As Object
    #if B4A or B4J
    Dim CI As B4XCipher
    #else if B4i
    Dim CI As Cipher
    #end if
    Dim SU As StringUtils

    'Dim MD As...

aeric

Expert
Licensed User
Longtime User
Perhaps you can try this library
 
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
Oh thanks.. but can I compile it with iOS? or I need the local builder?
Because at the moment I use the hosted builder
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Try this:

using iEncryption and StringUtils
B4X:
Dim hash(32) As Byte = strPassword.GetBytes("UTF8")
Return AES_Decrypt(StringUtils.DecodeBase64(strCipherText), hash, True)
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private CI As Cipher
    Private SU As StringUtils
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    Dim Message As String = "Hello B4i Cipher"
    Dim Password As String = "Password"
    Dim EncryptedText As String = Encrypt(Message.GetBytes("UTF8"), Password.GetBytes("UTF8"), True)
    Log(EncryptedText)
    Dim DecryptedText As String = Decrypt(SU.DecodeBase64(EncryptedText), Password.GetBytes("UTF8"), True)
    Log(DecryptedText)
End Sub

Sub Encrypt(data() As Byte, passB() As Byte, EncodeToBase64 As Boolean) As Object
    Dim MD As MessageDigest
    Dim Key() As Byte = MD.GetMessageDigest(passB, "MD5")
    Dim iOption As Int = Bit.Or(CI.OPTION_PKCS7Padding, CI.OPTION_ECBMode)
    Dim datas() As Byte = CI.Encrypt2(data, Key, "AES", Null, iOption)
    If EncodeToBase64 Then
        Return SU.EncodeBase64(datas)
    Else
        Return datas
    End If
End Sub

Sub Decrypt(data() As Byte, passB() As Byte, DecodeToString As Boolean) As Object
    Dim MD As MessageDigest
    Dim Key() As Byte = MD.GetMessageDigest(passB, "MD5")
    Dim iOption As Int = Bit.Or(CI.OPTION_PKCS7Padding, CI.OPTION_ECBMode)
    Dim datas() As Byte = CI.Decrypt2(data, Key, "AES", Null, iOption)
    If DecodeToString Then
        Return BytesToString(datas, 0, datas.Length, "UTF8")
    Else
        Return datas
    End If
End Sub
 
Upvote 1

aeric

Expert
Licensed User
Longtime User
I can't find any online tool that compatible with B4i code above. Anyone want to wrap a new library?
Found a github repo but convert to B4i library is beyond my skills.
 
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
thank you for your effort!

Multiple aes256 decryptor/encryptor gives me the same results... but the encrytper of B4i is the only one that gives me a different one


EDIT:
I think i solved.
Now i post another reply with the solution
 
Last edited:
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
I'm using this two websites to generate the AES256 encrypted text (two because in this way I can compare them)
Starting from the code suggested by @aeric in post #7 I changed one single thing, that was not clear to me: why do we need to hash the passwords bytes?
Infact the only edit i made is that one... so this is the final code:


Encrypter

B4X:
Sub Encrypt(data() As Byte, passB() As Byte, EncodeToBase64 As Boolean) As Object
    #if B4A or B4J
    Dim CI As B4XCipher
    #else if B4i
    Dim CI As Cipher
    #end if
    Dim SU As StringUtils

    'Dim MD As MessageDigest
    'Dim Key() As Byte = MD.GetMessageDigest(passB, "MD5")
    Dim Key() As Byte = passB
    Dim iOption As Int = Bit.Or(CI.OPTION_PKCS7Padding, CI.OPTION_ECBMode)
    Dim datas() As Byte = CI.Encrypt2(data, Key, "AES", Null, iOption)
    If EncodeToBase64 Then
        Return SU.EncodeBase64(datas)
    Else
        Return datas
    End If
End Sub


Decrypter
B4X:
Sub Decrypt(data() As Byte, passB() As Byte, DecodeToString As Boolean) As Object
    #if B4A or B4J
    Dim CI As B4XCipher
    #else if B4i
    Dim CI As Cipher
    #end if

    'Dim MD As MessageDigest
    'Dim Key() As Byte = MD.GetMessageDigest(passB, "MD5")
    Dim Key() As Byte = passB
    Dim iOption As Int = Bit.Or(CI.OPTION_PKCS7Padding, CI.OPTION_ECBMode)
    Dim datas() As Byte = CI.Decrypt2(data, Key, "AES", Null, iOption)
    If DecodeToString Then
        Return BytesToString(datas, 0, datas.Length, "UTF8")
    Else
        Return datas
    End If
End Sub


Usage (both in B4i)
B4X:
    Dim Message As String = "Test"
    Dim Password As String = "A?D(G+KbPeSgVkYp3s6v9y$B&E)H@McQ"
  
    Dim EncryptedText As String = Encrypt(Message.GetBytes("UTF8"), Password.GetBytes("UTF8"), True)
    Log(EncryptedText)    '-> produces Y17gylMNEFZbkPQJ3nVm6g==
  
    Dim DecryptedText As String = Decrypt(SU.DecodeBase64(EncryptedText), Password.GetBytes("UTF8"), True)
    Log(DecryptedText)


Usage (encryption made using a website, decryption made using B4i)
B4X:
Dim Password As String = "A?D(G+KbPeSgVkYp3s6v9y$B&E)H@McQ"
Dim EncryptedText As String = "Y17gylMNEFZbkPQJ3nVm6g=="    'Output of website encrypter
  
Dim DecryptedText As String = Decrypt(SU.DecodeBase64(EncryptedText), Password.GetBytes("UTF8"), True)
Log(DecryptedText)
 
Last edited:
Upvote 0
Solution

Swissmade

Well-Known Member
Licensed User
Longtime User
I have solve this in a class Library you need for this is
IEncryption
1 Public function to do both
Hope it helps if you not solve it yet

Aes256 for IOS:
Public Sub SetAES(Mess As String, Key As String, Encrypt As Boolean) As String
    Try
        Dim passGen As Cipher
        Dim Conv As ByteConverter
        Dim strReturn As String
        If Encrypt = True Then
            Dim Data() As Byte = passGen.Encrypt(Mess.GetBytes("UTF8"), Key)
'        Log(BytesToString(Data, 0, Data.Length, "UTF8"))
            strReturn = Conv.HexFromBytes(Data)
            'test    Decrypt
            Dim Bytes() As Byte = passGen.Decrypt(Conv.HexToBytes(strReturn), Key)
            Dim tmpPass As String = BytesToString(Bytes, 0, Bytes.Length, "UTF8")
            If tmpPass = Mess Then
           

                ShowLog("Password SetAES OK", True)
            End If
        Else
            Dim Bytes() As Byte = passGen.Decrypt(Conv.HexToBytes(Mess), Key)
            Dim tmpPass As String = BytesToString(Bytes, 0, Bytes.Length, "UTF8")
            strReturn = tmpPass
        End If
        Return strReturn
    Catch
        ShowLog("Error: getting Password or Key " & LastException.Message, True)
        Return "Wrong Key"
    End Try
End Sub
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
why do we need to hash the passwords bytes?
Maybe I need to use MD5 to get a 32 length.
This is specific to my app due to historical reason. You can skip that step.

Today I finally found the mystery which bothered me for "2000 years".
Dim iOption As Int = Bit.Or(CI.OPTION_PKCS7Padding, CI.OPTION_ECBMode)

In my B4A and B4J app, I am using CBC mode.
B4X:
Dim CI As Cipher

CI.Initialize("AES/CBC/PKCS5Padding")
To use AES encryption in B4i which is compatible with B4A, B4J and PHP OpenSSL, what I need to do is set the ECBMode to 0.
B4X:
Dim iOption As Int = Bit.Or(CI.OPTION_PKCS7Padding, 0)

If IV is not used (Null), you can use ECB mode. I think it was supported in older PHP.
If IV is used, there is an error requires you to use CBC mode.
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…