我在将APN 的身份验证密钥加载到Java 时遇到了困难。我的理解是 Java 可以读取 PKCS8 编码的私钥,但我得到了一个例外。
我使用 Bouncy Castle (bcprov-jdk15on-1.55) 时遇到的异常
org.bouncycastle.jcajce.provider.asymmetric.util.ExtendedInvalidKeySpecException: unable to process key spec: java.io.IOException: algorithm identifier 1.2.840.10045.2.1 in key not recognised
at org.bouncycastle.jcajce.provider.asymmetric.rsa.KeyFactorySpi.engineGeneratePrivate(Unknown Source)
at java.security.KeyFactory.generatePrivate(KeyFactory.java:366)
Caused by: java.io.IOException: algorithm identifier 1.2.840.10045.2.1 in key not recognised
at org.bouncycastle.jcajce.provider.asymmetric.rsa.KeyFactorySpi.generatePrivate(Unknown Source)
... 29 more
Run Code Online (Sandbox Code Playgroud)
我使用 Java 时遇到的异常 (jdk1.8.0_74)
java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: Invalid RSA private key
at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:217)
at java.security.KeyFactory.generatePrivate(KeyFactory.java:372)
Caused by: java.security.InvalidKeyException: Invalid RSA private key
at sun.security.rsa.RSAPrivateCrtKeyImpl.parseKeyBits(RSAPrivateCrtKeyImpl.java:206)
at sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:342)
at sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:356)
at …Run Code Online (Sandbox Code Playgroud)