Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从hyperledger ca生成的私钥文件中获取java.security.PrivateKey_Java_Hyperledger Fabric_Bouncycastle_Ecdsa - Fatal编程技术网

从hyperledger ca生成的私钥文件中获取java.security.PrivateKey

从hyperledger ca生成的私钥文件中获取java.security.PrivateKey,java,hyperledger-fabric,bouncycastle,ecdsa,Java,Hyperledger Fabric,Bouncycastle,Ecdsa,使用这个工具,我得到了如下的私钥 -----BEGIN PRIVATE KEY----- MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgrECQDuXL87QJKYDO O/Z1TT+vzVPqF3106wT75dJF5OqhRANCAASsFuneE46/9JmUJCiQ14zWDKcFn6TL kYl6mirTXefU7yYglu5hmehU0pD/PKKLkoTLNbPLn5RMdUe8aum3N1sZ -----END PRIVATE K

使用这个工具,我得到了如下的私钥

-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgrECQDuXL87QJKYDO
O/Z1TT+vzVPqF3106wT75dJF5OqhRANCAASsFuneE46/9JmUJCiQ14zWDKcFn6TL
kYl6mirTXefU7yYglu5hmehU0pD/PKKLkoTLNbPLn5RMdUe8aum3N1sZ
-----END PRIVATE KEY-----
默认情况下,软件使用
ecdsa-with-SHA256
prime256v1
)签名算法

在我的java应用程序中,我需要有基于上述私钥的java.security.PrivateKey实例

我尝试了以下代码

 public static void main(String[] args) throws Exception {

        String privateKeyString = "-----BEGIN PRIVATE KEY-----\n" +
                "MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgrECQDuXL87QJKYDO\n" +
                "O/Z1TT+vzVPqF3106wT75dJF5OqhRANCAASsFuneE46/9JmUJCiQ14zWDKcFn6TL\n" +
                "kYl6mirTXefU7yYglu5hmehU0pD/PKKLkoTLNbPLn5RMdUe8aum3N1sZ\n" +
                "-----END PRIVATE KEY-----\n";


        String privateKeyContent = privateKeyString.replaceAll("\\n|-----BEGIN PRIVATE KEY-----|-----END PRIVATE KEY-----", "");
        PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(privateKeyContent.getBytes());
        KeyFactory factory = KeyFactory.getInstance("EC");
        PrivateKey privateKey = factory.generatePrivate(spec);
    }
但是我越来越

Exception in thread "main" java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: invalid key format
    at sun.security.ec.ECKeyFactory.engineGeneratePrivate(ECKeyFactory.java:169)
    at java.security.KeyFactory.generatePrivate(KeyFactory.java:372)
    at QueryApp.main(QueryApp.java:36)
Caused by: java.security.InvalidKeyException: invalid key format
    at sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:330)
    at sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:356)
    at sun.security.ec.ECPrivateKeyImpl.<init>(ECPrivateKeyImpl.java:73)
    at sun.security.ec.ECKeyFactory.implGeneratePrivate(ECKeyFactory.java:237)
    at sun.security.ec.ECKeyFactory.engineGeneratePrivate(ECKeyFactory.java:165)
    ... 2 more
线程“main”中的异常java.security.spec.InvalidKeySpecException:java.security.InvalidKeyException:无效密钥格式 位于sun.security.ec.ECKeyFactory.EngineeGeneratePrivate(ECKeyFactory.java:169) 位于java.security.KeyFactory.generatePrivate(KeyFactory.java:372) 位于QueryApp.main(QueryApp.java:36) 原因:java.security.InvalidKeyException:密钥格式无效 位于sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:330) 位于sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:356) 在sun.security.ec.ECPrivateKeyImpl。(ECPrivateKeyImpl.java:73) 位于sun.security.ec.ECKeyFactory.implGeneratePrivate(ECKeyFactory.java:237) 位于sun.security.ec.ECKeyFactory.EngineeGeneratePrivate(ECKeyFactory.java:165) ... 还有两个
您必须对内容进行base64解码,例如

String privateKeyContent = privateKeyString.replaceAll("\\n|-----BEGIN PRIVATE KEY-----|-----END PRIVATE KEY-----", "");
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKeyContent));
KeyFactory factory = KeyFactory.getInstance("EC");