gpt4 book ai didi

java - 如何在 keystore 中存储 key

转载 作者:太空狗 更新时间:2023-10-29 22:40:29 25 4
gpt4 key购买 nike

我需要将 2 个 key 存储到 KeyStore这是相关代码:

KeyStore ks = KeyStore.getInstance("JKS");
String password = "password";
char[] ksPass = password.toCharArray();
ks.load(null, ksPass);
ks.setKeyEntry("keyForSeckeyDecrypt", privateKey, null, null);
ks.setKeyEntry("keyForDigitalSignature", priv, null, null);
FileOutputStream writeStream = new FileOutputStream("key.store");
ks.store(writeStream, ksPass);
writeStream.close();

虽然我得到一个执行“私钥必须伴随证书链”

那到底是什么?我将如何生成它?

最佳答案

您还需要为私钥条目提供证书(公钥)。对于由 CA 签名的证书,链是 CA 的证书和最终证书。对于自签名证书,您只有自签名证书
示例:

KeyPair keyPair = ...;//You already have this  
X509Certificate certificate = generateCertificate(keyPair);
KeyStore keyStore = KeyStore.getInstance("JKS");
keyStore.load(null,null);
Certificate[] certChain = new Certificate[1];
certChain[0] = certificate;
keyStore.setKeyEntry("key1", (Key)keyPair.getPrivate(), pwd, certChain);

要生成证书,请遵循此 link :
示例:

public X509Certificate generateCertificate(KeyPair keyPair){  
X509V3CertificateGenerator cert = new X509V3CertificateGenerator();
cert.setSerialNumber(BigInteger.valueOf(1)); //or generate a random number
cert.setSubjectDN(new X509Principal("CN=localhost")); //see examples to add O,OU etc
cert.setIssuerDN(new X509Principal("CN=localhost")); //same since it is self-signed
cert.setPublicKey(keyPair.getPublic());
cert.setNotBefore(<date>);
cert.setNotAfter(<date>);
cert.setSignatureAlgorithm("SHA1WithRSAEncryption");
PrivateKey signingKey = keyPair.getPrivate();
return cert.generate(signingKey, "BC");
}

关于java - 如何在 keystore 中存储 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13894699/

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