Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 61293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:17:47+00:00 2026-05-10T18:17:47+00:00

How do I list and export a private key from a keystore?

  • 0

How do I list and export a private key from a keystore?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T18:17:48+00:00Added an answer on May 10, 2026 at 6:17 pm

    A portion of code originally from Example Depot for listing all of the aliases in a key store:

        // 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);     } 

    The exporting of private keys came up on the Sun forums a couple of months ago, and u:turingcompleter came up with a DumpPrivateKey class to stitch into your app.

    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;     } } 

    Note: this use Sun package, which is a ‘bad thing’.
    If you can download apache commons code, here is a version which will compile without warning:

    javac -classpath .:commons-codec-1.4/commons-codec-1.4.jar DumpPrivateKey.java 

    and will give the same result:

    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;     } } 

    You can use it like so:

    java -classpath .:commons-codec-1.4/commons-codec-1.4.jar DumpPrivateKey $HOME/.keystore changeit tomcat 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider: List<String> someList = new ArrayList<>(); // add "monkey", "donkey", "skeleton key" to someList
My list (@degree) is built from a SQL command. The NVL command in the
List Comprehension for me seems to be like the opaque block of granite that
List Comprehension is a very useful code mechanism that is found in several languages,
List<String> nameList = new List<String>(); DropDownList ddl = new DropDownList(); List is populated here,
List<tinyClass> ids = new List<tinyClass(); ids.Add(new tinyClass(1, 2)); bool b = ids.IndexOf(new tinyClass(1, 2))
List comprehensions can be useful in certain situations, but they can also be rather
Any list of Testing frameworks for ActionScript 2.0/3.0 around there?
The list sort() method is a modifier function that returns None . So if
In a list of transaction objects, I am attempting to group by BatchNo then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.