iOS Question B4A cipher equivalent in B4I

iCAB

Well-Known Member
Licensed User
Longtime User
Hi All

I have this code that I am using with b4A, I need the equivalent for b4i as I am communicating with a .Net Backend system that has a matching algorithm

B4X:
Sub AES_Encrypt(szInputToEncrypt As String, szKey As String, szIV As String) As String
 
    Dim baInputdata() As Byte
    Dim baEncryptedData(0) As Byte
    Dim szEncryptedData As String
 
    Dim baIV(ENC_KEY_LENGTH) As Byte
    Dim baKey(ENC_KEY_LENGTH) As Byte


    ENC_ConvertHexStringTo16ByteArray(szKey, baKey)
    ENC_ConvertHexStringTo16ByteArray(szIV, baIV)
 
    baInputdata = szInputToEncrypt.GetBytes("UTF8")
 
    Dim kg As KeyGenerator ,c As Cipher
    c.Initialize("AES/CBC/PKCS5Padding")

    ' CBC needs an initialisation vector
    c.InitialisationVector = baIV
    kg.Initialize("AES")
    kg.KeyFromBytes(baKey) 'Key

    baEncryptedData = c.Encrypt(baInputdata, kg.key, True)
    szEncryptedData = GeneralLib.StringUtils1.EncodeBase64(baEncryptedData)
 
    'ENC_MyMessagesLog(szEncryptedData)
 
    Return szEncryptedData

End Sub

B4X:
Sub AES_Decrypt(szInputToDecrypt As String, szKey As String, szIV As String) As String
    Dim baInputdata() As Byte
    Dim baDecryptedData(0) As Byte
    Dim szDeccryptedData As String

      Dim baIV(ENC_KEY_LENGTH) As Byte
    Dim baKey(ENC_KEY_LENGTH) As Byte

    ENC_ConvertHexStringTo16ByteArray(szKey, baKey)
    ENC_ConvertHexStringTo16ByteArray(szIV, baIV)

    baInputdata = GeneralLib.StringUtils1.DecodeBase64(szInputToDecrypt)
 
 
    Dim kg As KeyGenerator ,c As Cipher
    c.Initialize("AES/CBC/PKCS5Padding")

    ' CBC needs an initialisation vector
    c.InitialisationVector = baIV
    kg.Initialize("AES")
    kg.KeyFromBytes(baKey) 'Key

    baDecryptedData = c.Decrypt(baInputdata, kg.key, True)
    szDeccryptedData = GeneralLib.ByteConv.StringFromBytes(baDecryptedData, "UTF8")
 
    Return szDeccryptedData
 
End Sub

Thanks in advance
 

iCAB

Well-Known Member
Licensed User
Longtime User
Can you post your B4i code? Most of it will be the same.
Hi Erel

This is what I have done, but I am getting some exception.
Please note that I am no sure about keyGenerator. From what I read before I shouldn't need it as I am passing the key

B4X:
#if B4A
    Dim kg As KeyGenerator, c As Cipher
    c.Initialize("AES/CBC/PKCS5Padding")

    ' CBC needs an initialisation vector
    c.InitialisationVector = baIV
    kg.Initialize("AES")
    kg.KeyFromBytes(baKey) 'Key

   
    baEncryptedData = c.Encrypt(baInputdata, kg.key, True)
    szEncryptedData = GeneralLib.StringUtils1.EncodeBase64(baEncryptedData)
#else

    'Dim kg As KeyGenerator
    Dim c As Cipher

    ' CBC needs an initialisation vector
    'c.InitialisationVector = baIV
    'kg.Initialize("AES")
    'kg.KeyFromBytes(baKey) 'Key

'I am getting an exception here
    baEncryptedData = c.Encrypt2(baInputdata, baKey, "AES", baIV, 0 )
    szEncryptedData = GeneralLib.StringUtils1.EncodeBase64(baEncryptedData)


#end if
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
Can you post the error message?
Please see below

B4X:
Application_Start
Error occurred on line: 421 (HTTPSignature)
Alignment error
Stack Trace: (
  CoreFoundation       <redacted> + 150
  libobjc.A.dylib      objc_exception_throw + 38
  CoreFoundation       <redacted> + 0
  B4i Example          -[B4ICipher crypt::::::] + 706
  B4i Example          -[B4ICipher Encrypt2:::::] + 74
  CoreFoundation       <redacted> + 68
  CoreFoundation       <redacted> + 292
  B4i Example          +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1786
  B4i Example          -[B4IShell runMethod:] + 574
  B4i Example          -[B4IShell raiseEventImpl:method:args::] + 1998
B4i Example          -[B4IShellBI raiseEvent:event:params:] + 1442
B4i Example          __33-[B4I raiseUIEvent:event:params:]_block_invoke + 74
libdispatch.dylib    <redacted> + 10
libdispatch.dylib    <redacted> + 22
libdispatch.dylib    _dispatch_main_queue_callback_4CF + 1532
CoreFoundation       <redacted> + 8
CoreFoundation       <redacted> + 1590
CoreFoundation       CFRunLoopRunSpecific + 516
CoreFoundation       CFRunLoopRunInMode + 108
GraphicsServices     GSEventRunModal + 160
UIKit                UIApplicationMain + 144
B4i Example          main + 108
libdyld.dylib        <redacted> + 2
)
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
The above works, however when I am trying to decrypt using the call below

B4X:
c.Decrypt2(baInputdata, baKey, "AES", baIV, c.OPTION_PKCS7Padding  )

I am getting extra 0`s appended to the original string
 
Last edited:
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
You will need to manually remove them.
B4X:
Sub Trim0(s As String) As String
   Return Regex.Replace("0+$", s, "")
End Sub

The above code didn't work for me. The code below did the job

B4X:
Sub Trim0(s As String) As String
       Return  Regex.Replace("\u0000{4,}", s, "")
End Sub

Please double check

Thank you
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
Why and how are you converting the bytes to a string?
I am attaching a stripped down version that you can add to any project.
Please have a look

Thank you
 

Attachments

  • AES_Test.bas
    2 KB · Views: 344
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This code is incorrect. You shouldn't convert raw bytes to string. The only proper way is to encode the bytes with StringUtils.EncodeBase64.

You can use this code to trim the array of bytes:
B4X:
Private Sub Trim0(arr() As Byte) As Byte()
   For i = arr.Length -1 To 0 Step -1
     If arr(i) <> 0 Then
       If i < arr.Length - 1 Then
         Dim newarr(i + 1) As Byte
         Dim bc As ByteConverter
         bc.ArrayCopy(arr, 0, newarr, 0, i + 1)
         Return newarr
       Else
         Exit
       End If
     End If
   Next
   Return arr
End Sub
 
Upvote 0
Top