3

我正在尝试使用 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();
}
}
4

1 回答 1

0

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

于 2012-09-18T00:03:59.580 回答