Java Keystores

Coverting between keystore formats

Sample code attached where IBM/AIX server had a pcks12 format keystore, and we needed the same key but in windows/jks. The problem is that windows java could not read the pcks12 format, but the IBM/AIX java could ready both, so the conversion code was run on that machine.

       KeyStore kspkcs12=KeyStore.getInstance("PKCS12");
       String inputPassword = "staging";
       kspkcs12.load(new FileInputStream("staging.p12"), inputPassword.toCharArray());
       KeyStore ksjks=KeyStore.getInstance("JKS");
       ksjks.load(null, inputPassword.toCharArray());
       Certificate c[]=kspkcs12.getCertificateChain("staging");
       Key key=kspkcs12.getKey("staging",inputPassword.toCharArray());

       String outputPassword = "staging";
       ksjks.setKeyEntry("staging", key, outputPassword.toCharArray(),c);
       OutputStream outJKS = new FileOutputStream("staging.jks");, outputPassword.toCharArray());

Published: Sunday, 14 January 2007