package crypto;
import org.bouncycastle.asn1.x500.X500Name;
import org.bouncycastle.asn1.x509.BasicConstraints;
import org.bouncycastle.asn1.x509.Extension;
import org.bouncycastle.asn1.x509.KeyUsage;
import org.bouncycastle.cert.X509v1CertificateBuilder;
import org.bouncycastle.cert.X509v3CertificateBuilder;
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
import org.bouncycastle.cert.jcajce.JcaX509ExtensionUtils;
import org.bouncycastle.cert.jcajce.JcaX509v1CertificateBuilder;
import org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.operator.ContentSigner;
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
import javax.security.auth.x500.X500Principal;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.*;
import java.security.cert.X509Certificate;
import java.util.Date;
import java.util.Properties;
public class App {
private static final int VALIDITY_PERIOD = 7 * 24 * 60 * 60 * 1000; // one week
public static void main (String[] args) throws Exception {
Security.addProvider(new BouncyCastleProvider());
KeyPair keyPair = generateRSAKeyPair();
X509Certificate rootCert = buildRootCert(keyPair);
buildEndEntityCert(keyPair.getPublic(), keyPair.getPrivate(), rootCert);
}
/**
* Create a random 2048 bit RSA key pair
*/
public static KeyPair generateRSAKeyPair() throws Exception {
KeyPairGenerator kpGen = KeyPairGenerator.getInstance("RSA", "BC");
kpGen.initialize(2048, new SecureRandom());
return kpGen.generateKeyPair();
}
/**
* Build a sample V1 certificate to use as a CA root certificate
*/
public static X509Certificate buildRootCert(KeyPair keyPair)
throws Exception
{
X509v1CertificateBuilder certBldr = new JcaX509v1CertificateBuilder(
new X500Name("CN=Test Root Certificate"),
BigInteger.valueOf(1),
new Date(System.currentTimeMillis()),
new Date(System.currentTimeMillis() + VALIDITY_PERIOD),
new X500Name("CN=Test Root Certificate"),
keyPair.getPublic());
ContentSigner signer = new JcaContentSignerBuilder("SHA1WithRSA").setProvider("BC").build(keyPair.getPrivate());
return new JcaX509CertificateConverter().setProvider("BC").getCertificate(certBldr.build(signer));
}
/**
* Build a sample V3 certificate to use as an end entity certificate
*/
public static X509Certificate buildEndEntityCert(PublicKey entityKey, PrivateKey caKey, X509Certificate caCert)
throws Exception
{
X509v3CertificateBuilder certBldr = new JcaX509v3CertificateBuilder(
caCert.getSubjectX500Principal(),
BigInteger.valueOf(1),
new Date(System.currentTimeMillis()),
new Date(System.currentTimeMillis() + VALIDITY_PERIOD),
new X500Principal("CN=Test End Entity Certificate"),
entityKey);
JcaX509ExtensionUtils extUtils = new JcaX509ExtensionUtils();
certBldr.addExtension(Extension.authorityKeyIdentifier, false, extUtils.createAuthorityKeyIdentifier(caCert))
.addExtension(Extension.subjectKeyIdentifier, false, extUtils.createSubjectKeyIdentifier(entityKey))
.addExtension(Extension.basicConstraints, true, new BasicConstraints(false))
.addExtension(Extension.keyUsage, true, new KeyUsage(KeyUsage.digitalSignature | KeyUsage.keyEncipherment));
ContentSigner signer = new JcaContentSignerBuilder("SHA1withRSA").setProvider("BC").build(caKey);
return new JcaX509CertificateConverter().setProvider("BC").getCertificate(certBldr.build(signer));
}
}
<dependencies>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.56</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
<version>1.47</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcmail-jdk15</artifactId>
<version>1.46</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-ext-jdk15on</artifactId>
<version>1.55</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpg-jdk15on</artifactId>
<version>1.56</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-debug-jdk15on</artifactId>
<version>1.55</version>
</dependency>
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.11</version>
</dependency>
Exception in thread "main" java.lang.NoSuchMethodError: org.bouncycastle.asn1.ASN1Integer.<init>(I)V
at org.bouncycastle.operator.DefaultSignatureAlgorithmIdentifierFinder.createPSSParams(Unknown Source)
at org.bouncycastle.operator.DefaultSignatureAlgorithmIdentifierFinder.<clinit>(Unknown Source)
at org.bouncycastle.operator.jcajce.JcaContentSignerBuilder.<init>(Unknown Source)
at crypto.App.buildRootCert(App.java:62)
at crypto.App.main(App.java:34)
Process finished with exit code 1
有人知道为什么吗?
发生这种情况是因为使用了不同的版本(1.56和1.46)。在1.47版本之后,API的许多部分都发生了变化。
我可以将pom.xml
更改为只使用1.56版本来运行您的代码:
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.56</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcmail-jdk15on</artifactId>
<version>1.56</version>
</dependency>
这是运行问题中代码的最小库集。当然,如果您的应用程序需要使用其他库(bcpg、bcprov-ext等等),您可以相应地包括它们(但我建议您选择versions>=1.55,以避免这些混合版本问题)
问题内容: 我正在尝试使用iText Java。当您运行示例“ 如何签名 ”时,会发生以下错误: 根据“ iText入门- 如何使用iText签名PDF” ,我必须使用BouncyCastle。 我从BouncyCastle下载页面 下载了文件bcprov-jdk15on-147.jar 。 并添加到项目中:Java Build Path / Libraries / Add External JA
问题内容: 我越来越: 我是否正确理解,这意味着doSmth()方法的返回类型为布尔值?如果为true,则实际上不存在这种方法,因为此方法返回一些Collection。但是另一方面,如果我调用此方法,则不会将其返回值分配给任何变量。我只是这样调用此方法: 任何想法为什么会发生此错误?所有必需的JAR文件都存在,并且此类中的所有其他方法似乎都存在。 问题答案: 看起来方法在编译期间存在于类路径中,但
我不明白,因为该方法存在于类中,而类是从提取到我的应用程序JAR中的。而且,我的其他库没有这个问题。 谢谢你抽出时间。
试图编译一个grails 1.3.7应用程序,并得到以下依赖项错误。有人帮忙吗? 我尝试将此添加到BuildConfig.groovy中,但没有成功: 插件{
我正在尝试升级到OGM 5.1.0.final,我也在使用Spring。pom如下-
问题内容: 我在尝试插入时收到以下错误: java.lang.NoSuchMethodError:org.hibernate.event.PreInsertEvent.getSource()Lorg / hibernate / event / EventSource; 我已经看到其他人由于hibernatejar中的不兼容而遇到了同样的问题,但是我相信我做对了(根据兼容性矩阵) 这是我的pom.x