从密钥库获取私钥 [英] Get privatekey from Keystore

查看:181
本文介绍了从密钥库获取私钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码从我在Windows中使用keytool创建的密钥库中导出证书和密钥:

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);  

但是它在最后一行失败,并显示无法检索私钥进行签名" 它可以检索clientcert,但是当尝试使用clientprivate时,它将失败.

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.

推荐答案

我的问题是,当我使用keytool生成密钥库时,它没有将PrivateKey创建为密钥条目.为了解决这个问题,我导入了.p12密钥库作为私钥,它可以正常工作.然后,我上面的原始代码成功了.

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.

这篇关于从密钥库获取私钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆