B4J Question Using pbkdf2_sha256 crypto

Ilya G.

Active Member
Licensed User
Longtime User
Please help solve the problem, I need to log into the application using the login and password on the site. The key is similar to pbkdf2_sha256$24000$ZMvFC56qgS2J$***********************************

B4X:
Sub AppStart (Args() As String)
    Dim j As JavaObject = Me
    Dim s As String = j.RunMethod("getPass", Array("Password123"))
    StartMessageLoop
End Sub

#If Java
import java.security.spec.KeySpec;
import java.security.AlgorithmParameters;
import javax.crypto.Cipher;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;

public class Decrypter {
    Cipher dcipher;

    byte[] salt = new String("12345678").getBytes();
    int iterationCount = 24000;
    int keyStrength = 32;
    SecretKey key;
    byte[] iv;

    Decrypter(String passPhrase) throws Exception {
        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
        KeySpec spec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount, keyStrength);
        SecretKey tmp = factory.generateSecret(spec);
        key = new SecretKeySpec(tmp.getEncoded(), "AES");
        dcipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    }

    public String encrypt(String data) throws Exception {
        dcipher.init(Cipher.ENCRYPT_MODE, key);
        AlgorithmParameters params = dcipher.getParameters();
        iv = params.getParameterSpec(IvParameterSpec.class).getIV();
        byte[] utf8EncryptedData = dcipher.doFinal(data.getBytes());
        String base64EncryptedData = new sun.misc.BASE64Encoder().encodeBuffer(utf8EncryptedData);
        return base64EncryptedData;
    }

    public void getPass(String args[]) throws Exception {
        Decrypter decrypter = new Decrypter("ABCDEFGHIJKL");
        String encrypted = decrypter.encrypt("the quick brown fox jumps over the lazy dog");
        System.out.println(encrypted);
    }
}
#End If

 

Attachments

  • 1649840847369.png
    17.9 KB · Views: 193

DonManfred

Expert
Licensed User
Longtime User
1. Posts errors as text as it is text. Rightclick to copy.
2. You have to create an Instance of Decrypter first to call a method of it. You are just trying to call a method. There is no such method in the Code you posted. There is such method in the Class Decrypter in the code you posted...
See https://www.b4x.com/android/help/javaobject.html#javaobject_initializenewinstance
3. Best is to upload a small project showing the problem.
 
Last edited:
Upvote 0

Ilya G.

Active Member
Licensed User
Longtime User

I tried like this, is it wrong?

B4X:
     Dim j As JavaObject = Me
      j.InitializeNewInstance("Decrypter", Null)
    
    Log(j.RunMethod("encrypt", Array As String("Password123")))

    main._appstart (java line: 52)
    java.lang.ClassNotFoundException: java$lang$Decrypter
    at anywheresoftware.b4j.object.JavaObject.getCorrectClassName(JavaObject.java:289)
    at anywheresoftware.b4j.object.JavaObject.InitializeNewInstance(JavaObject.java:84)
    at b4j.example.main._appstart(main.java:52)
    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:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:109)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:96)
    at b4j.example.main.main(main.java:36)
 

Attachments

  • Crypt.zip
    1.3 KB · Views: 178
Upvote 0

Ilya G.

Active Member
Licensed User
Longtime User
DonManfred, I tried your Alice lib, but get error:

B4X:
main._appstart (java line: 79)
java.lang.IllegalArgumentException: No enum constant com.rockaport.alice.AliceContext.Mode.PBKDF2WithHmacSHA256
    at java.lang.Enum.valueOf(Enum.java:238)
    at com.rockaport.alice.AliceContext$Mode.valueOf(AliceContext.java:135)
    at de.donmanfred.AliceContextBuilderwrapper.setMode(AliceContextBuilderwrapper.java:54)
    at b4j.example.main._appstart(main.java:79)
    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:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:109)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:96)
    at b4j.example.main.start(main.java:37)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:185)
    at java.lang.Thread.run(Thread.java:748)
 

Attachments

  • Alice.zip
    1 KB · Views: 196
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…