42 lines
1.2 KiB
Java
42 lines
1.2 KiB
Java
|
|
package com.de22.kstools;
|
||
|
|
|
||
|
|
import java.security.cert.Certificate;
|
||
|
|
import java.security.*;
|
||
|
|
import java.io.File;
|
||
|
|
import java.io.FileInputStream;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Exports the private key from the keystore in a pkcs12 format.
|
||
|
|
*/
|
||
|
|
public class ExportPrivateKey {
|
||
|
|
public static void main(String args[]) throws Exception {
|
||
|
|
if(args.length < 4) {
|
||
|
|
System.out.println("Expected parameters: keystore-path, alias, store-password, key-password.");
|
||
|
|
}//if//
|
||
|
|
else {
|
||
|
|
File keystoreFile = new File(args[0]);
|
||
|
|
String aliasName = args[1];
|
||
|
|
String storePass = args[2];
|
||
|
|
String keyPass = args[3];
|
||
|
|
KeyStore keystore = KeyStore.getInstance("JKS");
|
||
|
|
Key key;
|
||
|
|
|
||
|
|
keystore.load(new FileInputStream(keystoreFile), storePass.toCharArray());
|
||
|
|
key = keystore.getKey(aliasName, keyPass.toCharArray());
|
||
|
|
|
||
|
|
if(key instanceof PrivateKey) {
|
||
|
|
char[] encodedKey = Base64Coder.encode(((PrivateKey) key).getEncoded());
|
||
|
|
|
||
|
|
System.out.println("-----BEGIN PRIVATE KEY-----");
|
||
|
|
|
||
|
|
for(int i = 0; i < encodedKey.length; i += 76) {
|
||
|
|
char[] x = new char[Math.min(encodedKey.length - i, 76)];
|
||
|
|
System.arraycopy(encodedKey, i, x, 0, x.length);
|
||
|
|
System.out.println(x);
|
||
|
|
}//for//
|
||
|
|
|
||
|
|
System.out.println("-----END PRIVATE KEY-----");
|
||
|
|
}//if//
|
||
|
|
}//else//
|
||
|
|
}//main()//
|
||
|
|
}//ExportPrivateKey//
|