bouncycastle 运行错误?
myeclipse 10.0新建一个 java project.
JRE选javaSE-1.6
需要的3个jar包:bcmail-jdk16-146.jar,bcprov-ext-jdk15on-147.jar,bcprov-jdk16-146.jar
调用顺序也是如此。
按照http://blog.163.com/11_gying/blog/static/4067301220134293350972/文章写的。
下列程序出现如下问题:
Exception in thread "main" java.lang.NoSuchMethodError: org.bouncycastle.asn1.x509.V3TBSCertificateGenerator.setSerialNumber(Lorg/bouncycastle/asn1/DERInteger;)V
at org.bouncycastle.cert.X509v3CertificateBuilder.<init>(Unknown Source)
at org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder.<init>(Unknown Source)
at hgh.generateV3(hgh.java:70)
at hgh.main(hgh.java:49)
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Security;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.sql.Date;
import java.util.List;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.x500.X500Name;
import org.bouncycastle.cert.X509CertificateHolder;
import org.bouncycastle.cert.X509v3CertificateBuilder;
import org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.operator.ContentSigner;
import org.bouncycastle.operator.OperatorCreationException;
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
public class hgh {
static {
Security.addProvider(new BouncyCastleProvider());
}
public static void main(String[] args) {
try {
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(2048);
KeyPair keyPair = kpg.generateKeyPair();
KeyStore store = KeyStore.getInstance("JKS");
store.load(null, null);
String issuer = "C=CN,ST=GuangDong,L=Shenzhen,O=Skybility,OU=Cloudbility,CN=Atlas Personal License CA,E=cwjcsu@126.com";
String subject = issuer;
//issuer 与 subject相同的证书就是CA证书
Certificate cert = generateV3(issuer, subject,
BigInteger.ZERO, new Date(System.currentTimeMillis() - 1000
* 60 * 60 * 24),
new Date(System.currentTimeMillis() + 1000L * 60 * 60 * 24
* 365 * 32), keyPair.getPublic(),//待签名的公钥
keyPair.getPrivate(), null);
store.setKeyEntry("atlas", keyPair.getPrivate(),
"atlas".toCharArray(), new Certificate[] { cert });
cert.verify(keyPair.getPublic());
File file = new File("resource/atlas-ca.jks");
if (file.exists() || file.createNewFile())
store.store(new FileOutputStream(file), "atlas".toCharArray());
} catch (Exception e) {
e.printStackTrace();
}
}
public static Certificate generateV3(String issuer, String subject,
BigInteger serial, Date notBefore, Date notAfter,
PublicKey publicKey, PrivateKey privKey, List<Extension> extensions)
throws OperatorCreationException, CertificateException, IOException {
X509v3CertificateBuilder builder = new JcaX509v3CertificateBuilder(
new X500Name(issuer), serial, notBefore, notAfter,
new X500Name(subject), publicKey);
ContentSigner sigGen = new JcaContentSignerBuilder("SHA1withRSA")
.setProvider("BC").build(privKey);
//privKey:使用自己的私钥进行签名,CA证书
if (extensions != null)
for (Extension ext : extensions) {
builder.addExtension(new ASN1ObjectIdentifier(ext.getOid()),
ext.isCritical(),
ASN1Primitive.fromByteArray(ext.getValue()));
}
X509CertificateHolder holder = builder.build(sigGen);
CertificateFactory cf = CertificateFactory.getInstance("X.509");
InputStream is1 = new ByteArrayInputStream(holder.toASN1Structure()
.getEncoded());
X509Certificate theCert = (X509Certificate) cf.generateCertificate(is1);
is1.close();
return theCert;
}
public class Extension {
private String oid;
private boolean critical;
private byte[] value;
public String getOid() {
return oid;
}
public byte[] getValue() {
return value;
}
public boolean isCritical() {
return critical;
}
}
}
why? --------------------编程问答-------------------- 看异常应该是JAR不对,是不是jar不是你的操作系统的?(32 or 64位)
猜测 --------------------编程问答-------------------- 应该是调用的JAR版本不一致造成。
比如:前一版本有setSerialNumber(Long l)方法,后面版本又去掉了,而调用的地方还没有相应的变化。
但是只要JAR版本一致没有问题,那就应该不会出现这样的严重错误。再或者是你放了2个版本的JAR再程序里,造成这种问题。 --------------------编程问答-------------------- 你的包放错了 ,应该放bcmail-jdk16-146.jar,bcprov-ext-jdk15on-147.jar或者bcprov-jdk15on-147.jar ,bcprov-jdk16-146.jar。下载资源网址:http://central.maven.org/maven2/org/bouncycastle/[size=16px][/size]
补充:Java , Eclipse