gpt4 book ai didi

java - 从 JKS KeyStore 读回 ECPrivateKey

转载 作者:搜寻专家 更新时间:2023-11-01 03:25:48 24 4
gpt4 key购买 nike

我正在尝试从 JKS KeyStore 中读回 ECPrivateKey(包括证书链),如下面的代码所示。

String storeType = "JKS", storePass = "secret", storePath = "c:/keystore.ks";
ECNamedCurveParameterSpec bcParamSpec = ECNamedCurveTable.getParameterSpec("brainpoolp224r1");
ECNamedCurveSpec jceParamSpec = new ECNamedCurveSpec(bcParamSpec.getName(), bcParamSpec.getCurve(), bcParamSpec.getG(), bcParamSpec.getN(), bcParamSpec.getH(), bcParamSpec.getSeed());

KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC");
keyPairGenerator.initialize(jceParamSpec);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
ECPublicKey publicKey = (ECPublicKey)keyPair.getPublic();
ECPrivateKey privateKey = (ECPrivateKey)keyPair.getPrivate();

Certificate trustCert = createX509Certificate("CN=CA", "CN=CA", publicKey, privateKey, "SHA224withECDSA");
Certificate[] chain = { createX509Certificate("CN=Client", "CN=CA", publicKey, privateKey, "SHA224withECDSA"), trustCert };

KeyStore keyStore = KeyStore.getInstance(storeType);
keyStore.load(null, storePass.toCharArray());
keyStore.setKeyEntry("eckey", privateKey, storePass.toCharArray(), chain);

FileOutputStream outputStream = new FileOutputStream(storePath);
keyStore.store(outputStream, storePass.toCharArray());
outputStream.close();

/* Now read it back */
FileInputStream inputStream = new FileInputStream(storePath);
KeyStore keyStore2 = KeyStore.getInstance(storeType);
keyStore2.load(inputStream, storePass.toCharArray());

Key privateKey2 = keyStore2.getKey("eckey", storePass.toCharArray());

Sun/Oracle 安全提供商不支持我使用的曲线,因此我使用的是 Bouncy CaSTLe。 BC 被插入到我的安全提供者列表中的位置 0。存储工作正常,回读失败:

java.security.UnrecoverableKeyException: Unknown named curve: 1.3.36.3.3.2.8.1.1.5
at sun.security.provider.KeyProtector.recover(KeyProtector.java:338)
at sun.security.provider.JavaKeyStore.engineGetKey(JavaKeyStore.java:138)
at sun.security.provider.JavaKeyStore$JKS.engineGetKey(JavaKeyStore.java:55)
at java.security.KeyStore.getKey(KeyStore.java:792)

显然,JKS KeyStore 实现不会遍历安全提供程序列表。但是,这对于 BC 支持的其他 KeyStore 类型工作正常:PKCS12BKS有没有办法在使用 BC 恢复 key 的同时使用 JKS 作为 KeyStore 类型?

上面代码中使用的createX509Certificate方法由:

private static X509Certificate createX509Certificate(String dn, String issuer, PublicKey publicKey, PrivateKey privateKey, String sigAlg) throws Exception {
X509V3CertificateGenerator certGenerator = new X509V3CertificateGenerator();
certGenerator.setSerialNumber(BigInteger.valueOf(Math.abs(new Random().nextLong())));
certGenerator.setIssuerDN(new X509Name(issuer));
certGenerator.setSubjectDN(new X509Name(dn));
certGenerator.setNotBefore(Calendar.getInstance().getTime());
certGenerator.setNotAfter(Calendar.getInstance().getTime());
certGenerator.setPublicKey(publicKey);
certGenerator.setSignatureAlgorithm(sigAlg);
X509Certificate certificate = (X509Certificate)certGenerator.generate(privateKey, "BC");
return certificate;
}

最佳答案

我唯一的想法是实现你自己的 Provider它可以将“KeyFactory”请求代理到 BouncyCaSTLe 提供者,将其余请求代理到另一个了解 JKS 的提供者,或者将所有请求代理到 BouncyCaSTLe,但“KeyStore”请求除外。 Here's有关如何实现提供程序的文档。

关于java - 从 JKS KeyStore 读回 ECPrivateKey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14072095/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com