使用Bouncy Castle Java API生成X.509证书

时间:2012-09-17 20:17:22

标签: java cryptography x509certificate bouncycastle

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

我找到了示例代码:

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

但是,在编译下面的代码之后,JVM会显示一条错误消息,如下所示

Could not find the main class: org.bouncycastle.util.AllTests. Program will exit.

请注意,org.bouncycastle.util.AllTests已插入Java构建路径中。

如何修复我的代码才能正常工作?

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

1 个答案:

答案 0 :(得分:0)

您可能应该在类路径中包含bctest-jdk15on-147.jar(http://www.bouncycastle.org/download/bctest-jdk15on-147.jar)。