B4J Question B4XEncryption Decryption help needed [Solved]

Anser

Well-Known Member
Licensed User
Longtime User
Hi,

I am trying to use B4XEncryption and Decryption.

I Encrypt some data in B4A app and I expect B4J ABMaterial application to decrypt and use.

I am using the following Subs provided by Erel to Encrypt and Decrypt

B4X:
Sub EncryptText(text As String, password As String) As Byte()
    Dim c As B4XCipher
    Return c.Encrypt(text.GetBytes("utf8"), 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, "utf8")
End Sub

From my B4A App I encrypt the username and password and then call a WebUrl that contains the encrypted username and password. The Web application is developed using ABMaterial.

For eg. Here is the B4A code where I Encrypt the data
B4X:
Dim encryptedUserName() As Byte = EncryptText(Starter.cUserName.Trim, "123456") ' Returns encrypted data as bytes array
Dim encryptedPassword() As Byte = EncryptText(Starter.cPassword.Trim, "123456") ' Returns encrypted data as bytes array

 ' To pass the encrypted data along with the URL, convert it into string 
encryptedUserNameString = su.EncodeUrl( su.EncodeBase64(encryptedUserName), "UTF8" )
encryptedPasswordString = su.EncodeUrl( su.EncodeBase64(encryptedPassword), "UTF8" )

' The Web URL is created here
cWebAddress = "http://192.168.0.154:5100/MyApp/HomePage/?username=" & encryptedUserNameString & "&password=" & encryptedPasswordString

'Using a WebView, the ABMaterial app is invoked with the above URL

Now here is the B4J (ABMaterial) code where I receive the Parameters ie the username and password as encrypted data and then I try to decrypt it here using the same DeCryptText Sub
B4X:
Dim cUserName as String
Dim cPassword as String
Dim su As StringUtils

'Please note ws is WebSocket in ABMaterial application
  
Dim cTemp() As Byte = su.DecodeBase64( su.DecodeUrl( ws.UpgradeRequest.GetParameter("username") , "UTF8" ) )
cUserName = DecryptText(  cTemp , "123456")

Dim cTemp() As Byte = su.DecodeBase64( su.DecodeUrl( ws.UpgradeRequest.GetParameter("password"), "UTF8" ) )
cPassword = DecryptText(  cTemp , "123456")

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, "utf8")
End Sub

Unfortunately, when I decrypt the encrypted data in the B4J ABMaterial application, I get the following run time error. I could not find a solution and do not know where I have gone wrong.

The run time error is as follows org.bouncycastle.crypto.DataLengthException: last block incomplete in decryption

Websocket first connection
java.lang.RuntimeException: org.bouncycastle.crypto.DataLengthException: last block incomplete in decryption
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:119)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:77)
at anywheresoftware.b4j.object.WebSocketModule$Adapter$ThreadHandler.run(WebSocketModule.java:188)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.bouncycastle.crypto.DataLengthException: last block incomplete in decryption
at org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher.doFinal(Unknown Source)
at anywheresoftware.b4x.object.B4XEncryption.Decrypt(B4XEncryption.java:73)
at com.ab.template.homepage._decrypttext(homepage.java:427)
at com.ab.template.homepage._connectpage(homepage.java:399)
at com.ab.template.homepage._websocket_connected(homepage.java:894)
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:497)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
... 7 more
Disconnected


Any help will be appreciated
 
Last edited:

Anser

Well-Known Member
Licensed User
Longtime User
Solved.

The problem was that, inside the B4J app, to decrypt, I was calling su.DecodeURL, which was not required
The correct code in B4J to decrypt the value should be
B4X:
Dim cTemp() As Byte = su.DecodeBase64(  ws.UpgradeRequest.GetParameter("username")  )
cUserName = DecryptText(  cTemp , "123456")
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
My 1 cent of input:

Do not pass username/password in the URL. URL's are often logged. POST the username/password.
 
Upvote 0
Top