iOS Question Encrypt/Decrypt conversion from B4A to B4I

ThePuiu

Active Member
Licensed User
Longtime User
Hi, I need help to convert 2 routines from B4A to B4I:
B4X:
Sub Encrypt(dataToEncrypt As String ) As String

   Dim kg As KeyGenerator
   Dim c As Cipher
   Dim B64 As Base64
   Dim bconv As ByteConverter

   Dim data(0) As Byte
   Dim iv(0) As Byte
   iv = Array As Byte(211, 5, 233, 24, 55, 166, 7, 88)    
   c.Initialize("DESEDE/CBC/PKCS5Padding")   
   c.InitialisationVector = iv
   kg.Initialize("DESEDE")  
   kg.KeyFromBytes(bconv.StringToBytes("5244067896183996","ASCII"))
   data = bconv.StringToBytes(dataToEncrypt, "ASCII")     
   data = c.Encrypt(data, kg.Key, True)           
   Return B64.EncodeBtoS(data, 0, data.Length)  
End Sub

Sub Decrypt(encryptedData As String ) As String
   Dim kg As KeyGenerator
   Dim c As Cipher
   Dim B64 As Base64
   Dim bconv As ByteConverter

   Dim data(0) As Byte
   Dim iv(0) As Byte
   iv = Array As Byte(211, 5, 233, 24, 55, 166, 7, 88)    
   c.Initialize("DESEDE/CBC/PKCS5Padding")   
   c.InitialisationVector = iv
   kg.Initialize("DESEDE")   
   kg.KeyFromBytes(bconv.StringToBytes("5244067896183996","ASCII"))    
   data = B64.DecodeStoB(encryptedData)
   data = c.Decrypt(data, kg.Key, True)   
   Return bconv.StringFromBytes(data, "ASCII")
End Sub

I try to inspire from this code:
B4X:
Public Sub Encrypt(dataToEncrypt As String) As String
    Dim PW As String = "5244067896183996"
    Dim SU As StringUtils
    Dim C As Cipher
    Dim iOption As Int = Bit.Or(C.OPTION_PKCS7Padding,C.OPTION_ECBMode)
    Dim bData() As Byte = C.Encrypt2(dataToEncrypt.GetBytes("ASCII"),PW.GetBytes("ASCII"),"AES",Null,iOption)
    Return SU.EncodeBase64(bData) 
End Sub
but the result is not the same.
I need to get routines that return exactly the same result because I can't modify both the B4A and web application that already work...
 

ThePuiu

Active Member
Licensed User
Longtime User
I read on forum that I can not use B4XEncryption in connection with a web service written in c # ...

If I try to use ByteConverter and Encryption I get an error message: can't find the byteconverter.h and encryption.h files (I copied the B4A libraries probably are not compatible)
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I read on forum that I can not use B4XEncryption in connection with a web service written in c # ...
You will need to implement the same protocol in C#. Maybe someone already did it. Search the forum.

If I try to use ByteConverter and Encryption I get an error message: can't find the byteconverter.h and encryption.h files (I copied the B4A libraries probably are not compatible)
Not sure what you are doing. You should use iEncryption library on B4i and B4XEncryption on B4A. ByteConverter is part of iRandomAccessFile. You cannot copy B4A libraries to B4i (except of b4xlibs).
 
Upvote 0
Top