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

马克西姆

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

UnitTest路径=“ /..../资源/..../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 );

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何从文件加载RSA私钥

无法加载密钥文件。无法加载文件'id_rsa.pub'或不包含私钥。-文件Zilla

使用通过Heroku工头从.env文件加载的RSA私钥对JWT签名时出错

为什么PyCharm无法从指定目录加载文件/模块?

为什么用私钥加密时不能使用RSA OAEP填充?

为什么从类和类加载器加载文件的结果不一致

为什么在创建 CSR 时使用私钥?

为什么不能从名为“用户”的文件夹中加载文件?

使用SSH.NET从配置字符串加载SSH私钥时,出现“ Renci.SshNet.Common.SshException:私钥文件无效”

为什么我要使用python硒从Chrome下载文件时为什么出现错误

无法加载文件或程序集'System.Data.SQLite,版本= 1.0.109.0-为什么当我引用1.0.109.1时会搜索版本1.0.109.0

为什么Gembox电子表格在调用Excelfile.save时崩溃:FileNotFoundException无法加载文件或程序集System.Security.Permissions

scandir() 以什么顺序加载文件?

加载文件时的核心转储

加载文件时转换为灰度

Emacs:保存时加载文件.emacs

在MATLAB中加载文件时出错

在Unity中加载文件时等待

为什么我的自动加载文件夹中的脚本没有在Vim中执行?

为什么我的下载文件夹需要这么长时间才能加载?

PuTTY密钥生成器在加载PEM文件时显示“无法加载私钥(不是私钥)”

什么原因导致 Revit 中的“加载文件时发生架构冲突”?

paramiko-“无效的OpenSSH / RSA / ...私钥”错误

为什么DataNode不能下载文件?

computeRsaSha256Signature()返回无效的参数:当密钥为公钥或rsa私钥时,密钥错误

为什么pycharm在加载.joblib文件时出错

为什么在使用ajax上传文件时重新加载页面?

为什么每次上载文件时Google Chrome浏览器都会冻结?

从 URL 下载文件时,为什么要读入字节数组?