Android Question Looking for a simple encryption solution between B4A and PHP

toby

Well-Known Member
Licensed User
Longtime User
I need a simple solution to encrypt some user id in B4A app and decrypt it on the server using PHP, and vice versa.

Earlier I was checking on RSA which might be a little overkill.

Any suggestion is welcome.

TIA
 

Cableguy

Expert
Licensed User
Longtime User
The link is about b4x while my need is between b4a and php.
Then again you are in a B4X forum... PHP is outside the scope of this forum.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
The communication between b4a and php is not, I'm afraid.
Indeed.
If you have an absolute need to use php, then you first need to decide on wich encryption algorithms to use, and then apply them to your b4a project.
All major encryption algorithms are supported by the use or the encryption library.
Still, if you can trade php for b4j based solution? Your code will gain in fluidity.
 
Upvote 0

Hamied Abou Hulaikah

Well-Known Member
Licensed User
Longtime User
This is my favorite:
B4X:
Public Sub AES_Encrypt(data As String, secretkey As String) As String
 
    Dim SU As StringUtils
 
    Dim iv As String= RandomIVString(16)
    Dim IVb() As Byte = iv.GetBytes("UTF8")
    Dim kg As KeyGenerator
    Dim C As Cipher
    Dim SU As StringUtils
 
 
    kg.Initialize("AES")
    kg.KeyFromBytes(secretkey.GetBytes("UTF8"))
 
    C.Initialize("AES/CBC/PKCS5Padding")
    C.InitialisationVector = IVb
 
    Dim datas() As Byte = C.Encrypt(data.GetBytes("UTF8"), kg.Key, True)
 
    Return iv & SU.EncodeBase64(datas)
 
End Sub


Public Sub AES_Decrypt(data As String,  secretkey As String) As String
 
    Dim SU As StringUtils
 
    Dim iv As String = data.SubString2(0,16)
    Dim msg As String=data.Replace(iv,"")
 
    Dim IVb() As Byte = iv.GetBytes("UTF8") 
    Dim kg As KeyGenerator
    Dim C As Cipher
 
    kg.Initialize("AES")
    kg.KeyFromBytes(secretkey.GetBytes("UTF8"))
 
    C.Initialize("AES/CBC/PKCS5Padding")
    C.InitialisationVector = IVb
 
    Dim datas() As Byte = C.Decrypt(SU.DecodeBase64(msg), kg.Key, True)
 
    Dim SU As StringUtils
    Return BytesToString(datas,0,datas.Length,"UTF8")
 
End Sub

Private Sub RandomIVString(length As Int) As String
    Dim abc As String = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
    Dim randomstr As String = ""
    For i = 0 To length - 1
        randomstr = randomstr & (abc.CharAt(Rnd(0,abc.Length)))
    Next
    Return randomstr
 
End Sub

PHP:
function EncryptString($key,$plainText){
    $bytes = random_bytes(8);
    $iv=bin2hex($bytes);
    $encryptedData = openssl_encrypt($plainText, 'AES-256-CBC', $key, $options=0, $iv);
    return $iv.$encryptedData;
}

function DecryptString($key,$encText){
    $iv=substr($encText,0,16);
    $decryptedData = openssl_decrypt(substr($encText,16), 'AES-256-CBC', $key, $options=0, $iv);
    return $decryptedData;
}

- Secret key should be 32 characters.
- Use Encryption library.
 
Last edited:
Upvote 1

Addo

Well-Known Member
Licensed User
Longtime User
The code above originally posted here by @KMatle at the following thread with extra layer of adding salt to the encrypted message
 
Upvote 0
Top