I used the code in this link :
https://www.b4x.com/android/forum/t...s-with-all-platforms-like-php-net-etc.127811/
but it does not work in b4i because b4i don't have KeyGenerator as Erel said here :
https://www.b4x.com/android/forum/threads/solved-b4i-keygenerator-problem.102279/post-642002
so I used this code in b4a and b4i to encrypt and decrypt :
the b4a code is working fine but I can't get the b4i code to work,
the error I got when I decrypt is :
Please any help is appreciated ..
https://www.b4x.com/android/forum/t...s-with-all-platforms-like-php-net-etc.127811/
but it does not work in b4i because b4i don't have KeyGenerator as Erel said here :
https://www.b4x.com/android/forum/threads/solved-b4i-keygenerator-problem.102279/post-642002
so I used this code in b4a and b4i to encrypt and decrypt :
B4X:
AESPW=SHA256Hash32String("password")
text = "text"
encrypted_text = AES_Encrypt(text.GetBytes("UTF8"), AESPW,True)
AES_Decrypt(SU.DecodeBase64(encrypted_text, AESPW,True))
Public Sub AES_Encrypt(data() As Byte, passB() As Byte, ReturnB64String As Boolean) As Object
#if b4a
Dim SaltB() As Byte = GenerateBytes(32)
Dim IVb() As Byte = GenerateBytes(16)
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(data, kg.Key, True)
Dim SaltIVMessage(SaltB.Length + datas.Length + IVb.Length) As Byte = AddSaltIVMessage(SaltB,IVb,datas)
If ReturnB64String Then
Return SU1.EncodeBase64(SaltIVMessage)
Else
Return SaltIVMessage
End If
#End If
#if b4i
Dim SU As StringUtils
Dim md As MessageDigest
Dim C As Cipher
Dim bMD() As Byte = md.GetMessageDigest(passB,"MD5")
Dim iOption As Int = Bit.Or(C.OPTION_PKCS7Padding,C.OPTION_ECBMode)
Dim bData() As Byte = C.Encrypt2(data,bMD,"AES",Null,iOption)
Return SU.EncodeBase64(bData)
#End If
End Sub
Public Sub AES_Decrypt(data() As Byte, passB() As Byte, ReturnString As Boolean) As Object
#if b4a
Dim m As Map = SplitSaltIVMessage(data)
Dim IVb() As Byte = m.Get("iv")
Dim MessageB() As Byte = m.Get("message")
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.Decrypt(MessageB, kg.Key, True)
If ReturnString Then
Return BytesToString(datas, 0, datas.Length, "UTF8")
Else
Return datas
End If
#End If
#if b4i
Dim m As Map = SplitSaltIVMessage(data)
Dim IVb() As Byte = m.Get("iv")
Dim MessageB() As Byte = m.Get("message")
Dim SU As StringUtils
Dim MD As MessageDigest
Dim C As Cipher
Dim AES_PW As String = "password"
Dim bMD() As Byte = MD.GetMessageDigest(AES_PW.GetBytes("UTF8"), "MD5")
Dim iOption As Int = Bit.Or(C.OPTION_PKCS7Padding, C.OPTION_ECBMode)
Dim bData() As Byte = C.Decrypt2(MessageB, bMD, "AES", IVb, iOption)
Return BytesToString(bData, 0, bData.Length, "UTF8")
#End If
End Sub
the b4a code is working fine but I can't get the b4i code to work,
the error I got when I decrypt is :
this error occurred at this line :Error decoding data as string.
b4x:
Return BytesToString(bData, 0, bData.Length, "UTF8")
Please any help is appreciated ..