Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A portion of code originally from Example Depot for listing all of the aliases in a key store:</p> <pre><code> // Load input stream into keystore keystore.load(is, password.toCharArray()); // List the aliases Enumeration aliases = keystore.aliases(); for (; aliases.hasMoreElements(); ) { String alias = (String)aliases.nextElement(); // Does alias refer to a private key? boolean b = keystore.isKeyEntry(alias); // Does alias refer to a trusted certificate? b = keystore.isCertificateEntry(alias); } </code></pre> <p>The exporting of private keys came up on the <a href="http://forums.sun.com/thread.jspa?threadID=154587&amp;start=15&amp;tstart=0" rel="noreferrer">Sun forums</a> a couple of months ago, and <a href="http://forums.sun.com/profile.jspa?userID=505275" rel="noreferrer">u:turingcompleter</a> came up with a DumpPrivateKey class to stitch into your app.</p> <pre><code>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 &lt;ul&gt; * &lt;li&gt; [0] Keystore filename. * &lt;li&gt; [1] Keystore password. * &lt;li&gt; [2] alias * &lt;/ul&gt; */ static public void main(String[] args) throws Exception { if(args.length &lt; 3) { throw new IllegalArgumentException("expected args: Keystore filename, Keystore password, alias, &lt;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; } } </code></pre> <p>Note: this use Sun package, <a href="http://java.sun.com/products/jdk/faq/faq-sun-packages.html" rel="noreferrer">which is a "bad thing"</a>.<br> If you can download <a href="http://commons.apache.org/codec/index.html" rel="noreferrer">apache commons code</a>, here is a version which will compile without warning:</p> <pre><code>javac -classpath .:commons-codec-1.4/commons-codec-1.4.jar DumpPrivateKey.java </code></pre> <p>and will give the same result:</p> <pre><code>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 &lt;ul&gt; * &lt;li&gt; [0] Keystore filename. * &lt;li&gt; [1] Keystore password. * &lt;li&gt; [2] alias * &lt;/ul&gt; */ static public void main(String[] args) throws Exception { if(args.length &lt; 3) { throw new IllegalArgumentException("expected args: Keystore filename, Keystore password, alias, &lt;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; } } </code></pre> <p>You can use it like so:</p> <pre><code>java -classpath .:commons-codec-1.4/commons-codec-1.4.jar DumpPrivateKey $HOME/.keystore changeit tomcat </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload