Exporting the Private Key from a JKS keystore
Java - - Posted on January, 19 at 3:22 pm
A common problem faced when moving certificates and keys from tomcat to Apache web server is that keytool does not allow you to export the private key in the format that apache’s modssl module requires. Mark Foster’s post and Andrew Morrow’s post contains valuable information on how to export a key from a JKS keystore.
Here is a summary of the steps needed to export a private key
Download ExportPrivateKey.zip
Invoke
This would export the key to PKCS #8 PEM format. Now run openssl to convert it to the format apache modssl expects the file in
The java code for exporting the private key in PKCS #8 format
-
import java.io.File;
-
import java.io.FileInputStream;
-
import java.io.FileWriter;
-
import java.security.Key;
-
import java.security.KeyPair;
-
import java.security.KeyStore;
-
import java.security.KeyStoreException;
-
import java.security.NoSuchAlgorithmException;
-
import java.security.PrivateKey;
-
import java.security.PublicKey;
-
import java.security.UnrecoverableKeyException;
-
import java.security.cert.Certificate;
-
-
import sun.misc.BASE64Encoder;
-
-
public class ExportPrivateKey {
-
private File keystoreFile;
-
private String keyStoreType;
-
private char[] password;
-
private String alias;
-
private File exportedFile;
-
-
try {
-
}
-
}
-
return null;
-
}
-
-
BASE64Encoder encoder=new BASE64Encoder();
-
fw.write(“—–BEGIN PRIVATE KEY—–\n“);
-
fw.write(encoded);
-
fw.write(“\n“);
-
fw.write(“—–END PRIVATE KEY—–”);
-
fw.close();
-
}
-
-
-
ExportPrivateKey export=new ExportPrivateKey();
-
export.keyStoreType=args[1];
-
export.password=args[2].toCharArray();
-
export.alias=args[3];
-
export.export();
-
}
-
}
Posted in Java |





February 16th, 2006 at 2:34 pm
This was very usefull. I also would like to know how to import public and private keys as well.
February 21st, 2006 at 11:22 am
This should work ..
Key key={load key from file}…
Certificate cert={load cert from file} …
KeyStore keyStore = KeyStore.getInstance(”JKS”);
keyStore.load(null, “password”.toCharArray());
keyStore.setKeyEntry(”alias”, key, “password”.toCharArray(), cert);
May 18th, 2006 at 5:18 am
Great website! Bookmarked! I am impressed at your work!
May 23rd, 2006 at 6:18 am
Thanks!!! Saved my life!
September 29th, 2006 at 1:30 pm
Thanks for publishing this - it was very useful!
December 28th, 2006 at 12:54 pm
Hi. I know this *should* work, and it has worked before, but suddenly I’m getting this every time:
Exception in thread “main” java.lang.NullPointerException
at ExportPrivateKey.export(ExportPrivateKey.java:43)
at ExportPrivateKey.main(ExportPrivateKey.java:61)
I’ve found other similar export implementations, and they all fail on any line that calls “getPrivate().” Any ideas? This is killing me, and I seem to be the only person in the world to suddenly have this problem. haha!
Great work, though, and thanks.
June 12th, 2007 at 2:56 pm
KeyStore.PrivateKeyEntry
KeyStore.SecretKeyEntry
KeyStore.TrustedCertificateEntry
is your code only useful for the first method?
June 29th, 2007 at 12:18 pm
[…] or removed and it consequently becomes difficult to find. The following instructions are from this page, which originally got them from here. Here is a summary of the steps needed to export a […]
October 13th, 2007 at 3:45 am
This is excellent! I really needed this tonight. Thank you for posting.
November 30th, 2007 at 4:11 pm
How do I export a public key from a keystore? Can I use the same program by just changing the APIS to getpublic key. Also how I convert a generated public key to OPENSSL complaint format. Appreciate any help on this!
January 21st, 2008 at 6:16 pm
A question:
Why do you:
{code}
PrivateKey privateKey=keyPair.getPrivate();
String encoded=encoder.encode(privateKey.getEncoded());
{code}
And then suggest to:
{{openssl pkcs8 -inform PEM -nocrypt -in exported-pkcs8.key -out exported.key}}
Isn’t there a simple way of doing that, by directly recover the uncrypted key from the privateKey object ?
January 21st, 2008 at 7:01 pm
This is because apache expects the key to be in PKCS8 format, which is different from what JKS stores, so we need openssl to convert to PKCS8
February 6th, 2008 at 1:18 am
Has anybody tried and got it to work in real world? I’m curious. I didn’t seem to have any success myself.
Thanks,
jp
February 29th, 2008 at 12:20 am
I’ve failed to get the code to work. Keep having “Password tampered” error. And then I found that the code assume that the storepass and keypass are the same.
March 12th, 2008 at 3:44 pm
It worked for me. Thanks!
April 11th, 2008 at 6:27 am
Dear Anand,
Your program made our day. Thanks a lot.
Regards,
Prince Nishchal.N.E
April 15th, 2008 at 8:18 am
Does someone know how to convert the PKCS#8 PEM format to SSL PEM format directly in Java (so without the openssl tool)?
May 4th, 2008 at 1:21 am
Thanks for your help!
It really helps me!
The error of tempered keystore sais that you lanch program with wrong keystore name.
If we get NullPointer error, it means that Password for keystore is correct, but alias (with name you entered) is not in this keystore!
You need to find out a valid alias-name.
И для русских:
прога действительно работает!!!
Если вылезает ошибка tempered keystore, это значит, что вы указали неправильный пароль для keystore.
Если вылезает ошибка NullPointer error, значит пароль правильный, но вы указали неправильное имя alias
June 18th, 2008 at 1:39 pm
Well, how about only using keytool and openssl? Here’s how you do it: http://www.swview.org/node/191