Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Load input stream into keystore
- keystore.load(is, password.toCharArray());
- // List the aliases
- Enumeration enum = keystore.aliases();
- for (; enum.hasMoreElements(); ) {
- String alias = (String)enum.nextElement();
- // Does alias refer to a private key?
- boolean b = keystore.isKeyEntry(alias);
- // Does alias refer to a trusted certificate?
- b = keystore.isCertificateEntry(alias);
- }
- import java.io.FileInputStream;
- import java.security.Key;
- import java.security.KeyStore;
- import sun.misc.BASE64Encoder;
- public class DumpPrivateKey {
- /**
- * Provides the missing functionality of keytool
- * that Apache needs for SSLCertificateKeyFile.
- *
- * @param args <ul>
- * <li> [0] Keystore filename.
- * <li> [1] Keystore password.
- * <li> [2] alias
- * </ul>
- */
- static public void main(String[] args)
- throws Exception {
- if(args.length < 3) {
- throw new IllegalArgumentException("expected args: Keystore filename, Keystore password, alias, <key password: default same tha
- n keystore");
- }
- final String keystoreName = args[0];
- final String keystorePassword = args[1];
- final String alias = args[2];
- final String keyPassword = getKeyPassword(args,keystorePassword);
- KeyStore ks = KeyStore.getInstance("jks");
- ks.load(new FileInputStream(keystoreName), keystorePassword.toCharArray());
- Key key = ks.getKey(alias, keyPassword.toCharArray());
- String b64 = new BASE64Encoder().encode(key.getEncoded());
- System.out.println("-----BEGIN PRIVATE KEY-----");
- System.out.println(b64);
- System.out.println("-----END PRIVATE KEY-----");
- }
- private static String getKeyPassword(final String[] args, final String keystorePassword)
- {
- String keyPassword = keystorePassword; // default case
- if(args.length == 4) {
- keyPassword = args[3];
- }
- return keyPassword;
- }
- }
- javac -classpath .:commons-codec-1.4/commons-codec-1.4.jar DumpPrivateKey.java
- import java.io.FileInputStream;
- import java.security.Key;
- import java.security.KeyStore;
- //import sun.misc.BASE64Encoder;
- import org.apache.commons.codec.binary.Base64;
- public class DumpPrivateKey {
- /**
- * Provides the missing functionality of keytool
- * that Apache needs for SSLCertificateKeyFile.
- *
- * @param args <ul>
- * <li> [0] Keystore filename.
- * <li> [1] Keystore password.
- * <li> [2] alias
- * </ul>
- */
- static public void main(String[] args)
- throws Exception {
- if(args.length < 3) {
- throw new IllegalArgumentException("expected args: Keystore filename, Keystore password, alias, <key password: default same tha
- n keystore");
- }
- final String keystoreName = args[0];
- final String keystorePassword = args[1];
- final String alias = args[2];
- final String keyPassword = getKeyPassword(args,keystorePassword);
- KeyStore ks = KeyStore.getInstance("jks");
- ks.load(new FileInputStream(keystoreName), keystorePassword.toCharArray());
- Key key = ks.getKey(alias, keyPassword.toCharArray());
- //String b64 = new BASE64Encoder().encode(key.getEncoded());
- String b64 = new String(Base64.encodeBase64(key.getEncoded(),true));
- System.out.println("-----BEGIN PRIVATE KEY-----");
- System.out.println(b64);
- System.out.println("-----END PRIVATE KEY-----");
- }
- private static String getKeyPassword(final String[] args, final String keystorePassword)
- {
- String keyPassword = keystorePassword; // default case
- if(args.length == 4) {
- keyPassword = args[3];
- }
- return keyPassword;
- }
- }
- java -classpath .:commons-codec-1.4/commons-codec-1.4.jar DumpPrivateKey $HOME/.keystore changeit tomcat
- keytool -importkeystore -srckeystore keystore.jks
- -destkeystore intermediate.p12 -deststoretype PKCS12
- openssl pkcs12 -in intermediate.p12 -out extracted.pem -nodes
- char[] password = ...
- KeyStore anchors = ...
- Enumeration<String> aliases = anchors.aliases();
- while (aliases.hasMoreElements()) {
- String alias = aliases.nextElement();
- if (!anchors.isKeyEntry(alias))
- continue;
- Key key = anchors.getKey(alias, password);
- if ((key instanceof PrivateKey) && "PKCS#8".equals(key.getFormat())) {
- /* Most PrivateKeys use this format, but check for safety. */
- FileOutputStream os = new FileOutputStream(alias + ".key");
- os.write(key.getEncoded());
- os.flush();
- os.close();
- }
- }
Add Comment
Please, Sign In to add comment