spring 在将.pem密钥加载到java中时出错,但仅在运行jar时出错

sqserrrh  于 2023-04-19  发布在  Spring
关注(0)|答案(1)|浏览(168)

我很感激任何帮助,我已经被困在这比我应该有更长的时间。
java 17 mvn 3.9.1 spring 3.0.4
因此,我需要将.pem密钥加载到java中以解密我使用该对加密的内容。当从intellij运行项目时,它工作正常。但当从使用'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

我读到的每一处都说要么设置安全提供程序,我做了。我甚至放了一个print来检查BC是否在那里,它是。或者BC库不在JAR中,但它们在,我用jar tf app.jar检查。
使用mvn着色器插件强制所有库在另一个jar中,没有工作。
尝试让intellij创建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>

谢谢!

6jjcrrmo

6jjcrrmo1#

好吧,结果是BouncyCastle数字签名在将它们包含在jar中时被损坏了,所以我不得不将它们从jar中排除,并将它们作为变量添加到jar命令中,不知道为什么第一次没有工作。
java -cp“path/to/lib/bcprov-jdk15on-1.70.jar:path/to/lib/bcpkix-jdk15on-1.70.jar:path/to/jar/artifact.jar”com.project.main.class

相关问题