java生成数字证书(一、公钥数字证书)
2015-05-16 17:25
411 查看
这一段时间做毕业设计,要生成数字证书,于是学习了相关知识,这儿和大家分享一下。
首先介绍一下数字证书的分类,数字证书分为两种:公钥数字证书(cer)和私钥数字证书(pfx),顾名思义cer就是只含有公钥的数字证书,pfx则含有私钥。
1.生成公钥数字证书
需要导入bouncycastle-jce-jdk13-112.jar
代码结果如下,有图有真相
写到这儿先休息一下,下一期再说私钥数字证书。88
首先介绍一下数字证书的分类,数字证书分为两种:公钥数字证书(cer)和私钥数字证书(pfx),顾名思义cer就是只含有公钥的数字证书,pfx则含有私钥。
1.生成公钥数字证书
需要导入bouncycastle-jce-jdk13-112.jar
package ca; import java.io.FileOutputStream; import java.io.IOException; import java.math.BigInteger; import java.security.InvalidKeyException; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; import java.security.SecureRandom; import java.security.Security; import java.security.SignatureException; import java.security.cert.CertificateEncodingException; import java.security.cert.X509Certificate; import java.util.Date; import org.bouncycastle.asn1.x509.X509Name; import org.bouncycastle.jce.X509V3CertificateGenerator; import org.bouncycastle.jce.provider.BouncyCastleProvider; public class MyCert { static { Security.addProvider(new BouncyCastleProvider()); } /** * 根据seed产生密钥对 * @param seed * @return * @throws NoSuchAlgorithmException */ public KeyPair generateKeyPair(int seed) throws NoSuchAlgorithmException { KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); kpg.initialize(1024, new SecureRandom(new byte[seed])); KeyPair keyPair = kpg.generateKeyPair(); return keyPair; } /** * 产生数字公钥证书 * String[] info长度为9,分别是{cn,ou,o,c,l,st,starttime,endtime,serialnumber} * @throws SignatureException * @throws SecurityException * @throws NoSuchProviderException * @throws InvalidKeyException */ public X509Certificate generateCert(String[] info, KeyPair keyPair_root,KeyPair keyPair_user) throws InvalidKeyException, NoSuchProviderException, SecurityException, SignatureException { X509V3CertificateGenerator certGen = new X509V3CertificateGenerator(); X509Certificate cert = null; certGen.setSerialNumber(new BigInteger(info[8])); certGen.setIssuerDN(new X509Name( "CN=huahua, OU=hnu, O=university , C=china")); certGen.setNotBefore(new Date(Long.parseLong(info[6]))); certGen.setNotAfter(new Date(Long.parseLong(info[7]))); certGen.setSubjectDN(new X509Name("C=" + info[0] + ",OU=" + info[1] + ",O=" + info[2] + ",C=" + info[3] + ",L=" + info[4] + ",ST=" + info[3])); certGen.setPublicKey(keyPair_user.getPublic()); certGen.setSignatureAlgorithm("SHA1WithRSA"); cert = certGen.generateX509Certificate(keyPair_root.getPrivate(), "BC"); return cert; } /** * 在D盘产生公钥数字证书了 * @param args * @throws NoSuchAlgorithmException * @throws InvalidKeyException * @throws NoSuchProviderException * @throws SecurityException * @throws SignatureException * @throws CertificateEncodingException * @throws IOException */ public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException, SecurityException, SignatureException, CertificateEncodingException, IOException{ MyCert myCert = new MyCert(); KeyPair keyPair_root = myCert.generateKeyPair(10); KeyPair keyPair_user = myCert.generateKeyPair(100); String[] info = {"huahua_user","hnu","university","china","hunan","changsha","111111","11111111","1"}; X509Certificate cert = myCert.generateCert(info, keyPair_root, keyPair_user); String certPath = "d:/"+info[0]+".cer"; FileOutputStream fos = new FileOutputStream(certPath); fos.write(cert.getEncoded()); fos.close(); } }
代码结果如下,有图有真相
写到这儿先休息一下,下一期再说私钥数字证书。88
相关文章推荐
- PHP通过OpenSSL生成证书、密钥并且加密解密数据,以及公钥,私钥和数字签名的理解
- Java 生成数字证书系列(二)剖析数字证书
- Java 生成数字证书系列(四)生成数字证书(续)
- java(13)--配置https加密连接器并生成数字证书
- java安全架构____openssl生成数字证书
- Java 生成数字证书系列(三)生成数字证书
- JAVA中数字证书的维护及生成方法
- Java中的数字证书的生成及维护方法
- java生成数字证书(二、私钥数字证书)
- java生成数字证书方法
- java安全架构____数字证书生成
- mac自带的openssl 生成公钥私钥(包括java,iOS端各平台支持的证书转换)
- java 代码生成签发数字证书
- java生成数字证书
- Java 生成数字证书系列(二)剖析数字证书
- Java 生成数字证书系列(四)生成数字证书(续)
- Java 生成数字证书系列(三)生成数字证书
- java代码调用linux命令,生成.pem公钥私钥证书
- java 代码生成签发数字证书
- java通过ssl连接LDAP服务器(包括LDAP服务器搭建、openssl数字证书生成、openldap服务器配置启动)