import java.nio.Buffer;
/**
* @author TBear
*
*/
import java.awt.TexturePaintContext.Byte;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.SignatureException;
import java.security.cert.Certificate;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateParsingException
import java.security.cert.X509Certificate;
import javax.security.auth.x500.X500Principal;
import org.bouncycastle.asn1.ASN1EncodableVector;
import org.bouncycastle.asn1.DERBitString
import org.bouncycastle.asn1.DERInteger
import org.bouncycastle.asn1.DERSequence
import org.bouncycastle.asn1.DERUTCTime
import org.bouncycastle.asn1.x509.AlgorithmIdentifier
import org.bouncycastle.asn1.x509.BasicConstraints
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.bouncycastle.asn1.x509.V3TBSCertificateGenerator
import org.bouncycastle.asn1.x509.X509CertificateStructure
import org.bouncycastle.asn1.x509.X509Extensions;
import org.bouncycastle.asn1.x509.X509ExtensionsGenerator;
import org.bouncycastle.asn1.x509.X509Name
import org.bouncycastle.jce.provider.BouncyCastleProvider
import org.bouncycastle.jce.provider.X509CertificateObject
import org.bouncycastle.x509.extension.SubjectKeyIdentifierStructure;
import com.itrus.raapi.info.CertInfo;
public class CertService{
public static void main(String[] args)
{
X509CertificateObject certificate = genSM2_Cert();
byte[] buffer = certificate.getEncoded();
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(new File('E:/abc.cer')));
outputStream.write buffer;
outputStream.flush();
outputStream.close();
}
//X509CertificateObject
static X509CertificateObject genSM2_Cert() throws CertificateParsingException{
long currTime = new Date().getTime();
String issuerDN = "cn=ibm";
String subjectDN = "cn=ibm";
int vday = 10;
//证书生成
V3TBSCertificateGenerator v3CertGen = new V3TBSCertificateGenerator();
//系列号
DERInteger serialNumber = new DERInteger(BigInteger.valueOf(currTime));
v3CertGen.setSerialNumber(serialNumber);
//发行人
v3CertGen.setIssuer(new X509Name(issuerDN));
//开始时间和结束时间
v3CertGen.setStartDate(new DERUTCTime(new Date(currTime)));
v3CertGen.setEndDate(new DERUTCTime(new Date(currTime + vday*24*60*60*1000)));
//主题
v3CertGen.setSubject(new X509Name(subjectDN));
//签名算法
AlgorithmIdentifier algSign = new AlgorithmIdentifier("1.2.156.197.1.501");
v3CertGen.setSignature(algSign);
//公钥算法
AlgorithmIdentifier algKey = new AlgorithmIdentifier("1.2.156.197.1.301");
byte[] pubData = new byte[65];
pubData[0] = 0;
for(byte i=1;i<pubData.length;i++){
pubData[i] = i;
}
//有公钥算法和签名算法生成公钥信息摘要
SubjectPublicKeyInfo pubKeyInfo = new SubjectPublicKeyInfo(algKey,pubData);
v3CertGen.setSubjectPublicKeyInfo(pubKeyInfo);
byte[] signInfo = new byte[69];
for(byte i=1;i<pubData.length;i++){
pubData[i] = i;
}
X509ExtensionsGenerator extenGen = new X509ExtensionsGenerator();
//extenGen.addExtension(paramDERObjectIdentifier, paramBoolean, paramArrayOfByte);
X509Extensions exten = extenGen.generate();
v3CertGen.setExtensions(exten);
ASN1EncodableVector asn1encodablevector = new ASN1EncodableVector();
asn1encodablevector.add(v3CertGen.generateTBSCertificate());
asn1encodablevector.add(algSign);
asn1encodablevector.add(new DERBitString(signInfo));
return new X509CertificateObject(new X509CertificateStructure(new DERSequence(asn1encodablevector)));
}
}
注意最终的输出流必须用BufferedOutputStream;其他的流写不出!
分享到:
相关推荐
IDEA连接数据库生成带注解的实体类方法与定制Groovy生成脚本适用于springboot jpa中IDEA连接数据库生成带注解的实体类方法与定制Groovy生成脚本适用于springboot jpa中
IDEA groovy生成实体解决乱码问题
IDEA自带的插件Generate POJOs.groovy 比较简陋,不能生成完整的dao/mapper,切生成的POJO没有注解。没有统一格式化,所以在此基础上进行了扩展能够简单的生成pojo/dao/mapper. 使用时选择目录后会在改目录下生成...
groovy-2.3.6-installer windows安装版本
日常的开发都用到 Mybatis Plus,但是每次开新功能都得设计表、映射实体代码,写相关service、mapper。...于是自己写了个Groovy脚本来生成 演示文章链接:https://blog.csdn.net/m4330187/article/details/122444249
Java调用Groovy,实时动态加载数据库groovy脚本,java读取mongoDB的groovy脚本,加载实时运行,热部署
赠送jar包:groovy-3.0.9.jar; 赠送原API文档:groovy-3.0.9-javadoc.jar; 赠送源代码:groovy-3.0.9-sources.jar; 赠送Maven依赖信息文件:groovy-3.0.9.pom; 包含翻译后的API文档:groovy-3.0.9-javadoc-API...
apache-groovy-3.0.8.zip apache官网的groovy3.0.8版本,希望大家多多下载,apache-groovy-3.0.8.zip apache官网的groovy3.0.8版本,希望大家多多下载,apache-groovy-3.0.8.zip apache官网的groovy3.0.8版本,希望...
MyVo Generate POJOs.groovy生成实体类文件
Making Java Groovy is a practical handbook for developers who want to blend Groovy into their day to day work with Java It starts by introducing the key differences between Java and Groovy and how you...
groovy入门经典,groovyeclipse 插件
Groovy轻松入门—搭建Groovy开发环境 Groovy轻松入门—搭建Groovy开发环境
Groovy jar包 3.0.
JUN SpringBoot API Service 是一个基于SpringBoot+Groovy+SQL动态生成API并动态发布,且发布后可动态执行groovy脚本及SQL脚本的API服务项目。提供在线执行动态程序脚热加载本及动态生成API并执行的功能。支持动态...
Generate POJOs.groovy 生成POJO内容包括: 1.@Data 2.@column 3./** comment */
idea利用自带插件Generate POJOs.groovy生成pojo类。idea可以连接数据库,datagrip集成在里面了。
什么是 Groovy? Groovy 是 JVM 的一个替代语言 — 替代 是指可以用 Groovy 在 Java 平台上进行 Java 编程,使用方式基本与使用 Java 代码的方式相同。在编写新应用程序时,Groovy 代码能够与 Java 代码很好地结合,...
Groovy in Action, Second Edition is the undisputed definitive reference on the Groovy language. Written by core members of the Groovy language team, this book presents Groovy like no other can—from ...
Groovy
groovy