"DumpKey.java" Dumping Private Keys Out of "keystore"

This section provides a tutorial example on how to dump private key and public key pair output of a 'PrivateKeyEntry' in a 'keystore' file using a Java program, DumpKey.java.

Since "keytool" can not be used to export the private and public key pair out of a 'keystore' file, I wrote the following Java program, DumpKey.java, to do this job:

/* DumpKey.java
#- Copyright (c) 2013, HerongYang.com, All Rights Reserved.
 */
import java.io.*;
import java.security.*;
public class DumpKey {
   static public void main(String[] a) {
      if (a.length<5) {
         System.out.println("Usage:");
         System.out.println(
            "java DumpKey jks storepass alias keypass out");
         return;
      }
      String jksFile = a[0];
      char[] jksPass = a[1].toCharArray();
      String keyName = a[2];
      char[] keyPass = a[3].toCharArray();
      String outFile = a[4];

      try {
         KeyStore jks = KeyStore.getInstance("jks");
         jks.load(new FileInputStream(jksFile), jksPass);
         Key key = jks.getKey(keyName, keyPass);
         System.out.println("Key algorithm: "+key.getAlgorithm());
         System.out.println("Key format: "+key.getFormat());
         System.out.println("Writing key in binary form to "
            +outFile);

         FileOutputStream out = new FileOutputStream(outFile);
         out.write(key.getEncoded());
         out.close();
      } catch (Exception e) {
         e.printStackTrace();
         return;
      }
   }
}

Notes on DumpKey.java:

I tried my DumpKey.java program with my key pair stored in herong.jks as show below:

>javac DumpKey.java

>java DumpKey herong.jks jkspass herong_key keypass herong_bin.key
Key algorithm: DSA
Key format: PKCS#8
Writing key in binary form to herong_bin.key

Excellent. I got my key pair dumped out of the keystore file into a binary PKCS#8 format.

Now I am ready to test my private and public key pair with "OpenSSL" as shown in the next section.

Last update: 2013.

Table of Contents

 About This Book

 Cryptography Terminology

 Cryptography Basic Concepts

 Introduction to AES (Advanced Encryption Standard)

 Introduction to DES Algorithm

 DES Algorithm - Illustrated with Java Programs

 DES Algorithm Java Implementation

 DES Algorithm - Java Implementation in JDK JCE

 DES Encryption Operation Modes

 DES in Stream Cipher Modes

 PHP Implementation of DES - mcrypt

 Blowfish - 8-Byte Block Cipher

 Secret Key Generation and Management

 Cipher - Secret Key Encryption and Decryption

 Introduction of RSA Algorithm

 RSA Implementation using java.math.BigInteger Class

 Introduction of DSA (Digital Signature Algorithm)

 Java Default Implementation of DSA

 Private key and Public Key Pair Generation

 PKCS#8/X.509 Private/Public Encoding Standards

 Cipher - Public Key Encryption and Decryption

 MD5 Mesasge Digest Algorithm

 SHA1 Mesasge Digest Algorithm

 OpenSSL Introduction and Installation

 OpenSSL Generating and Managing RSA Keys

 OpenSSL Managing Certificates

 OpenSSL Generating and Signing CSR

 OpenSSL Validating Certificate Path

 "keytool" and "keystore" from JDK

 "OpenSSL" Signing CSR Generated by "keytool"

Migrating Keys from "keystore" to "OpenSSL" Key Files

 No "keytool" Command to Export Keys

 "keytool -genkeypair" Generating PrivateKeyEntry

 "keytool -exportcert" Exporting PrivateKeyEntry

 "keytool -printcert" Printing Certificate Details

 "openssl x509" Viewing Certificate Details

"DumpKey.java" Dumping Private Keys Out of "keystore"

 "openssl enc" Converting Keys from Binary to PEM

 "openssl dsa" Viewing Private and Public Key Pair

 Certificate X.509 Standard and DER/PEM Formats

 Migrating Keys from "OpenSSL" Key Files to "keystore"

 Using Certificates in IE (Internet Explorer)

 Using Certificates in Firefox

 Using Certificates in Google Chrome

 Outdated Tutorials

 References

 PDF Printing Version