为什么加载文件时 RSA 私钥无效?

Why Invalid RSA private key when load file?

使用 ClassLoader.class 加载我的密钥时出现错误,为什么?

UnitTest 路径 = "/..../resource/..../key.der

版本部署路径=classLoader.getResource(AESKeyPath);

(aESKeyPath = "key.der")

private void loadKey(final String AESKeyPath, final String privateKeyFileDerPath) {

    Properties prop = new Properties();
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    InputStream input = classLoader.getResourceAsStream("foo.properties");
    prop.load(input);
    final String classpath = prop.getProperty("test.foo");

    File aESKeyFile = null;
    File privateKeyFile = null;

    // My version deployed using the if.
    if (classpath.equals("local")) {
        URL aesKeyPath = classLoader.getResource(AESKeyPath);
        if (aesKeyPath != null) {
            aESKeyFile = new File(aesKeyPath.toURI());
        }

        URL privateKeyURL = classLoader.getResource(privateKeyFileDerPath);
        if (privateKeyURL != null) {
            privateKeyFile = new File(privateKeyURL.toURI());
        }

     // The tests used the else
    } else {
        aESKeyFile = new File(AESKeyPath); 
        privateKeyFile = new File(privateKeyFileDerPath);
    }

    byte[] encodedKey = new byte[(int) privateKeyFile.length()];
    new FileInputStream(privateKeyFile).read(encodedKey);

    PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedKey);
    KeyFactory kf = KeyFactory.getInstance("RSA");
    PrivateKey pk = kf.generatePrivate(privateKeySpec); <---- Error

    pkCipher.init(Cipher.DECRYPT_MODE, pk);
    aesKey = new byte[AES_Key_Size / 8];
    CipherInputStream is = new CipherInputStream(new FileInputStream(AESKeyFile), pkCipher);
    is.read(aesKey);
    aeskeySpec = new SecretKeySpec(aesKey, "AES");
}

我的单元测试通过了,但是当我部署并使用“ClassLoader”加载文件时,出现了这个错误:

Caused by: java.security.InvalidKeyException: Invalid RSA private key
at sun.security.rsa.RSAPrivateCrtKeyImpl.parseKeyBits(RSAPrivateCrtKeyImpl.java:206) ~[na:1.8.0_40]
at sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:342) ~[na:1.8.0_40]
at sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:356) ~[na:1.8.0_40]
at sun.security.rsa.RSAPrivateCrtKeyImpl.<init>(RSAPrivateCrtKeyImpl.java:91) ~[na:1.8.0_40]
at sun.security.rsa.RSAPrivateCrtKeyImpl.newKey(RSAPrivateCrtKeyImpl.java:75) ~[na:1.8.0_40]
at sun.security.rsa.RSAKeyFactory.generatePrivate(RSAKeyFactory.java:316) ~[na:1.8.0_40]
at     sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:213) ~    [na:1.8.0_40]
    ... 92 common frames omitted
Caused by: java.io.IOException: DER input, Integer tag error
at sun.security.util.DerInputStream.getBigInteger(DerInputStream.java:180) ~[na:1.8.0_40]
at     sun.security.rsa.RSAPrivateCrtKeyImpl.getBigInteger(RSAPrivateCrtKeyImpl.java:214) ~[na:1.8.0_40]
at     sun.security.rsa.RSAPrivateCrtKeyImpl.parseKeyBits(RSAPrivateCrtKeyImpl.java:198    ) ~[na:1.8.0_40]

我比较前10个字节是相等的。最后一个也是一样的尺寸。

我使用以下命令生成密钥:

openssl genrsa -out private.pem 2048
openssl pkcs8 -topk8 -in private.pem -outform DER -out private.der -nocrypt
openssl rsa -in private.pem -pubout -outform DER -out public.der

我的问题是 Maven。 Maven 编码我的密钥。我通过在 pom.xml 中为资源添加过滤器解决了我的问题。我更改了我的代码。

    <resources>
        <resource>
            <directory>src/main/resources/foo</directory>
            <filtering>true</filtering>
            <excludes>
                <exclude>**/key/*</exclude>
            </excludes>
        </resource>
        <resource>
            <directory>src/main/resources/foo/key</directory>
        </resource>
    </resources>

String keyDerPath = getClass().getResource(privateKeyFileDerPath).getFile();
String AESKeyPathResource = getClass().getResource(AESKeyPath).getFile();
aESKeyFile = new File(AESKeyPathResource );
privateKeyFile = new File(keyDerPath );