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:


Any ideas on how to decrypt the in coming message in my B4A app ?
 

aaronk

Well-Known Member
Licensed User
Longtime User
You need to use Base64 encoding instead (StringUtils).
Is that for both sides?

EncodeBase64 on B4J and DecodeBase64 on B4A, or do what I am doing in B4J but in B4A use DecodeBase64 ?
 
Upvote 0

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:

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

aaronk

Well-Known Member
Licensed User
Longtime User
Where is the call to EncryptText?
I might of read your reply wrong, but didn't you say I had to use EncodeBase64 / DecodeBase64 instead ?

Or did I read this wrong and doing this wrong ?
 
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
Cookies are required to use this site. You must accept them to continue using the site. Learn more…