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:
This the error
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!
I found this super example:
[B4X] Cross platform Editable B4XTable + Form Example
This example demonstrates several things: Cross platform code and files, similar to the way XUI2D games are organized: All the logic is implemented in a class named EditableTable. The module is located in the projects parent folder. The two asset files (list of animals - source; and the...
www.b4x.com
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!