【发布时间】: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