Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export secret key from jck key store

We have a jck keystore (jceks) format containing a secret key. It was generated using keytool command

keytool -genseckey -alias mykey -keyalg AES -keysize 256 -storetype jceks -keystore mykeystore.jks

We need to share this with another application and they seem to have limitations in working with jck store. They are asking for the key to be exported and supplied to them.

We tried a few tools, but are not able to export the secret key. Is there a command or workaround to achieve this ?

like image 939
John Smith Avatar asked Feb 06 '23 10:02

John Smith


2 Answers

keytool doesn't support exporting of Secret Keys. You could use the KeyStore api to do this.

KeyStore ks = KeyStore.getInstance("JCEKS");
ks.load(new FileInputStream(new File("KEYSTORE_PATH")), "PASSWORD".toCharArray());

SecretKey key = (SecretKey) ks.getKey("ALIAS", "PASSWORD".toCharArray());

System.out.println(new String(Base64.encode(key.getEncoded())));
like image 113
always_a_rookie Avatar answered Mar 19 '23 08:03

always_a_rookie


KeyStore Explorer shows the key as a hex string if you double-click on it:

enter image description here

like image 39
Omikron Avatar answered Mar 19 '23 06:03

Omikron