java - 私钥算法与最终实体证书中的公钥算法不匹配(索引 0)

标签 java certificate x509certificate keystore

我正在尝试将私钥及其证书链存储在 keystore 中,但出现以下错误:私钥算法与最终实体证书中的公钥算法不匹配(索引 0)

这就是我生成 key 对的方式:

public GenerateKeyPair() throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException {

    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

    //Generating and ECDSA KeyPair
    ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("prime239v3");
    KeyPairGenerator g = KeyPairGenerator.getInstance("ECDSA", "BC");

    g.initialize(ecSpec, new SecureRandom());

    KeyPair keygen = g.generateKeyPair();

    //Setting the ECDSA KeyGen
    this.keygen = keygen;
}

这是我用来生成 X509Certificate 的方法:
public static X509Certificate GetCertificate_v3(KeyPair keygen, Date startDate, Date expiryDate, 
        String serial,  String Certification_Aut_Id) throws InvalidKeyException, SecurityException, SignatureException{

    X509V3CertificateGenerator v3CertGen =  new X509V3CertificateGenerator();
    v3CertGen.setSerialNumber(BigInteger.valueOf(System.currentTimeMillis()));
    v3CertGen.setIssuerDN(new X509Principal("CN=" + Certification_Aut_Id + ", O=o, L=L, ST=il, C= c"));
    v3CertGen.setNotBefore(startDate);
    v3CertGen.setNotAfter(expiryDate);
    v3CertGen.setSubjectDN(new X509Principal("CN=" + Certification_Aut_Id + ", O=o, L=L, ST=il, C= c"));
    v3CertGen.setPublicKey(keygen.getPublic());
    v3CertGen.setSignatureAlgorithm("SHA256withECDSA");
    X509Certificate cert = v3CertGen.generateX509Certificate(keygen.getPrivate());

    return cert;

}

用于存储 key 对的代码是:
public static void storeKeypair(String KSpwd, String PKpwd, String KSname, X509Certificate certificate, 
        KeyPair keygen, String alias, String temp_local) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException{

    //Before a keystore can be accessed, it must be loaded.
        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
        // get user password and file input stream
        char[] KSpassword = KSpwd.toCharArray();
        FileInputStream fis = new java.io.FileInputStream(KSname);
        ks.load(fis, KSpassword);
        fis.close();

        //writing the X509Certificate in a .cer file
        FileOutputStream fos1 = new FileOutputStream(temp_local + alias + ".cer");
        fos1.write( certificate.getEncoded() );
        fos1.flush();
        fos1.close();

    // Load the certificate chain (in X.509 DER encoding).
        FileInputStream certificateStream = new FileInputStream(temp_local + alias + ".cer");
        CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
        Certificate[] chain = {};
        chain = certificateFactory.generateCertificates(certificateStream).toArray(chain);

    // save my private key & certificate chain
        char[] PKpassword = PKpwd.toCharArray();
        ks.setEntry(alias, new KeyStore.PrivateKeyEntry(keygen.getPrivate(), chain),
                    new KeyStore.PasswordProtection(PKpassword)
                );

    //Store the KeyStore
     // Write out the keystore
        FileOutputStream fos = new FileOutputStream(KSname);
        ks.store(fos, KSpassword);
        fos.close();
}

产生的错误是:
Exception in thread "main" java.lang.IllegalArgumentException: private key algorithm does not match algorithm of public key in end entity certificate (at index 0)
at java.security.KeyStore$PrivateKeyEntry.<init>(KeyStore.java:408)
at SDSGeneration.keyStore.storeKeypair(keyStore.java:65)
at FinalTest.main(FinalTest.java:70)

最佳答案

我在使用 Web Crypto API 时遇到了同样的问题.我的问题是 我正在使用 key 对 而不是 派生 key 来加密消息。

你可以找到一个完整的例子 here

关于java - 私钥算法与最终实体证书中的公钥算法不匹配(索引 0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26673740/

相关文章:

java - wss4j: - 找不到别名的 key :monit

c - 将 X509 证书保存到文件

android - 将 .p12 文件导入 AndroidKeyStore

java - 如何在vlcj中使用 "MediaPlayer"准备播放列表?

java - 线性时间内的 BST toString

java - 通过 xpath w.r.t 到其他元素查找元素

java - X509 的 Spring-Security?

java - 从 Eclipse 使用资源文件夹部署 AWS Lambda 函数时出现问题

java - cmd 无法识别 ssl 连接证书

Android - 使用 Thawte CA 的 SSL 证书连接到 Web 服务时出现 SSLPeerUnverifiedException