1

我正在尝试使用bcprov-jdk15-136.jarbcprov-jdk16-140.jar的充气城堡库

我有以下 RSA 私钥:

generateRSAKeyPair - RSA 私钥,iccKeypair 在 KEK 下加密B058474721CBD75820F5F56C1AC91B593FF8C28C866E7764BFC59C6070781524ACB91501FAB84250F04FAF9776712B82558C2B8985EA0CFD718577360A362BFBBC761DFC94EE8582250EB31A04B147DAC28FF425511923832199D2D4BE2E296EF1A1A249BCB68012508DBDE6CF0C7AB6313EFBBE2A0E527AD5C481A37E00473859C578269D972A243756991AD40E231C6051C50BBF71559D2FA77AC6A2D7CC2D101DEB5FBC0B4EAB1E16093A9AF181338F68E56611166725A8EFE2284EC0FB8F7919B8556B30E4BCEBE8150873DC5E0F9467686E38F4742746412EB4402FFC471AC0A55C9486ED2A613ADED759607675A548029C9C70875452A2724EC7D9E1D96D928BCE1FCF1681C39759CDE165CD9B484B222D2D33C3CA53BD461F11033F503F618FB4B9003C946D5B785594950E2C4A8D6EB299E19FF1147183A59A7FAFA2F3AD676917DAF0E14C735BB526266D8732F608B282891D0887C58C65857D52B00EE5E24B3C58426E9F6858DC7E6807F6B2CE4353A4088DBE8761F20AB0017541D0E0A58637342744870B18BB1D3606EAC351D049EE064E0C5CED63983B52E1D4D2A484CC82D962CD06FA611AD06075F04F642FFDB2FC0EFF89294313E519402DDF8B98DF821689A2E00CB5839DACE3D96D69E1FC3CF3D79FD84C72904D24D392

并具有以下 RSA 公钥:

generateRSAKeyPair - RSA Public Key, iccKeypair cyphered under KEK BDBC2FC40FBA580EFB9A9AC346C10FCE6F4C72BD6D79D2039807ABAB644D9F49F5EC06152B78A9C4D7468D6A82F3FEBFF0ABC7EE166C9FBC2A10DB1FE1AA4C66D6B94C871BEC97CE818EE03DDBFB55F5CC9E050E7BB7202EEBD6471172491F2EA22B83D62A747EE6677DBB7CFDA2ED6B0C3EE10D550760E20654A0421133778B

每当我尝试运行以下代码时:

X500Principal subject = new X500Principal("CN=Test V3 Certificate");
PKCS10CertificationRequest kpGen = new PKCS10CertificationRequest("SHA1withRSA", subject, publicKey, null, privateKey);

我不断得到

java.lang.IllegalArgumentException: can't encode public key
    at org.bouncycastle.jce.PKCS10CertificationRequest.<init>(Unknown Source)
    at org.bouncycastle.jce.PKCS10CertificationRequest.<init>(Unknown Source)
    at com.gemalto.nrs.test.KmsTest.generatePCK10(KmsTest.java:148)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at junit.framework.JUnit4TestAdapter.run(JUnit4TestAdapter.java:39)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:518)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:1052)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:906)

当我尝试调试代码时,有时我会得到

java.lang.ClassCastException: org.bouncycastle.asn1.DERApplicationSpecific cannot be cast to org.bouncycastle.asn1.ASN1Sequence
at org.bouncycastle.jce.PKCS10CertificationRequest.<init>(Unknown Source)
at org.bouncycastle.jce.PKCS10CertificationRequest.<init>(Unknown Source)
at com.gemalto.nrs.test.KmsTest.generatePCK10(KmsTest.java:148)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at junit.framework.JUnit4TestAdapter.run(JUnit4TestAdapter.java:39)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:518)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:1052)
4

1 回答 1

0

生成的公钥和私钥只是 DER 而不是 ASN1

所以我必须在使用它们之前为它们每个人获取 ASN1,这是这个问题的根本原因。

以下调用解决了这个问题:

    PublicKey publicKey = RSAtoX509Key.getPublicKey(PUBLIC_KEY.getBytes(), RSA_EXPONENT, "BC");
    PrivateKey privateKey = RSAtoX509Key.getPrivateKey(PRIVATE_KEY.getBytes(), RSA_EXPONENT, "BC");

    public static PublicKey getPublicKey(byte[] modulus, byte[] exponent, String provider) {
        PublicKey publicKey = null;
        try {
            RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger(1, modulus), new BigInteger(1, exponent));
            KeyFactory keyFactory = null;
            if(provider != null && !provider.isEmpty()) {
                keyFactory = KeyFactory.getInstance("RSA", provider);
            } else {
                keyFactory = KeyFactory.getInstance("RSA");
            }

            publicKey = keyFactory.generatePublic(pubKeySpec);
        } catch(Exception ex) {
            logger.error(ex.getMessage());
            return null;
        }

        return publicKey;
    }

当然,密钥是使用 KEK 密钥加密的,但这是另一个问题,与上述异常无关。

于 2013-01-22T16:47:05.030 回答