【问题标题】:Encrypted Private key in JAVA: java.security.InvalidKeyException [closed]JAVA中的加密私钥:java.security.InvalidKeyException [关闭]
【发布时间】:2016-03-19 13:53:50
【问题描述】:

您好,我正在尝试读取 JAVA 中的加密私钥。正在使用的密钥是 PKCS#8,加密算法为 PBE,带有 SHA-1 和 2 密钥 DESede。

代码如下:

EncryptedPrivateKeyInfo encryptPKInfo = new EncryptedPrivateKeyInfo("RSA",readFileBytes(filename));
Cipher cipher = Cipher.getInstance(encryptPKInfo.getAlgName());
PBEKeySpec pbeKeySpec = new PBEKeySpec("pwd".toCharArray());
SecretKeyFactory secFac = SecretKeyFactory.getInstance("PBEWithSHA1AndDESede");
Key pbeKey = secFac.generateSecret(pbeKeySpec);
AlgorithmParameters algParams = encryptPKInfo.getAlgParameters();
cipher.init(Cipher.DECRYPT_MODE, pbeKey,algParams);
KeySpec pkcs8KeySpec = encryptPKInfo.getKeySpec(cipher);
KeyFactory kf = KeyFactory.getInstance("RSA");
return kf.generatePrivate(pkcs8KeySpec);

我正面临下面提到的错误:

Exception in thread "main" java.security.InvalidKeyException: No installed provider supports this key: com.sun.crypto.provider.PBEKey
at javax.crypto.Cipher.chooseProvider(Cipher.java:888)
at javax.crypto.Cipher.init(Cipher.java:1507)
at javax.crypto.Cipher.init(Cipher.java:1438)
at com.abc.utils.CertificateUtil.readEncryptedPrivateKey(CertificateUtil.java:62)
at com.abc.test.Test1.main(Test1.java:16)

【问题讨论】:

  • 为什么你认为Cipher.getInstance("1.2.840.113549.1.12.1.4") 不应该抛出那个异常?密码被称为AES 之类的东西。 Blowfish, DES, DESede, PBEWithSHA1AndDESede, RC2, RSA, ...

标签: java security private-key pkcs#8


【解决方案1】:
SecretKeyFactory secFac = SecretKeyFactory.getInstance("PBEWithSHA1AndDESede")

PBEWithSHA1AndDESede 不是 Java 支持的密码算法的名称。正确列表见Standard Algorithm Names

你的意思可能是PBEWithHmacSHA1AndDESede

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-23
    • 2013-02-08
    • 2018-07-05
    • 2021-09-22
    • 1970-01-01
    • 2019-03-09
    • 2011-10-17
    • 1970-01-01
    相关资源
    最近更新 更多