iOS Question Decrypt a string encrypted on ios

Alex_197

Well-Known Member
Licensed User
Longtime User
Hi, I'm working on conversion my b4a app to b4i and have encrypt / decrypt problem

I already have a code that does encryption / decryption on Android and on VB.NET on my server.

b4a
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) ' 16 bytes for AES
     
    c.Initialize("DESEDE/CBC/PKCS5Padding")
    c.InitialisationVector = iv
    kg.Initialize("DESEDE")
   
    kg.KeyFromBytes(bconv.StringToBytes("1234567890123456","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) ' 16 bytes for AES
     
    c.Initialize("DESEDE/CBC/PKCS5Padding")
    c.InitialisationVector = iv
    kg.Initialize("DESEDE")
   
    kg.KeyFromBytes(bconv.StringToBytes("1234567890123456","ASCII"))
   
   
   
    data = B64.DecodeStoB(encryptedData)
    data = c.Decrypt(data, kg.Key, True)

    Return bconv.StringFromBytes(data, "ASCII")

End Sub

VB.NET

B4X:
Public Function Encrypt(dataToEncrypt As String) As String

        Try

            Dim buffer As Byte() = Encoding.ASCII.GetBytes(dataToEncrypt)
            Dim des As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()
            des.IV = New Byte() {211, 5, 233, 24, 55, 166, 7, 88}

            des.Key = ASCIIEncoding.UTF8.GetBytes("1234567890123456")


            Dim result = System.Convert.ToBase64String(des.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length))
            des.Clear()
            Return result

        Catch ex As Exception
            Dim s As String
            s = ex.Message
            Return -1 'ex.Message
        End Try

    End Function

    Public Function Decrypt(encryptedData As String) As String

        Try

            Dim buffer As Byte() = Convert.FromBase64String(encryptedData)
            Dim des As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()
            des.IV = New Byte() {211, 5, 233, 24, 55, 166, 7, 88}

            des.Key = ASCIIEncoding.UTF8.GetBytes("1234567890123456")

            Dim result As String = Encoding.ASCII.GetString(des.CreateDecryptor().TransformFinalBlock(buffer, 0, buffer.Length))
            des.Clear()
            Return result

        Catch ex As Exception
            Dim s As String
            s = ex.Message
            Return -1 'ex.Message
        End Try

    End Function

From what I learnt already (I'm new to crypt stuff and to b4i) that b4i doesn't support KeyGenerator.

So what are my options?

I already found a code that works on b4a and does encrypt / decrypt [URL]https://www.b4x.com/android/fo...yption-for-b4j-b4a-and-b4i-also-in-url.58773/[/url]

but how adapt this code to VB.NET? Or keep my VB.NET and adjust b4a code?

Thanks in advance.

Alex
 

Alex_197

Well-Known Member
Licensed User
Longtime User
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
See the description: https://www.b4x.com/android/forum/threads/b4xencryption.48177/

B4XEncryption is a B4A library. It is compatible with iEncryption.Encrypt and Decrypt methods.

Here is my code in b4i. I'm using iEncryption library

B4X:
'Code module
#Region  Project Attributes 
    #ApplicationLabel: B4i Crypt Example
    #Version: 1.0.0 
    'Orientation possible values: Portrait, LandscapeLeft, LandscapeRight and PortraitUpsideDown
    #iPhoneOrientations: Portrait, LandscapeLeft, LandscapeRight
    #iPadOrientations: Portrait, LandscapeLeft, LandscapeRight, PortraitUpsideDown
    #Target: iPhone, iPad
    #ATSEnabled: True
    #MinVersion: 8
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public App As Application
    Public NavControl As NavigationController
    Private Page1 As Page

End Sub

Private Sub Application_Start (Nav As NavigationController)
   
    Try
       
        'SetDebugAutoFlushLogs(True) 'Uncomment if program crashes before all logs are printed.
        NavControl = Nav
        Page1.Initialize("Page1")
        Page1.Title = "Page 1"
        Page1.RootPanel.Color = Colors.White
        NavControl.ShowPage(Page1)
   
        Dim EncryptedData() As Byte = EncryptText("confidential", "123456")
        Dim EncryptedStr As String=BytesToString(EncryptedData,0,EncryptedData.Length, "utf8") ' this is my line 34 that crushes
        Dim DecryptedStr As String=DecryptText(EncryptedData, "123456")
   
        Log(EncryptedStr)
   
        Log(DecryptedStr)
   
    Catch
        Log(LastException)
    End Try
   
End Sub

Private Sub Page1_Resize(Width As Int, Height As Int)
   
End Sub

Private Sub Application_Background
   
End Sub

Sub EncryptText(text As String, password As String) As Byte()

    Dim c As Cipher
    Return c.Encrypt(text.GetBytes("utf8"), password)
End Sub

Sub DecryptText(EncryptedData() As Byte, password As String) As String
    Dim c As Cipher
    Dim b() As Byte = c.Decrypt(EncryptedData, password)
    Return BytesToString(b, 0, b.Length, "utf8")
End Sub

This line crushes Dim EncryptedStr As String=BytesToString(EncryptedData,0,EncryptedData.Length, "utf8")

and this is en error

B4X:
Application_Start
Error occurred on line: 34 (Main)
Error decoding data as string.
Stack Trace: (
  CoreFoundation       1B9B1E61-8CB4-3903-9870-402C3DE959BB + 1227356
  libobjc.A.dylib      objc_exception_throw + 56
  CoreFoundation       1B9B1E61-8CB4-3903-9870-402C3DE959BB + 135616
  B4i Crypt Example    -[B4ICommon BytesToString::::] + 336
  CoreFoundation       1B9B1E61-8CB4-3903-9870-402C3DE959BB + 1252384
  CoreFoundation       1B9B1E61-8CB4-3903-9870-402C3DE959BB + 7472
  B4i Crypt Example    +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1608
  B4i Crypt Example    -[B4IShell runMethod:] + 448
  B4i Crypt Example    -[B4IShell raiseEventImpl:method:args::] + 1648
  B4i Crypt Example    -[B4IShellBI raiseEvent:event:params:] + 1580
 B4i Crypt Example    __33-[B4I raiseUIEvent:event:params:]_block_invoke + 60
 libdispatch.dylib    A3849F96-1C9F-36C5-A15F-70C566F14CFF + 374288
 libdispatch.dylib    A3849F96-1C9F-36C5-A15F-70C566F14CFF + 377220
 libdispatch.dylib    A3849F96-1C9F-36C5-A15F-70C566F14CFF + 57744
 CoreFoundation       1B9B1E61-8CB4-3903-9870-402C3DE959BB + 693732
 CoreFoundation       1B9B1E61-8CB4-3903-9870-402C3DE959BB + 673240
 CoreFoundation       CFRunLoopRunSpecific + 464
 GraphicsServices     GSEventRunModal + 104
 UIKitCore            UIApplicationMain + 1936
 B4i Crypt Example    main + 124
 libdyld.dylib        2E3F4750-8B67-398B-8530-8417651B1718 + 4960
)
<B4IExceptionWrapper: Error Domain=caught_exception Code=0 " Error decoding data as string." UserInfo={NSLocalizedDescription= Error decoding data as string.}>
Application_Active
 
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
Thanks, problem solved. Line 34 should be like this
B4X:
EncryptedStr=su.EncodeBase64(EncryptedData)
 
Upvote 0
Top