B4J Question b4xencryption

le_toubib

Active Member
Licensed User
Longtime User
hi all
in mysql i encrypted a field using the sql :
UPDATE `TABLE` SET `FIELD1` = AES_ENCRYPT(`FIELD2`,'PASSWORD123456')
(both fields are varchar(100))

I fetched data using php json encode
in b4j i tryed to decrypt using the code :
B4X:
Sub decrypt (s As String) As String
    Dim su As StringUtils
            Dim Cipher As B4XCipher
             Dim bc As ByteConverter   
   s=bc.StringFromBytes(Cipher.Decrypt(s.GetBytes("UTF8"), "PASSWORD123456"),"UTF8")
   Return s
End Sub

but i get the error :
Error occurred on line: 2089 (Codes)
java.lang.ArrayIndexOutOfBoundsException
at java.lang.System.arraycopy(Native Method)
at anywheresoftware.b4x.object.B4XEncryption.Decrypt(B4XEncryption.java:61)
at b4j.example.codes._decrypt(codes.java:2006)
at b4j.example.main._getone1(main.java:14919)
at b4j.example.main._jobdone(main.java:18213)
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:612)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:229)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
at sun.reflect.GeneratedMethodAccessor19.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:90)
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:462)
at anywheresoftware.b4a.keywords.Common.access$0(Common.java:442)
at anywheresoftware.b4a.keywords.Common$CallSubDelayedHelper.run(Common.java:516)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(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$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
java.lang.RuntimeException: java.net.SocketException: Socket closed
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:114)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:90)
at anywheresoftware.b4a.BA$2.run(BA.java:165)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(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$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.SocketException: Socket closed
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:118)
at java.net.SocketOutputStream.write(SocketOutputStream.java:155)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
at anywheresoftware.b4a.shell.ShellConnector.sendControlMessage(ShellConnector.java:55)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:182)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
at sun.reflect.GeneratedMethodAccessor19.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
... 9 more
what am i missing ?
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
It is only part of the answer but. s.getBytes() is not enough. you must decode it with base64 like this.

B4X:
Dim byt() As Byte = SU.DecodeBase64(text)
    Dim bytEn() As Byte = Cipher.Decrypt(byt,"PASSWORD123456")
    Return BytesToString(bytEn,0,bytEn.Length,"UTF-8")
 
Upvote 0

le_toubib

Active Member
Licensed User
Longtime User
It is only part of the answer but. s.getBytes() is not enough. you must decode it with base64 like this.

B4X:
Dim byt() As Byte = SU.DecodeBase64(text)
    Dim bytEn() As Byte = Cipher.Decrypt(byt,"PASSWORD123456")
    Return BytesToString(bytEn,0,bytEn.Length,"UTF-8")
Thanks for reply ...
Should I encode it in base64 in mysql as well ?
 
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
That is why i said that it was a partial answer. i know that for encryption to work FROM and TO B4x, you should use base64, but i do not know if this line is already doing it.

AES_ENCRYPT(`FIELD2`,'PASSWORD123456')
 
Upvote 0

le_toubib

Active Member
Licensed User
Longtime User
That is why i said that it was a partial answer. i know that for encryption to work FROM and TO B4x, you should use base64, but i do not know if this line is already doing it.

AES_ENCRYPT(`FIELD2`,'PASSWORD123456')
I used
B4X:
  Update table SET `FIELD 1` = TO_BASE64 (AES_ENCRYPT(`FIELD2`,'PASSWORD123456'))
In mysql . But still some result ..
 
Upvote 0
Top