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

Ask A Question

Stats

  • Questions 208k
  • Answers 208k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The MVC Pattern is the best thing going for web… May 12, 2026 at 9:37 pm
  • Editorial Team
    Editorial Team added an answer This funky looking string defines the secret-key algorithm to be… May 12, 2026 at 9:37 pm
  • Editorial Team
    Editorial Team added an answer You should call if_freenameindex() on first pif, not the final… May 12, 2026 at 9:37 pm

Related Questions

I have a pure Action Script 3 project which I am compiling with the
I'm writing a diagram editor in java. This app has the option to export
First the code (sorry if its not 100%) I am no expert and then
I have an ActiveRecord model with a field called name (in the database) and

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.