B4J Question [SOLVED] How to run a .jar without a wrapper?

DonManfred

Expert
Licensed User
Longtime User
it is the same for b4j. Use javaobject.
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
The source is available here: https://github.com/AnywhereSoftware...nywheresoftware/b4x/object/B4XEncryption.java
You can see what the library is doing with the bouncy castle...
Don Manfred,

I'm not interested in how it works - I can make it do what I want just fine.

If you look at the post I refer to above, namely:
https://www.b4x.com/android/forum/threads/solved-obfuscating-of-library-methods.110448/

you'll see what I am chasing - a way to make hackers work a bit harder.

Do you know of any full B4J examples of accessing a jar via javaobject?

Thanks...
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Do you know of any full B4J examples of accessing a jar via javaobject?
No. I never use javaobject. I usually write a wrapper in java using eclipse if i need access to a java library.

But here is one

B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

#AdditionalJar: jbcrypt-0.4

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
    
    Dim bc As JavaObject = GetBCrypt
    Dim saltstr As String = bc.RunMethod("gensalt",Null)
    Dim hashedpw As String = bc.RunMethod("hashpw",Array("Test :-)",saltstr))
    Log("HashedPassword="&hashedpw)
End Sub
Sub GetBCrypt As JavaObject
    Dim jo As JavaObject
    Return jo.InitializeStatic("org.mindrot.jbcrypt.BCrypt")
End Sub
Sub GetContext As JavaObject
    Return GetBA.GetField("context")
End Sub

Sub GetBA As JavaObject
    Dim jo As JavaObject
    Dim cls As String = Me
    cls = cls.SubString("class ".Length)
    jo.InitializeStatic(cls)
    Return jo.GetFieldJO("processBA")
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
 

Attachments

  • jbcrypt-0.4.jar
    17 KB · Views: 190
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
Don Manfred,

Ah, that's the sort of code I'm looking for.

Now to really show my ignorance, can you explain how the string "org.mindrot.jbcrypt.BCrypt" is arrived at - if I google it it takes me to the github project but I don't see that string anywhere.
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
Looking into jbcrypt jar from above (rename to.zip) you find a structure
Digging through the jbcrypt-0.4.jar you gave at post #6 - I worked this out about 30 seconds before your email arrived:)

It is a good help if you know some java
As I said I'm java illiterate - and Objective C too - but I normally can work out what I need to do if I can find a good enough template - which I think you have now supplied.

Many thanks for your perseverance...

PS I'm off to bed now - I may have more questions tomorrow when I try to implement this.
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
Don Manfred,

Your example code can be simplified to:
B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

#AdditionalJar: jbcrypt-0.4

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show

    Dim bc As JavaObject
    bc = bc.InitializeStatic("org.mindrot.jbcrypt.BCrypt")

    Dim saltstr As String = bc.RunMethod("gensalt",Null)
    Dim hashedpw As String = bc.RunMethod("hashpw",Array("Test :-)",saltstr))
    Log("HashedPassword="&hashedpw)
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

The GetContext and GetBA subroutines are never used and the GetBCrypt subroutine can be replaced as indicated.

BUT...

When I modify this simplified code to try to drive jB4XEncryption:
B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

'#AdditionalJar: jbcrypt-0.4
#AdditionalJar: bcprov-jdk15on-159
#AdditionalJar: jB4XEncryption

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show

'    Dim bc As JavaObject
'    bc = bc.InitializeStatic("org.mindrot.jbcrypt.BCrypt")

'    Dim saltstr As String = bc.RunMethod("gensalt",Null)
'    Dim hashedpw As String = bc.RunMethod("hashpw",Array("Test :-)",saltstr))
'    Log("HashedPassword="&hashedpw)

    Private wrk_hexstr As String = "377B68B781B99388E6BE69E62AFA4145E660F4DFB81627CF9C2D969D403C0D1DC401A1B4841F90002533231A7555A9ACD238DBA3022BF74D"
 
    Private Obj_bytehandler As ByteConverter
 
    Private wrk_bytes() As Byte = Obj_bytehandler.HexToBytes(wrk_hexstr)

    Private wrk_jo As JavaObject
    wrk_jo = wrk_jo.InitializeStatic("anywheresoftware.b4x.object.B4XEncryption")

    wrk_bytes = wrk_jo.RunMethod("Decrypt", Array(wrk_bytes, "sneaky"))
 
    'Should produce AKIAZZZZZZZZZZZZZZZZ
    Log(BytesToString(wrk_bytes, 0, wrk_bytes.Length, "UTF8"))

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
I get:
Error occurred on line: 36 (Main)
java.lang.IllegalArgumentException: object is not an instance of declaring class
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
at b4j.example.main._appstart(main.java:106)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:632)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:78)
at b4j.example.main.start(main.java:38)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:919)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:175)
at java.base/java.lang.Thread.run(Thread.java:844)

Where line 36 is:

wrk_bytes = wrk_jo.RunMethod("Decrypt", Array(wrk_bytes, "sneaky"))

Any ideas?
 
Last edited:
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
Don Manfred,

You are my java magician - works like a charm.

For anyone who is interested, the final code looks like:
B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

#AdditionalJar: bcprov-jdk15on-159
#AdditionalJar: jB4XEncryption

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show

    Private wrk_hexstr As String = "377B68B781B99388E6BE69E62AFA4145E660F4DFB81627CF9C2D969D403C0D1DC401A1B4841F90002533231A7555A9ACD238DBA3022BF74D"
    
    Private Obj_bytehandler As ByteConverter
    
    Private wrk_bytes() As Byte = Obj_bytehandler.HexToBytes(wrk_hexstr)

    Private wrk_jo As JavaObject
    wrk_jo = wrk_jo.InitializeNewInstance("anywheresoftware.b4x.object.B4XEncryption", Null)

    wrk_bytes = wrk_jo.RunMethod("Decrypt", Array(wrk_bytes, "sneaky"))
    
    'Should produce AKIAZZZZZZZZZZZZZZZZ
    Log(BytesToString(wrk_bytes, 0, wrk_bytes.Length, "UTF8"))

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
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
Don Manfred,

Now the dust has settled I'm in a reflective mood.

On thing is nagging me - why is the jbcrypt-0.4 jar "static" and the jB4XEncryption jar isn't?

And how could you tell?

And what does "static" mean anyrate?

Regards...
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
From the code. The class only contains static methods.

B4X:
public class BCrypt {
[...]
   public static String hashpw(String password, String salt) {

nd what does "static" mean anyrate?
No idea. I suggest to buy a java book.

For any further question please create a new thread.
 
Upvote 0
Top