使用Java生成证书,公钥和私钥

使用Java生成证书,公钥和私钥,第1张

使用Java生成证书,公钥和私钥

您可以使用一对或密钥在Java中动态生成证书。(公钥,私钥)。以BigInteger格式获取这些密钥,并检查以下代码以生成证书。

RSAPrivateKeySpec serPrivateSpec = new RSAPrivateKeySpec(    new BigInteger(val of pub key), new BigInteger(val of pri key));fact = KeyFactory.getInstance("RSA");PrivateKey serverPrivateKey = fact.generatePrivate(serPrivateSpec);RSAPublicKeySpec serPublicSpec = new RSAPublicKeySpec(    new BigInteger(agentCL.getSerPubMod()), new BigInteger(agentCL.getSerPubExp()));PublicKey serverPublicKey = fact.generatePublic(serPublicSpec);keyStore = KeyStore.getInstance(IMXAgentCL.STORE_TYPE);keyStore.load(null, SOMEPWD.toCharArray());Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());X509Certificate[] serverChain = new X509Certificate[1];X509V3CertificateGenerator serverCertGen = new X509V3CertificateGenerator();X500Principal serverSubjectName = new X500Principal("CN=OrganizationName");serverCertGen.setSerialNumber(new BigInteger("123456789"));// X509Certificate caCert=null;serverCertGen.setIssuerDN(somename);serverCertGen.setNotBefore(new Date());serverCertGen.setNotAfter(new Date());serverCertGen.setSubjectDN(somename);serverCertGen.setPublicKey(serverPublicKey);serverCertGen.setSignatureAlgorithm("MD5WithRSA");// certGen.addExtension(X509Extensions.AuthorityKeyIdentifier, false,new// AuthorityKeyIdentifierStructure(caCert));serverCertGen.addExtension(X509Extensions.SubjectKeyIdentifier, false,    new SubjectKeyIdentifierStructure(serverPublicKey));serverChain[0] = serverCertGen.generateX509Certificate(serverPrivateKey, "BC"); // note: private key of CAkeyStore.setEntry("xyz",    new KeyStore.PrivateKeyEntry(serverPrivateKey, serverChain),    new KeyStore.PasswordProtection("".toCharArray()));

希望这会帮助你。



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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存