Get privatekey from Keystore

2019-06-10 02:45发布

I have the following code to export certificates and keys from a keystore, that I created with keytool in Windows:

final KeyStore keystore = KeyUtil.loadKeystore("keystore.jks", "pass");  
UserInfo userinfo = new UserInfo(WSusername, WSpassword);  
X509Certificate clientcert = KeyUtil.getCertificate(CLIENT_KEY_ALIAS, keystore);  
X509Certificate servercert = KeyUtil.getCertificate(SERVER_KEY_ALIAS, keystore);  
PrivateKey clientprivate = KeyUtil.getPrivateKey(CLIENT_KEY_ALIAS, CLIENT_KEY_PASSWORD, keystore);  

Yet it fails at the last line with "unable to retrieve private key for signing" It can retrieve the clientcert, but when it tries clientprivate, it fails.

标签: java keystore
1条回答
何必那么认真
2楼-- · 2019-06-10 03:16

My problem was that when I generated the keystore with keytool, it did not create the PrivateKey as a key entry. To fix this, I imported the .p12 keystore as the private key, which worked. My original code above then worked.

查看更多
登录 后发表回答