java 从私钥和公钥创建密钥库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6252045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:02:29  来源:igfitidea点击:

Creating a keystore from private key and a public key

javakeystorepem

提问by ivar

I have java code that needs a keystore and I have privateKey.pem and bank.cer file. Private key would be to sign a value to bank and bank.cer to verify banks response. I can't find a way to put them into a keystore so my code would work.

我有需要密钥库的 Java 代码,我有 privateKey.pem 和 bank.cer 文件。私钥将签署一个值给银行和 bank.cer 以验证银行的响应。我找不到将它们放入密钥库的方法,以便我的代码可以工作。

Can it be done with keytool?

可以用keytool来完成吗?

回答by musiKk

From my understanding it's impossible to do this with keytoolalone. I use opensslfor preparation.

根据我的理解,keytool单独这样做是不可能的。我openssl用于准备。

Suppose the key is in file keyand the certificate is in a file cert. You have to create a PKCS12 file that contains both (because keytoolcan handle PKCS12 and JKS and I don't know if anything else):

假设密钥在文件中key,证书在文件中cert。您必须创建一个包含两者的 PKCS12 文件(因为keytool可以处理 PKCS12 和 JKS,我不知道是否还有其他内容):

openssl pkcs12 -inkey key -in cert -export -out keys.pkcs12

Now you can import that into a keystore:

现在您可以将其导入密钥库:

keytool -importkeystore -srckeystore keys.pkcs12 -srcstoretype pkcs12 -destkeystore mykeystore

This approach worked for me where everything else failed.

这种方法对我有用,其他一切都失败了。