java-如何在密钥库中存储密钥

java-如何在密钥库中存储密钥,第1张

java-如何在密钥库中存储密钥

您还需要提供私钥条目的证书(公钥)。对于由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);

要生成证书,请点击以下链接:
示例:

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");  }


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5142892.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-18
下一篇 2022-11-18

发表评论

登录后才能评论

评论列表(0条)

保存