在java中加载.pem密钥时出现错误,但只有在运行jar时才会出现错误



我感谢任何帮助,我已经被困在这个比我应该有更长时间。
java 17 mvn 3.9.1 spring 3.0.4
所以,我需要加载。pem密钥到java中解密我用这对加密的东西。当从intelllij运行项目时,它工作得很好。但是,当使用' mvn clean install/package '从jar中执行时,它会失败。它运行了但是当它到达loadIntoJavaPart

public PrivateKey loadPrivateKeyFromFile(String privateKeyPath)
throws IOException, NoSuchAlgorithmException, InvalidKeySpecException,
OperatorCreationException, PKCSException
{

try (FileReader fileReader = new FileReader(privateKeyPath);
PEMParser pemParser = new PEMParser(fileReader)) {
Object pemObject = pemParser.readObject();
if (pemObject instanceof PKCS8EncryptedPrivateKeyInfo) {
PKCS8EncryptedPrivateKeyInfo encryptedPrivateKeyInfo = (PKCS8EncryptedPrivateKeyInfo) pemObject;
InputDecryptorProvider decryptorProvider = new JceOpenSSLPKCS8DecryptorProviderBuilder().build("pass".toCharArray());

*Tried putting this in several places, this is just the last one I tried*
Security.addProvider(new BouncyCastleProvider());

ASN1OctetString asn1OctetString= encryptedPrivateKeyInfo.decryptPrivateKeyInfo
(decryptorProvider).getPrivateKey();

byte[] privateKeyBytes = asn1OctetString.getOctets();
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return keyFactory.generatePrivate(keySpec);
} else {
throw new IllegalArgumentException("Unsupported PEM object: " + pemObject);
}
}
}

ASN1OctetString asn1OctetString= encryptedPrivateKeyInfo.decryptPrivateKeyInfo
(decryptorProvider).getPrivateKey();

抛出:

org.bouncycastle.pkcs.PKCSException: unable to read encrypted data: 1.2.840.113549.1.5.13 not available: Cannot find any provider supporting 1.2.840.113549.3.7

我读过的所有地方都说要么设置安全提供程序,我就是这样做的。我甚至做了指纹检查,确认BC在那里,确实是。或者BC库不在JAR中,但它们是,我检查了JAR tf app.jar。
使用mvn shader插件强制所有库在另一个jar中,没有工作。
尝试使用intelllij创建jar并使用它,但它无法找到主类,我检查了在manifest-inf中列出的主类。

所以我已经没有主意了。我真的很感谢任何人的帮助。

POM.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.micro</groupId>
<artifactId>encrypt</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>encryption</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.santuario</groupId>
<artifactId>xmlsec</artifactId>
<version>2.1.6</version>
</dependency>
<dependency>
<groupId>javax.xml.stream</groupId>
<artifactId>stax-api</artifactId>
<version>1.0-2</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.70</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
<version>1.70</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.15</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>io.projectreactor.netty</groupId>
<artifactId>reactor-netty</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadeTestJar>true</shadeTestJar>
<shadedClassifierName>SHADED</shadedClassifierName>
<shadedArtifactAttached>true</shadedArtifactAttached>
</configuration>
</execution>
</executions>

</plugin>
</plugins>
</build>
</project>

谢谢!

好的,所以事实证明BouncyCastle数字签名在包括它们在jar中时被破坏了,所以我不得不将它们从jar中排除,并将它们作为jar命令中的变量添加,不知道为什么它第一次没有工作。

java -cp "path/to/lib/bcpkix-jdk15on-1.70.jar:path/to/lib/bcpkix-jdk15on-1.70.jar:path/to/jar/artifact.jar"com.project.main.class

相关内容

  • 没有找到相关文章

最新更新