Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a keystore from private key and a public key

Tags:

java

pem

keystore

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.

Can it be done with keytool?

like image 263
ivar Avatar asked Jun 06 '11 12:06

ivar


1 Answers

From my understanding it's impossible to do this with keytool alone. I use openssl for preparation.

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

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.

like image 174
musiKk Avatar answered Sep 29 '22 00:09

musiKk