B4J Question Encrypt - problem to store an encrypted text and to decrypt it

Elric

Well-Known Member
Licensed User
Hello everybody!

I found this super example:

My aim is to use this example to adapt it and store data into database. But, first of all, I would like to understand how I can encrypt data (also on csv).

So I've tried to apply the Erel's code (https://www.b4x.com/android/forum/threads/b4xencryption.48177/) without success.

Better start with a new simple project, I've thought: B4XEncryption library, one button and three textfield: one to write something to be encrypted, one to store the encrypted string took from the first and one to get the decryption from the second. But again no success.

The simple project code:
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private TextField1 As B4XView
    Private TextField2 As B4XView
    Private TextField3 As B4XView
    
    Dim myPassword As String = "12345678"
End Sub

Public Sub Initialize
    
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Sub Button1_Click
    'xui.MsgboxAsync("Hello world!", "B4X")
    
    Dim myString As String = TextField1.Text
    Dim myEncryptedData() As Byte
    Dim myStringFromTextField2 As String
    Dim myByteToBeDecrypted() As Byte
    Dim myDecriptedString As String
    
    myEncryptedData = EncryptText(myString, myPassword)
    'Log(myEncryptedData)
    Log(DecryptText(myEncryptedData, myPassword))
    
    TextField2.Text = BytesToString(myEncryptedData, 0, myEncryptedData.Length,"utf-8")
    
    myStringFromTextField2 = TextField2.Text
    
    myByteToBeDecrypted = myStringFromTextField2.GetBytes("utf-8") ' <----- This generates a byte array different from myEncriptedData byte array
    
    myDecriptedString = DecryptText(myByteToBeDecrypted, myPassword) ' <----- This line make the program crashed. Maybe is due to myByteToBeDecrypted byte array is different from myEncriptedData byte array
    TextField3.Text = myDecriptedString
    
End Sub

Sub EncryptText(text As String, password As String) As Byte()
    Dim c As B4XCipher
    Return c.Encrypt(text.GetBytes("utf-8"), password)
End Sub

Sub DecryptText(EncryptedData() As Byte, password As String) As String
    Dim c As B4XCipher
    Dim b() As Byte = c.Decrypt(EncryptedData, password)
    Return BytesToString(b, 0, b.Length, "utf-8")
End Sub

This the error
B4X:
Waiting for debugger to connect...
Program started.
Test text
Errore nella linea: 61 (B4XMainPage)
javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:936)
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:847)
    at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:446)
    at javax.crypto.Cipher.doFinal(Cipher.java:2164)
    at anywheresoftware.b4a.object.B4XEncryption.Decrypt(B4XEncryption.java:47)
    at b4j.example.b4xmainpage._decrypttext(b4xmainpage.java:146)
    at b4j.example.b4xmainpage._button1_click(b4xmainpage.java:106)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:632)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
    at anywheresoftware.b4a.BA$1.run(BA.java:216)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    at java.lang.Thread.run(Thread.java:748)

I've tried also with jStringUtils and ByteConverter without success.

What is wrong?

... and is it possible to encrypt/decrypt and entire list at once or I've to encrypt/decrypt each item?

Thank you!
 

Elric

Well-Known Member
Licensed User
I solved the first issue:
B4X:
Sub Button1_Click
   
    Dim myStringToBeEncrypted As String
    Dim myEncryptedDataByte() As Byte
    Dim myEncryptedDataString64 As String
    Dim myByteToBeDecrypted() As Byte
    Dim myDecriptedDataString As String
   
    Dim su As StringUtils
   
    'To encrypt
    myStringToBeEncrypted = TextField1.Text
    myEncryptedDataByte = EncryptText(myStringToBeEncrypted, myPassword)
    myEncryptedDataString64 = su.EncodeBase64(myEncryptedDataByte)
    TextField2.Text = myEncryptedDataString64
   
    myEncryptedDataString64 = ""
   
    'To decrypt
    myEncryptedDataString64 = TextField2.Text
    myByteToBeDecrypted = su.DecodeBase64(myEncryptedDataString64)
    myDecriptedDataString = DecryptText(myByteToBeDecrypted, myPassword)
    TextField3.Text = myDecriptedDataString
   
End Sub
thanks to https://www.b4x.com/android/forum/threads/b4xencryption.48177/post-473574.

So I tried to implement that in TableAndForms but, if I can encrypt data, I'm not able to decrypt them.

This:
B4X:
Sub LoadData
    Dim data As List
    
    Dim data2 As List
    Dim myEncryptedDataString64 As String
    Dim myByteToBeDecrypted() As Byte
    Dim myDecriptedDataString As String
    
    If File.Exists(xui.DefaultFolder, CSVFile) Then
        Dim su As StringUtils
        data = su.LoadCSV(xui.DefaultFolder, CSVFile, ",") 'This create a bidimensional list: at index 0 there are the five items of the first row of the table (one per column)
        
        'Start to decrypt data
        For ii = 0 To data.Size-1
            
            ' To extract from each record each field: the single encrypted data
            Dim myArr(data.Size-1)
            myArr = data.Get(ii) ' To split the fields in the record: save each record (first level) of the list "data" in an array
            data2.Initialize
            For i = 0 To myArr.Length-1
                data2.InsertAt(i, myArr(i))  ' and then store the array into the new list "data2" (maybe this is not necessary)
            Next
            
            ' Decrypt each encrypted field
            For i = 0 To data2.Size-1
                Log(data2.Get(i))
                myEncryptedDataString64 = data2.Get(i)
                myByteToBeDecrypted = su.DecodeBase64(myEncryptedDataString64)
                myDecriptedDataString = DecryptText(myByteToBeDecrypted, myPassword)
                Log(myDecriptedDataString)
                data2.Set(i,myDecriptedDataString)
            Next
            
            ' Replace the encrypted field with the decrypted field
            For i = 0 To data2.Size-1
                myArr(i) = data2.Get(i) ' and then store into an array
            Next
            data.Set(ii, myArr) 'Insert for each record of CSV file the decrypted fields
        Next
        
    Else
        data.Initialize
    End If
    B4XTable1.SetData(data)
End Sub

returns me other encrypted data.

Thank you!
 
Last edited:
Upvote 0

Elric

Well-Known Member
Licensed User
I don't know but everything works now (maybe was due to 5 o'clock of the morning? o_O).

Sorry. I'm not able to edit or delete the thread anymore.

I left the .zip if you think may be useful as example.

My last question: can I encrypt a list all at once in a single command string or I need to disassemble it?
 

Attachments

  • B4XPageTableAndForms+B4XEncryption.zip
    7.6 KB · Views: 169
Upvote 0
Top