iOS Question AES encryption with salt and iv

amer kasem

Member
Licensed User
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 :

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 :
Error decoding data as string.
this error occurred at this line :
b4x:
Return BytesToString(bData, 0, bData.Length, "UTF8")

Please any help is appreciated ..
 

amer kasem

Member
Licensed User
Thank you very much Erel,

I used the code I mentioned because I need to decrypt in php, If it can be accomplished with your code please let me know how to do it
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Maybe this helps:
 
Upvote 0

amer kasem

Member
Licensed User
Maybe this helps:
Thank you, but I tried the code and in b4a if I used B4XEncryption library with B4XCipher or Encryption library with Cipher I faced these errors :

B4XMainPage - 14484: Unknown member: encrypt2
B4XMainPage - 14483: Unknown member: option_pkcs7padding

so I couldn't even run the app
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Thank you, but I tried the code and in b4a if I used B4XEncryption library with B4XCipher or Encryption library with Cipher I faced these errors :

B4XMainPage - 14484: Unknown member: encrypt2
B4XMainPage - 14483: Unknown member: option_pkcs7padding

so I couldn't even run the app
Check the last post I shared in post #4.

In B4i, you should use iEncryption library.
In B4A, use Agraham's Encryption library.
 
Upvote 0

amer kasem

Member
Licensed User
Check the last post I shared in post #4.

In B4i, you should use iEncryption library.
In B4A, use Agraham's Encryption library.
I used this code in b4i with iEncryption library and altered it as per your advice in that post #4 :
b4i:
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, 0)
    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

but I`m still having an error :
Error occurred on line: 14552 (B4XMainPage)
Error encrypting data. Error number: -4310

this error hapened at line :
b4i:
Dim datas() As Byte = CI.Decrypt2(data, Key, "AES", Null, iOption)

Please advice what I am doing wrong, Thanks a lot
 
Upvote 0

amer kasem

Member
Licensed User
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
For B4A, everything is the same as KMatle's Code Snippet. Use KeyGenerator to generate a key.
B4X:
Dim datas() As Byte = CI.Encrypt(data, kg.Key, True)

Different is in B4i, use:
B4X:
Dim iOption As Int = Bit.Or(CI.OPTION_PKCS7Padding, 0)
Dim datas() As Byte = CI.Encrypt2(data, passB, "AES", IvB, iOption)
 
Upvote 0

amer kasem

Member
Licensed User
For B4A, everything is the same as KMatle's Code Snippet. Use KeyGenerator to generate a key.
B4X:
Dim datas() As Byte = CI.Encrypt(data, kg.Key, True)

Different is in B4i, use:
B4X:
Dim iOption As Int = Bit.Or(CI.OPTION_PKCS7Padding, 0)
Dim datas() As Byte = CI.Encrypt2(data, passB, "AES", IvB, iOption)
Thank you very much it worked for b4i, please can you guide me to KMatle's Code Snippet for b4a I couldn't find it šŸ˜Š
 
Upvote 0
Top