iOS Question Conversion encryption routine from B4A to B4I

falbertini

Member
Licensed User
Longtime User
Hello,
I've got an Android app that comunicates with an AspNet web service; I am creating the same app for IOS with B4i. But I am not able to convert the encryption routine I used from B4a to B4i..

Here is the working well b4a routine:
B4X:
Sub Encrypt(dataToEncrypt As String, key 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(1, 2, 3, 4, 5, 6, 7, 8) 'not the original iV
       
    c.Initialize("DESEDE/CBC/PKCS5Padding")    
    c.InitialisationVector = iv
    kg.Initialize("DESEDE")
   
    kg.KeyFromBytes(bconv.StringToBytes(key,"UTF8"))
    data = bconv.StringToBytes(dataToEncrypt, "UTF8")       
    data = c.Encrypt(data, kg.key, True)               

    Return B64.EncodeBtoS(data, 0, data.Length)
End Sub

I have converted it in B4i in this way:
B4X:
Sub Encrypt(dataToEncrypt As String, key As String) As String
    Dim c As Cipher
    Dim bconv As ByteConverter
    Dim opzioni As Int
    Dim data(0) As Byte
    Dim data2(0) As Byte
    Dim iv(0) As Byte
    iv = Array As Byte(1, 2, 3, 4, 5, 6, 7, 8) 'not the original iV
    Dim kgBytes() As Byte
    Dim pw() As Byte
    Dim output As String
    pw = bconv.StringToBytes(key, "UTF8")
    kgBytes = c.GenerateKey(pw, "SHA-1", iv, 1)
   
    data = bconv.StringToBytes(dataToEncrypt, "UTF8")    
    opzioni = Bit.OR(c.OPTION_ECBMode, c.OPTION_PKCS7Padding)
    data2 = c.Encrypt2(data, kgBytes, "3DES", iv, opzioni) 
    output = bconv.StringFromBytes(data2, "UTF8")
    Return output
End Sub

But in this line I always get error:
output = bconv.StringFromBytes(data2, "UTF8")

Using another encoding works fine, but then my webservice is not able to decrypt the content; for example:
output = bconv.StringFromBytes(data2, "ISO-8859-1")

What am I doing wrong?
Thanks
 

stevel05

Expert
Licensed User
Longtime User
Shouldn't it be "UTF-8"?
 
Upvote 0

falbertini

Member
Licensed User
Longtime User
I have modified my routine with
kgBytes = key.GetBytes("UTF8")

But I get the same error when calling this:
output = bconv.StringFromBytes(data2, "UTF8")

How can I convert it to Base64 like I was doing in B4a ?
(B64.EncodeBtoS(data, 0, data.Length))

This is my complete routine:

B4X:
Sub Encrypt(dataToEncrypt As String, key As String) As String
    Dim c As Cipher
    Dim bconv As ByteConverter
    Dim opzioni As Int
    Dim data(0) As Byte
    Dim data2(0) As Byte
    Dim iv(0) As Byte
    iv = Array As Byte(1, 2, 3, 4, 5, 6, 7, 8) 'not the original iV
    Dim kgBytes() As Byte
    'Dim pw() As Byte
    Dim output As String
    'pw = bconv.StringToBytes(key, "UTF8")
    kgBytes = key.GetBytes("UTF8")
  
    data = bconv.StringToBytes(dataToEncrypt, "UTF8")   
    opzioni = Bit.OR(c.OPTION_ECBMode, c.OPTION_PKCS7Padding)
    data2 = c.Encrypt2(data, kgBytes, "3DES", iv, opzioni)
    output = bconv.StringFromBytes(data2, "UTF8")
    Return output
End Sub

Thanks
 
Last edited:
Upvote 0

falbertini

Member
Licensed User
Longtime User
Base64 conversion now works, but the result of the encryption is different from the result with the B4a android.
Attached you can find two simple app, one with B4a (the working well one) and one with B4i..
Strange is that the encryption of B4i gets different result in different time.. clicking generate, waiting one second and reclicking generate gives different result ..
 

Attachments

  • Test sample.zip
    413.4 KB · Views: 331
Upvote 0

falbertini

Member
Licensed User
Longtime User
Thanks for your answer..
I have downloaded and installed the new B4XEncryption library; but this seems a B4a and not a B4i library..
My problem is that when I send data encrypted from B4i to my web service, decrypting the data from the web service raises exception and it is different from what is generated from my existing B4i apps
 
Upvote 0

falbertini

Member
Licensed User
Longtime User
I have downloaded and tried the new Iencryption 1.0.1 but with no luck.
The generated string is different from the previous version of B4a and I am not able to decrypt it in my Vb.Net web service

Here is the routine I use to decrypt in Vb.Net what arrives from B4i / B4a apps, that now does not work:

B4X:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        MsgBox(Decrypt_B4a("xDkUBnMrpsm2D+tqor1nUS5mapQGmIVU", "abcdefghijklmnop"))
    End Sub

    Public Function Decrypt_B4a(ByVal encryptedData As String, ByVal key As String) As String
        Dim buffer As Byte() = Convert.FromBase64String(encryptedData)
        Dim des As New System.Security.Cryptography.TripleDESCryptoServiceProvider
        des.IV = New Byte() {100, 101, 102, 103, 104, 105, 106, 107}
        des.Key = System.Text.ASCIIEncoding.UTF8.GetBytes(key)
        Dim result As String = System.Text.Encoding.UTF8.GetString(des.CreateDecryptor().TransformFinalBlock(buffer, 0, buffer.Length))
        des.Clear()
        Return result
    End Function

Attached you can find a little Vb.Net example that tries to decrypt the B4i encripted string (and the previous B4i and B4a example that crypts)
 

Attachments

  • Example.zip
    486.2 KB · Views: 327
Upvote 0

falbertini

Member
Licensed User
Longtime User
Thanks, but I cannot convert the web service to B4J because is innested in a web site in AspNet, and using a lot of calls from a VbNet dll shared between multiple projects
 
Upvote 0
Top