我正在创建一个 java独立应用程序,它使用 bouncycastle。一切都在eclipse中运行。我正在创建一个 jar with dependencies ,如this。
当我使用“java -jar myapp-0.0.1-SNAPSHOT-jar-with-dependencies.jar”运行应用程序时。
我收到以下错误:
java.io.IOException: exception encrypting data - java.lang.SecurityException: JCE cannot authenticate the provider BC
我的代码:
Security.addProvider(new BouncyCastleProvider());
String keystoreDirectory = "C:/myapp/security";
File file = new File(keystoreDirectory + "/" + PRIVATE_KEY_FILE);
if (!file.isFile()) {
try {
Configuration idOrganization = configurationBoundary.find(Configuration.ID_ORGANIZATION);
KeyStore store = KeyStore.getInstance("PKCS12", SECURITY_PROVIDER);
char[] password = KEY.toCharArray();
store.load(null, password);
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", SECURITY_PROVIDER);
keyPairGenerator.initialize(2048);
KeyPair pair = keyPairGenerator.generateKeyPair();
X500Name issuer = new X500Name("CN=" + idOrganization.getProperty());
BigInteger serial = BigInteger.valueOf(new SecureRandom().nextLong());
Date notBefore = new Date(System.currentTimeMillis() - 10000);
Date notAfter = new Date(System.currentTimeMillis() + 24L * 3600 * 1000 * 365);
X500Name subject = new X500Name("CN=" + idOrganization.getProperty());
SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfo.getInstance(pair.getPublic().getEncoded());
X509v3CertificateBuilder builder = new X509v3CertificateBuilder(issuer, serial, notBefore, notAfter, subject, publicKeyInfo);
ContentSigner sigGen = new JcaContentSignerBuilder("SHA256WithRSAEncryption").setProvider(SECURITY_PROVIDER).build(pair.getPrivate());
X509Certificate cert = new JcaX509CertificateConverter().setProvider(SECURITY_PROVIDER).getCertificate(builder.build(sigGen));
store.setKeyEntry(idOrganization.getProperty(), pair.getPrivate(), null, new java.security.cert.Certificate[]{cert});
try (FileOutputStream fos = new FileOutputStream(file)) {
store.store(fos, password); //Error here
}
} catch (Exception ex) {
logger.error("Keystore creation error", ex);
}
}
有什么想法吗? 感谢。
答案 0 :(得分:7)
Bouncycastle jar必须签名,不能放在胖罐里面。 您可以单独发货并使用maven-shade-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<artifactSet>
<excludes>
<exclude>org.bouncycastle:*:*:*</exclude>
</excludes>
</artifactSet>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>com.example.Main</Main-Class>
<Class-Path>. ./lib/bcprov-jdk16-1.46.jar</Class-Path>
</manifestEntries>
</transformer>
</transformers>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>fat</shadedClassifierName>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
在ManifestResourceTransformer中,您可以为bouncycastle jar依赖项定义类路径。
答案 1 :(得分:1)
不幸的是,JCE要求JAR与Bouncy Castle签名并创建可执行的jar废墟。准备单独运送BouncyCastle罐。
你实际上可以使用Spring Boot或类似的东西 - 它将jar嵌入fat jar中,然后解压缩并加载它们。或类似Spring Boot的东西。
答案 2 :(得分:0)
答案 3 :(得分:-2)
问题是BouncyCastle jar已签名,必须保持原样。对我来说,包括Maven中的依赖关系,范围是&#39;提供&#39;工作
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.55</version>
<scope>provided</scope>
</dependency>