B4J Question jB4XEncrption - ERROR

Guenter Becker

Active Member
Licensed User
Longtime User
Hello,
Im using latest jB4XEncrytion, #AdditionalJar: bcprov-jdk18on-1.84, Java 19.02., B4J 10.5
Running the code in Debug or Release mode I get an error and compiling is aborted
Don't know why because sub is not used at this moment,

using this code from the forum with Error in compiling :

B4X:
public Sub DecryptText(EncryptedData() As Byte, password As String) As String
Dim c As B4XCipher
If EncryptedData.Length > 0 And password <> "" Then
Dim b() As Byte = c.Decrypt(EncryptedData, password) <---- Error ??
Return BytesToString(b, 0, b.Length, "utf8")
Else
Return ""
End If
End Sub

Error message:

B4X:
Waiting for debugger to connect...
mod1._decrypttext (java line: 114)
java.lang.NullPointerException: Cannot read the array length because "<parameter1>" is null
at b4j.example.mod1._decrypttext(mod1.java:114)
at b4j.example.main._appstart(main.java:133)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:578)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:117)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:104)
at b4j.example.main.start(main.java:37)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
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:184)
at java.base/java.lang.Thread.run(Thread.java:1589)

Help requested to solve the Problem, Thank you
 

Swissmade

Well-Known Member
Licensed User
Longtime User
I'm using #AdditionalJar: bcprov-jdk18on-172

And have this function do it all. Maybe you can use it


Encrypt and Decrypt:
Public Sub SetAES(Mess As String, Key As String, Encrypt As Boolean) As String
    Try
        Dim passGen As B4XCipher
        Dim Conv As ByteConverter
        Dim strReturn As String

        If Encrypt = True Then
            Dim Data() As Byte = passGen.Encrypt(Mess.GetBytes("UTF8"), Key)
'        Log(BytesToString(Data, 0, Data.Length, "UTF8"))
            strReturn = Conv.HexFromBytes(Data)
            'test    Decrypt
            Dim Bytes() As Byte = passGen.Decrypt(Conv.HexToBytes(strReturn), Key)
            Dim tmpPass As String = BytesToString(Bytes, 0, Bytes.Length, "UTF8")
            If tmpPass = Mess Then

#If DEBUG          
                Log("Password SetAES OK")
#End If          
            End If
        Else
            Dim Bytes() As Byte = passGen.Decrypt(Conv.HexToBytes(Mess), Key)
            Dim tmpPass As String = BytesToString(Bytes, 0, Bytes.Length, "UTF8")
            strReturn = tmpPass
        End If
        Return strReturn
    Catch
        ShowLog("Error getting Password or Key", True, LastException.Message)
        Return "Wrong-Key"
    End Try
End Sub

ShowLog not used for you just change it to log if you like
 
Upvote 0

Guenter Becker

Active Member
Licensed User
Longtime User
I'm using #AdditionalJar: bcprov-jdk18on-172

And have this function do it all. Maybe you can use it


Encrypt and Decrypt:
Public Sub SetAES(Mess As String, Key As String, Encrypt As Boolean) As String
    Try
        Dim passGen As B4XCipher
        Dim Conv As ByteConverter
        Dim strReturn As String

        If Encrypt = True Then
            Dim Data() As Byte = passGen.Encrypt(Mess.GetBytes("UTF8"), Key)
'        Log(BytesToString(Data, 0, Data.Length, "UTF8"))
            strReturn = Conv.HexFromBytes(Data)
            'test    Decrypt
            Dim Bytes() As Byte = passGen.Decrypt(Conv.HexToBytes(strReturn), Key)
            Dim tmpPass As String = BytesToString(Bytes, 0, Bytes.Length, "UTF8")
            If tmpPass = Mess Then

#If DEBUG         
                Log("Password SetAES OK")
#End If         
            End If
        Else
            Dim Bytes() As Byte = passGen.Decrypt(Conv.HexToBytes(Mess), Key)
            Dim tmpPass As String = BytesToString(Bytes, 0, Bytes.Length, "UTF8")
            strReturn = tmpPass
        End If
        Return strReturn
    Catch
        ShowLog("Error getting Password or Key", True, LastException.Message)
        Return "Wrong-Key"
    End Try
End Sub

ShowLog not used for you just change it to log if you like
Will try it thankyou
 
Upvote 0
Top