Search code examples
javakeytooljksp12

Convert jks to p12 in Java


Instead of using keytool in cmd or openssl, I want to convert a jks file to a p12 file in Java.

My code so far is this:

KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());

char[] password = "lol".toCharArray();
ks.load(null, password);

FileOutputStream fos = new FileOutputStream("C:\\Users\\Antonio\\Desktop\\jkstest\\test.jks");
ks.store(fos, password);
fos.close();

Thats how I create a jks file. But I did not find any information about how to convert it to anything. Who does know a solution? Thanks for every answer!


Solution

  • You have to enumerate aliases in the source KeyStore and do setEntry() on the target key store for each Entry that you get from the source key store.

    Also, as mentioned by Dave in the comment, use explicit getInstance("PKCS12") for the target key store.