Android Question Decrypt Message

aaronk

Well-Known Member
Licensed User
Longtime User
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:
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 ?
 

aaronk

Well-Known Member
Licensed User
Longtime User
I can't seem to work out how to do this ?

I am guessing I am doing it wrong somewhere..

In B4A I am sending the data to B4J using:
SendToServer("message to send")
B4X:
Private Sub SendEventToServer(Event As String, Data As Map)

        Dim m As Map
        m.Initialize
        m.Put("type", "event")
        m.Put("event", Event)
        m.Put("params", Data)
        Dim jg As JSONGenerator
        jg.Initialize(m)

        Dim msg As String = jg.ToString
        Dim secure As StringUtils
        msg = secure.EncodeBase64(msg.GetBytes("UTF8"))

        ws.SendText(msg)

End Sub
Private Sub SendToServer(msg As String)
    Dim Data As Map
    Data.Initialize
    Data.Put("message", msg)
    SendEventToServer("cloud_message", Data)
End Sub

In B4J I have used the following to receive the data:
B4X:
Sub Cloud_Message(Params As Map)
 
    Dim msg_value As String = Params.Get("message")
    Dim msg_value_byte() As Byte
 
    Dim secure As StringUtils
    msg_value_byte = secure.DecodeBase64(msg_value)
 
 
    msg_value = BytesToString(msg_value_byte,0,msg_value_byte.Length,"UTF8")
 
 
    Log("msg_value = " & msg_value)
End Sub

However it logs the following in the B4J IDE:
java.lang.RuntimeException: JSON Object expected.
at anywheresoftware.b4j.objects.collections.JSONParser.NextObject(JSONParser.java:50)
at anywheresoftware.b4j.object.WebSocketModule$Adapter.onWebSocketText(WebSocketModule.java:110)
at org.eclipse.jetty.websocket.common.events.JettyListenerEventDriver.onTextMessage(JettyListenerEventDriver.java:189)
at org.eclipse.jetty.websocket.common.message.SimpleTextMessage.messageComplete(SimpleTextMessage.java:69)
at org.eclipse.jetty.websocket.common.events.AbstractEventDriver.appendMessage(AbstractEventDriver.java:66)
at org.eclipse.jetty.websocket.common.events.JettyListenerEventDriver.onTextFrame(JettyListenerEventDriver.java:158)
at org.eclipse.jetty.websocket.common.events.AbstractEventDriver.incomingFrame(AbstractEventDriver.java:162)
at org.eclipse.jetty.websocket.common.WebSocketSession.incomingFrame(WebSocketSession.java:367)
at org.eclipse.jetty.websocket.common.extensions.ExtensionStack.incomingFrame(ExtensionStack.java:214)
at org.eclipse.jetty.websocket.common.Parser.notifyFrame(Parser.java:220)
at org.eclipse.jetty.websocket.common.Parser.parse(Parser.java:256)
at org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection.readParse(AbstractWebSocketConnection.java:663)
at org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection.onFillable(AbstractWebSocketConnection.java:493)
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:261)
at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:95)
at org.eclipse.jetty.io.SelectChannelEndPoint$2.run(SelectChannelEndPoint.java:75)
at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceAndRun(ExecuteProduceConsume.java:213)
at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:147)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:654)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:572)
at java.lang.Thread.run(Thread.java:745)

I am guessing Params.Get("message") is still encrypted, but can't work how to decrypt it (if that is the reason why it's not working) ?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Private su As StringUtils

Public Sub EncryptText(text As String) As String
    Dim c As B4XCipher
    Return su.EncodeBase64(c.Encrypt(text.GetBytes("utf8"), "12345"))
End Sub

Public Sub DecryptText(EncryptedData As String) As String
    Dim c As B4XCipher
    Dim b() As Byte = c.Decrypt(su.DecodeBase64(EncryptedData), "12345")
    Return BytesToString(b, 0, b.Length, "utf8")
End Sub
 
Upvote 0
Top