Android Question java.security.spec.InvalidKeySpecException: java.lang.RuntimeException:

Angelo Messina

Active Member
Licensed User
Dim privateKey As String=$"MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQC2gqnurckKk1Zb
AXqyGDTKBVUg7wyr/WTb5ow0mecad…….

B4X:
    Dim su As StringUtils
    Dim privateKey As String="
    Dim data() As Byte = Array As Byte(1, 2, 3)
    Dim ByteFromPrivKey() As Byte = su.DecodeBase64(privateKey)
    Dim k2 As KeyPairGenerator
    k2.Initialize("RSA",2048)
    k2.PublicKeyFromBytes(ByteFromPrivKey)
    Dim sign As Signature
    sign.Initialise("SHA256withRSA",sign.SIGNATURE_SIGN,k2.PrivateKey)
    sign.Update(data)
 
Last edited:

Angelo Messina

Active Member
Licensed User
java.security.spec.InvalidKeySpecException: java.lang.RuntimeException: error:0c0890ba:ASN.1 encoding routines:asn1_check_tlen:WRONG_TAG
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 anywheresoftware.b4a.agraham.encryption.CipherWrapper$KeyPairGeneratorWrapper.PrivateKeyFromBytes(CipherWrapper.java:457)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
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:1139)
at sisoft.rifornimenti.corrispettivielettronici._btncertificahttps_click(corrispettivielettronici.java:1029)
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.lang.RuntimeException: error:0c0890ba:ASN.1 encoding routines:asn1_check_tlen:WRONG_TAG
at com.android.org.conscrypt.NativeCrypto.d2i_PKCS8_PRIV_KEY_INFO(Native Method)
at com.android.org.conscrypt.OpenSSLKey.getPrivateKey(OpenSSLKey.java:281)
 

Angelo Messina

Active Member
Licensed User
error
java.security.spec.InvalidKeySpecException: java.lang.RuntimeException: error:0c0890ba:ASN.1 encoding routines:asn1_check_tlen:WRONG_TAG
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 anywheresoftware.b4a.agraham.encryption.CipherWrapper$KeyPairGeneratorWrapper.PrivateKeyFromBytes(CipherWrapper.java:457)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
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:1139)
at sisoft.rifornimenti.corrispettivielettronici._btncertificahttps_click(corrispettivielettronici.java:1029)
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.lang.RuntimeException: error:0c0890ba:ASN.1 encoding routines:asn1_check_tlen:WRONG_TAG
at com.android.org.conscrypt.NativeCrypto.d2i_PKCS8_PRIV_KEY_INFO(Native Method)
at com.android.org.conscrypt.OpenSSLKey.getPrivateKey(OpenSSLKey.java:281)
... 27 more
B4X:
    Dim data() As Byte = bc.StringToBytes(Canonico, "UTF8")
    Dim privateKeyByte() As Byte = bc.StringToBytes(privatekey2,"UTF8")
    Dim k2 As KeyPairGenerator
    k2.Initialize("RSA",2048)
    k2.PrivateKeyFromBytes(privateKeyByte)

    
    Dim sign As Signature
    sign.Initialise("SHA256withRSA",sign.SIGNATURE_SIGN,k2.PrivateKey)
    sign.Update(data)
    Dim b() As Byte = sign.Sign
    
    Log("bc" & bc.HexFromBytes(b))
    Firma = Encode64(bc.HexFromBytes(b))
error at line : k2.PrivateKeyFromByte(privatekeyByte)
 
Top