Hi,
I am using B4J as my backend for my app, and I have a websocket and sending data between the B4A app and B4J app.
I have the connection working fine and when not encrypting the message being sent it works fine, except I am now trying to decrypt the message being sent.
I have tried using B4XEncryption - https://www.b4x.com/android/forum/threads/b4xencryption.48177/#content
However, I seem to get an error on the B4A side.
B4J Code:
When I trigger Send_Message above, it will then send the data to my B4A app..
B4A:
It seems to log the following in the B4A IDE:
Any ideas on how to decrypt the in coming message in my B4A app ?
I am using B4J as my backend for my app, and I have a websocket and sending data between the B4A app and B4J app.
I have the connection working fine and when not encrypting the message being sent it works fine, except I am now trying to decrypt the message being sent.
I have tried using B4XEncryption - https://www.b4x.com/android/forum/threads/b4xencryption.48177/#content
However, I seem to get an error on the B4A side.
B4J Code:
B4X:
Public Sub EncryptText(text As String) As Byte()
Dim c As B4XCipher
Return c.Encrypt(text.GetBytes("utf8"), "12345")
End Sub
' This sub will send the messaage to the app
Sub Send_Message
Dim msg As String = $"{"Command":"Test", "Message": "Test Message"}"$
msg = BytesToString(EncryptText(msg), 0, EncryptText(msg).Length, "UTF8")
Log("OUT: " & msg) ' this seems to log the encryped message
ws.RunFunction("server_incoming", Array As Object(msg))
ws.Flush
End Sub
When I trigger Send_Message above, it will then send the data to my B4A app..
B4A:
B4X:
Public Sub DecryptText(EncryptedData() As Byte) As String
Dim c As B4XCipher
Dim b() As Byte = c.Decrypt(EncryptedData, "12345")
Return BytesToString(b, 0, b.Length, "utf8")
End Sub
' Data from the server get sent to this sub
Sub server_incoming(Params As List)
Log("** server_incoming **")
Dim msg As String
msg = Params.Get(0)
Dim s As String = msg
Dim bytes() As Byte = s.GetBytes("UTF8")
msg = AppGlobals.DecryptText(bytes)
Log(msg)
End Sub
Private Sub ws_TextMessage(msg As String)
Dim jp As JSONParser
jp.Initialize(msg)
Dim m As Map = jp.NextObject
Dim etype As String = m.get("etype")
Dim params As List = m.get("value")
Dim Event As String = m.get("prop")
If etype = "runFunction" Then
If Event = "server_incoming" Then
server_incoming(params)
End If
End If
End Sub
It seems to log the following in the B4A IDE:
javax.crypto.IllegalBlockSizeException: error:1e00007b:Cipher functions:OPENSSL_internal:WRONG_FINAL_BLOCK_LENGTH
at com.android.org.conscrypt.NativeCrypto.EVP_CipherFinal_ex(Native Method)
at com.android.org.conscrypt.OpenSSLCipher$EVP_CIPHER.doFinalInternal(OpenSSLCipher.java:568)
at com.android.org.conscrypt.OpenSSLCipher.engineDoFinal(OpenSSLCipher.java:350)
at javax.crypto.Cipher.doFinal(Cipher.java:2056)
at anywheresoftware.b4a.object.B4XEncryption.Decrypt(B4XEncryption.java:47)
at m1.touch.cloud.appglobals._decrypttext(appglobals.java:1306)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at anywheresoftware.b4a.objects.WebSocketWrapper$1.onTextMessage(WebSocketWrapper.java:77)
at de.tavendo.autobahn.WebSocketConnection$2.handleMessage(WebSocketConnection.java:392)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
TextMessage Error: (Exception) java.lang.Exception: javax.crypto.IllegalBlockSizeException: error:1e00007b:Cipher functions:OPENSSL_internal:WRONG_FINAL_BLOCK_LENGTH
Any ideas on how to decrypt the in coming message in my B4A app ?