Android Question java.lang.RuntimeException: error:0c074079:ASN.1 encoding routines:ASN1_get_object:HEADER_TOO_LONG

Angelo Messina

Active Member
Licensed User
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:131)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:180)
at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262)
at sisoft.rifornimenti.corrispettivielettronici._firmaxml(corrispettivielettronici.java:1168)
at sisoft.rifornimenti.corrispettivielettronici._btninviocorrhttps_click(corrispettivielettronici.java:1421)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA$2.run(BA.java:370)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.security.spec.InvalidKeySpecException: java.lang.RuntimeException: error:0c074079:ASN.1 encoding routines:ASN1_get_object:HEADER_TOO_LONG
at com.android.org.conscrypt.OpenSSLKey.getPrivateKey(OpenSSLKey.java:283)
at com.android.org.conscrypt.OpenSSLRSAKeyFactory.engineGeneratePrivate(OpenSSLRSAKeyFactory.java:64)
at java.security.KeyFactory.generatePrivate(KeyFactory.java:187)
at sisoft.rifornimenti.corrispettivielettronici.getSHA256RSA(corrispettivielettronici.java:2921)
... 26 more
Caused by: java.lang.RuntimeException: error:0c074079:ASN.1 encoding routines:ASN1_get_object:HEADER_TOO_LONG
at com.android.org.conscrypt.NativeCrypto.d2i_PKCS8_PRIV_KEY_INFO(Native Method)
at com.android.org.conscrypt.OpenSSLKey.getPrivateKey(OpenSSLKey.java:281)
... 29 more
B4X:
 nativeMe.InitializeContext
 Dim Signature As String=nativeMe.RunMethod("getSHA256RSA",Array(Canonico,$"4N4DfFoKq9Cg25AVmCk3V5VfKypdinHE/nUWfBmXxhA="$))
 Log(Signature)

#If Java
import android.util.Base64;
import java.security.KeyFactory;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.MessageDigest;
import java.util.Locale;
import android.text.TextUtils;
 public static String getSHA256RSA(String input, String strPk) throws Exception
   {
        // Remove markers and new line characters in private key
        String realPK = strPk.replaceAll("-----END PRIVATE KEY-----", "")
                             .replaceAll("-----BEGIN PRIVATE KEY-----", "")
                             .replaceAll("\n", "");
 byte[] b1 = realPK.getBytes("UTF-8");
 
        PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(b1);
        KeyFactory kf = KeyFactory.getInstance("RSA");
        Signature privateSignature = Signature.getInstance("SHA256withRSA");
        privateSignature.initSign(kf.generatePrivate(spec));
        privateSignature.update(input.getBytes("UTF-8"));
        byte[] s = privateSignature.sign();
  return Base64.encodeToString(s, Base64.DEFAULT);
    }
#End If
 
Top