JAVA中的加密私钥:java.security.InvalidKeyException

时间:2015-12-14 06:32:38

标签: java security private-key pkcs#8

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

下面给出的代码:

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)

1 个答案:

答案 0 :(得分:0)

SecretKeyFactory secFac = SecretKeyFactory.getInstance("PBEWithSHA1AndDESede")

PBEWithSHA1AndDESede不是Java支持的密码算法的名称。有关正确的列表,请参阅Standard Algorithm Names

可能你的意思是PBEWithHmacSHA1AndDESede