2015-12-14 73 views
0

嗨我正在嘗試讀取JAVA中的加密私鑰。使用的密鑰是帶有加密算法的PKCS#8,作爲具有SHA-1和2個密鑰DESede的PBE。用JAVA加密的私鑰:java.security.InvalidKeyException

下面的代碼給出:

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

爲什麼你認爲Cipher.getInstance(「1.2.840.113549.1.12.1.4」)不應該拋出異常?密碼被稱爲'AES'。 Blowfish,DES,DESede,PBEWithSHA1andDESede,RC2,RSA,... – Andreas

回答

0
SecretKeyFactory secFac = SecretKeyFactory.getInstance("PBEWithSHA1AndDESede") 

PBEWithSHA1AndDESede是不支持Java加密算法的名稱。有關正確的列表,請參見Standard Algorithm Names

可能你的意思是PBEWithHmacSHA1AndDESede

相關問題