String keyStorePath = "2keystore.jks";
String keyStoreType = "JKS";
String keyStorePassword = "mypass";
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
InputStream ksis = ClassLoader.getSystemResourceAsStream(keyStorePath);
keyStore.load(ksis, keyStorePassword.toCharArray());
ksis.close();
// Above seems to work fine
KeyManagerFactory kmf =
KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(keyStore, keyStorePassword.toCharArray());
// Above throws exception:
// UnrecoverableKeyException: Cannot recover key
然而,使用密鑰工具,我可以查看2keystore.jks
內容像這樣:的KeyManagerFactory無法恢復的關鍵,但它在密鑰工具打開
keytool -list -v -keystore 2keystore.jks | more
Enter keystore password: mypass
Keystore type: JKS
Keystore provider: SUN
Your keystore contains 3 entries
Alias name:
...
任何想法嘗試加載這個時候可能是什麼問題keystore放入KeyManagerFactory
?
感謝
您確定您的密鑰庫是您期望它在類路徑中的位置嗎?例如'ksis'不爲null?您可以在加載密鑰庫別名時列出密鑰庫別名嗎? – Bruno 2012-08-10 16:34:46
keystore'ksis'不爲空......它加載。我想我找到了問題...密鑰庫加載了整個密鑰庫的密碼,但KeyManagerFactory需要密鑰庫中包含的密鑰對的密碼不同。 – Cuga 2012-08-10 17:09:30
KMF密碼是保護密鑰本身的密碼,而KeyStore.load(...)使用的密碼保護整個密鑰庫。這些密碼是否有所不同取決於您構建密鑰庫的方式。 – Bruno 2012-08-10 17:34:19