//ExportPriv.java
//A short utility program to extract private keys from a Java JKS keystore
import sun.misc.BASE64Encoder;
import java.security.cert.Certificate;
import java.security.*;
import java.io.File;
import java.io.FileInputStream;
class ExportPriv {
public static void main(String args[]) throws Exception{
for (int i = 0; i < args.length; i++) {
System.err.println(i + ": "+ args[i]);
}
if (args.length < 2) {
System.err.println("Usage: java ExportPriv <keystore> <keystorepassword> <alias> <aliaspassword>");
System.exit(1);
}
ExportPriv myep = new ExportPriv();
myep.doit(args[0], args[1], args[2], args[3]);
}
public void doit(String fileName, String keystorepass, String aliasName, String aliaspass) throws Exception{
KeyStore ks = KeyStore.getInstance("JKS");
char[] keyStorePassPhrase = keystorepass.toCharArray();
BASE64Encoder myB64 = new BASE64Encoder();
File certificateFile = new File(fileName);
ks.load(new FileInputStream(certificateFile), keyStorePassPhrase);
char[] aliasPassPhrase = aliaspass.toCharArray();
KeyPair kp = getPrivateKey(ks, aliasName, aliasPassPhrase);
PrivateKey privKey = kp.getPrivate();
String b64 = myB64.encode(privKey.getEncoded());
System.out.println("-----BEGIN PRIVATE KEY-----");
System.out.println(b64);
System.out.println("-----END PRIVATE KEY-----");
}
public KeyPair getPrivateKey(KeyStore keystore, String alias, char[] password) {
try {
// Get private key
Key key = keystore.getKey(alias, password);
if (key instanceof PrivateKey) {
// Get certificate of public key
Certificate cert = keystore.getCertificate(alias);
// Get public key
PublicKey publicKey = cert.getPublicKey();
// Return a key pair
return new KeyPair(publicKey, (PrivateKey)key);
}
} catch (UnrecoverableKeyException e) {
} catch (NoSuchAlgorithmException e) {
} catch (KeyStoreException e) {
}
return null;
}
}
Lets run the above program and you will see similar results like below showing the private key in PEM format. D:\oracle\WLS1036\user_projects\domains\expirationcheck>java ExportPriv D:\oracle\WLS1036\wlserver_10.3\server\lib\DemoIdentity.jks DemoIdentityKeyStorePassPhrase Demoidentity DemoIdentityPassPhrase 0: D:\oracle\WLS1036\wlserver_10.3\server\lib\DemoIdentity.jks 1: DemoIdentityKeyStorePassPhrase 2: demoidentity 3: DemoIdentityPassPhrase -----BEGIN PRIVATE KEY----- MIIBVgIBADANBgkqhkiG9w0BAQEFAASCAUAwggE8AgEAAkEA5it9l23EOP14pWYcN0jwO2jzKggO S/lg7RGmt3YRzj75TdPsbJGl/VaoqXQlR6VR38OR04X+LirkVmLtPSuTjQIDAQABAkBYYm7xzTbC Y6MGcHV5TUripWRqyXkj93kez95jKLWhqpZaMC/nzl0al9I+4460zGN9JdrfT8KZxdqjT6hhKpBJ AiEA/ZmAMYLPjgBwpkQoiFf/bmXJWqTA1ckcC5OxDBCG72sCIQDoWTeSYtktCP6Jaumdn82uKDz8 wrY+8REpb8QXZ9Ee5wIhAKvIE8715fQxlhz6JF6fBRZZ+nfdDAaEkOvConYip9R7AiEA6Bxodaam CmVX0nOpmbuQv5CXDHJ/mXU9jcWHgVJHCLUCIQCO/Xy78hex2kBOsnaAIcjjV3UTnn+ZfjXteZl6 Ls3xOQ== -----END PRIVATE KEY----- or redirect it to a file D:\oracle\WLS1036\user_projects\domains\expirationcheck>java ExportPriv D:\oracle\WLS1036\wlserver_10.3\server\lib\DemoIdentity.jks DemoIdentityKeyStorePassPhrase Demoidentity DemoIdentityPassPhrase > private.key 0: D:\oracle\WLS1036\wlserver_10.3\server\lib\DemoIdentity.jks 1: DemoIdentityKeyStorePassPhrase 2: demoidentity 3: DemoIdentityPassPhrase