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.
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
I missed this part earlier and set the key length to 16. It works quite well on both ends once the key length problem is fixed. Thank you @Hamied Abou Hulaikah very much for sharing the code!
This example is based on agrahams encryption library: Encryption Lib 1. Generate a 32 byte pw (just call the sub) and store it 2. Encrypt the data by calling AES_Encrypt. Return is a byte array or a base64 encoded string 3. Salt is a random value which is added at the beginning of the encrypted...