B4J Question AES Decrypt issue

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I am trying to Decrypt a AES (AES-128) message but having a issue in doing so.

My Code:

B4X:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
    #AdditionalJar: bcprov-jdk15on-154
#End Region

Sub Process_Globals

End Sub

Sub AppStart (Args() As String)
    DecryptMessage

    StartMessageLoop
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Public Sub DecryptMessage

    Dim EncryptedData As String = "$�H(��<n��U���s�ޒKt|�&<,�V_���uUtB���;_p(�b���p��ӧ�����"

    Private su As StringUtils
    Dim c As B4XCipher ' jB4XEncryption 1.00
    Dim b() As Byte = c.Decrypt(su.DecodeBase64(EncryptedData), "123456")
    Log(BytesToString(b, 0, b.Length, "utf8"))

End Sub

The error shows:

Waiting for debugger to connect...
Program started.
Error occurred on line: 29 (Main)
java.io.IOException: Bad Base64 input character decimal 36 in array position 0
at anywheresoftware.b4a.objects.Base64.decode(Base64.java:1204)
at anywheresoftware.b4a.objects.Base64.decode(Base64.java:1259)
at anywheresoftware.b4a.objects.Base64.decode(Base64.java:1227)
at anywheresoftware.b4a.objects.StringUtils.DecodeBase64(StringUtils.java:36)
at b4j.example.main._decryptmessage(main.java:103)
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.shell.Shell.runMethod(Shell.java:613)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:228)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
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)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:93)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:77)
at b4j.example.main.main(main.java:29)

Line 29 is Dim b() As Byte = c.Decrypt(su.DecodeBase64(EncryptedData), "123456")

The string is sent to me from another device (not from a B4X project, so I have no control on how it gets encrypted).

The Password is a base64Binary and I used 123456 as the password.

Any ideas on what I am doing wrong ?
 
Last edited:

udg

Expert
Licensed User
Longtime User
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
I tried pretty much everything but can't work it out..

Using the Encryption Library (v1.10) I used the following code:

B4X:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
   
End Sub

Sub AppStart (Args() As String)

    Log(Decrypt("$�H(��<n��U���s�ޒKt|�&<,�V_���uUtB���;_p(�b���p��ӧ�����"))

    StartMessageLoop
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Decrypt(dataToDecrypt As String) As String
    Dim su As StringUtils
    Dim kg As KeyGenerator
    Dim C As Cipher
    Dim md As MessageDigest
    Dim decrypted() As Byte
    Dim BConv As ByteConverter
   
    Dim Key As String = "123456"

    kg.Initialize("AES")
    kg.KeyFromBytes(md.GetMessageDigest(Key.GetBytes("UTF8"), "MD5"))
      
    C.Initialize("AES/ECB/PKCS5Padding")

    decrypted = su.DecodeBase64(dataToDecrypt)
    decrypted = C.Decrypt(decrypted, kg.Key, False)

    Return BConv.StringFromBytes(decrypted, "UTF8")
End Sub

But it shows the following error:
(seems to be the same error message)

Waiting for debugger to connect...
Program started.
Error occurred on line: 38 (Main)
java.io.IOException: Bad Base64 input character decimal 36 in array position 0
at anywheresoftware.b4a.objects.Base64.decode(Base64.java:1204)
at anywheresoftware.b4a.objects.Base64.decode(Base64.java:1259)
at anywheresoftware.b4a.objects.Base64.decode(Base64.java:1227)
at anywheresoftware.b4a.objects.StringUtils.DecodeBase64(StringUtils.java:36)
at b4j.example.main._decrypt(main.java:128)
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.shell.Shell.runMethod(Shell.java:613)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:228)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
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)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:93)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:77)
at b4j.example.main.main(main.java:29)

I can't seem to work this out.

I know it uses AES128 encryption and I can set the AES Key (which I used 123456 for testing) and then it will send me strings encrypted. I now need to work out how to decrypt this string so I can work with it in B4J.

Anyone know where I have gone wrong ?
 
Upvote 0
Top