使用Bouncy Castle Java API生成X.509证书 [英] Generating X.509 Certificate using Bouncy Castle Java API

查看:3020
本文介绍了使用Bouncy Castle Java API生成X.509证书的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Bouncy Castle Java API生成X.509证书。

I am trying to generate a X.509 Certificate using Bouncy Castle Java API.

我在以下网址找到示例代码:

I found sample code at:

http://www.bouncycastle.org/wiki/display/JA1/X.509+Public+Key+Certificate+and+Certification+Request+Generation

无法找到主类org.bouncycastle,但是在编译以下代码后,JVM会显示一个错误消息。 util.AllTests。程序将退出。

请注意,插入了 org.bouncycastle.util.AllTests

如何解决我的代码正常工作?

How can I fix my code to work properly?

import java.math.BigInteger;
import java.util.Date;

import org.bouncycastle.asn1.ASN1Object;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.x500.X500Name;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.bouncycastle.cert.X509CertificateHolder;
import org.bouncycastle.cert.X509v1CertificateBuilder;
import org.bouncycastle.crypto.params.RSAKeyParameters;
import org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters;
import org.bouncycastle.operator.ContentSigner;
import org.bouncycastle.operator.DefaultDigestAlgorithmIdentifierFinder;
import org.bouncycastle.operator.DefaultSignatureAlgorithmIdentifierFinder;
import org.bouncycastle.operator.bc.BcRSAContentSignerBuilder;

public class X509CertificateCreator{
    AlgorithmIdentifier sigAlgId = new DefaultSignatureAlgorithmIdentifierFinder().find("SHA1withRSA");
AlgorithmIdentifier digAlgId = new DefaultDigestAlgorithmIdentifierFinder().find(sigAlgId);

//define lwPrivKey
RSAKeyParameters lwPubKey = new RSAKeyParameters(
        false,
        new BigInteger("b4a7e46170574f16a97082b22be58b6a2a629798419be12872a4bdba626cfae9900f76abfb12139dce5de56564fab2b6543165a040c606887420e33d91ed7ed7", 16),
        new BigInteger("11", 16));


RSAPrivateCrtKeyParameters lwPrivKey = new RSAPrivateCrtKeyParameters(
        new BigInteger("b4a7e46170574f16a97082b22be58b6a2a629798419be12872a4bdba626cfae9900f76abfb12139dce5de56564fab2b6543165a040c606887420e33d91ed7ed7", 16),
        new BigInteger("11", 16),
        new BigInteger("9f66f6b05410cd503b2709e88115d55daced94d1a34d4e32bf824d0dde6028ae79c5f07b580f5dce240d7111f7ddb130a7945cd7d957d1920994da389f490c89", 16),
        new BigInteger("c0a0758cdf14256f78d4708c86becdead1b50ad4ad6c5c703e2168fbf37884cb", 16),
        new BigInteger("f01734d7960ea60070f1b06f2bb81bfac48ff192ae18451d5e56c734a5aab8a5", 16),
        new BigInteger("b54bb9edff22051d9ee60f9351a48591b6500a319429c069a3e335a1d6171391", 16),
        new BigInteger("d3d83daf2a0cecd3367ae6f8ae1aeb82e9ac2f816c6fc483533d8297dd7884cd", 16),
        new BigInteger("b8f52fc6f38593dabb661d3f50f8897f8106eee68b1bce78a95b132b4e5b5d19", 16));


////

public void creation(){
    try {

ContentSigner sigGen = new BcRSAContentSignerBuilder(sigAlgId, digAlgId).build(lwPrivKey);


//
byte[] publickeyb=sigAlgId.getEncoded();
//SubjectPublicKeyInfo subPubKeyInfo = ....;
SubjectPublicKeyInfo subPubKeyInfo = new SubjectPublicKeyInfo((ASN1Sequence)ASN1Object.fromByteArray(publickeyb));


Date startDate = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000);
Date endDate = new Date(System.currentTimeMillis() + 365 * 24 * 60 * 60 * 1000);

X509v1CertificateBuilder v1CertGen = new X509v1CertificateBuilder(
          new X500Name("CN=Test"), 
          BigInteger.ONE, 
          startDate, endDate, 
          new X500Name("CN=Test"), 
          subPubKeyInfo);

X509CertificateHolder certHolder = v1CertGen.build(sigGen);

} catch (Exception E)
{

}
}

public static void main(String[] args) throws Exception {
    System.out.println("CertificationCreator!"); 
    X509CertificateCreator X509C = new X509CertificateCreator();
    X509C.creation();
}
}


推荐答案

应该包括bctest-jdk15on-147.jar( http://www.bouncycastle。 org / download / bctest-jdk15on-147.jar )。

You should probably include bctest-jdk15on-147.jar ( http://www.bouncycastle.org/download/bctest-jdk15on-147.jar) in your class path .

这篇关于使用Bouncy Castle Java API生成X.509证书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆