DsaKeyChecker.java - Reading and Checking DSA Keys

This section provides a tutorial example on how to read DSA public key and private key back from the key files, assuming they are using PKCS#8 and X.509 encoding formats.

In my DsaKeyGenerator.java program, DSA public key and private key are saved in 2 separate files. If you want to learn how read them back into Java programs, you can read my Java program, DsaKeyChecker.java:

/* DsaKeyChecker.java
#- Copyright (c) 2013, HerongYang.com, All Rights Reserved.
import java.io.*;
import java.math.*;
import java.security.*;
import java.security.interfaces.*;
import java.security.spec.*;
class DsaKeyChecker {
   public static void main(String[] a) {
      if (a.length<1) {
         System.out.println("java DsaKeyChecker input");
      String input = a[0];
      String algorithm = "DSA";
      try {
         KeyPair pair = readKeyPair(input,algorithm);
      } catch (Exception e) {
         System.out.println("Exception: "+e);
   private static KeyPair readKeyPair(String input,
         String algorithm) throws Exception {
      KeyFactory keyFactory = KeyFactory.getInstance(algorithm);

      String priKeyFile = input+".pri";
      FileInputStream priKeyStream = new FileInputStream(priKeyFile);
      int priKeyLength = priKeyStream.available();
      byte[] priKeyBytes = new byte[priKeyLength];
      PKCS8EncodedKeySpec priKeySpec 
         = new PKCS8EncodedKeySpec(priKeyBytes);
      PrivateKey priKey = keyFactory.generatePrivate(priKeySpec);

      String pubKeyFile = input+".pub";
      FileInputStream pubKeyStream = new FileInputStream(pubKeyFile);
      int pubKeyLength = pubKeyStream.available();
      byte[] pubKeyBytes = new byte[pubKeyLength];
      X509EncodedKeySpec pubKeySpec 
         = new X509EncodedKeySpec(pubKeyBytes);
      PublicKey pubKey = keyFactory.generatePublic(pubKeySpec);

      return new KeyPair(pubKey, priKey);
   private static void checkDsaKeyPair(KeyPair pair) {
      DSAPublicKey pubKey = (DSAPublicKey) pair.getPublic();
      DSAPrivateKey priKey = (DSAPrivateKey) pair.getPrivate();
      DSAParams params = priKey.getParams();
      BigInteger p = params.getP();
      BigInteger q = params.getQ();
      BigInteger g = params.getG();
      BigInteger x = priKey.getX();
      BigInteger y = pubKey.getY();

      System.out.println("DSA Key Parameters: ");
      System.out.println("p = "+p);
      System.out.println("q = "+q);
      System.out.println("g = "+g);
      System.out.println("x = "+x);
      System.out.println("y = "+y);

      System.out.println("DSA Key Verification: ");
      System.out.println("What's key size? "+p.bitLength());
      System.out.println("Is p a prime? "+p.isProbablePrime(200));
      System.out.println("Is q a prime? "+q.isProbablePrime(200));
      System.out.println("Is p-1 mod q == 0? "
      System.out.println("Is g**q mod p == 1? "+g.modPow(q,p));
      System.out.println("Is q > x? "+(q.compareTo(x)==1));
      System.out.println("Is g**x mod p == y? "+g.modPow(x,p).equals(y));

Some notes on DsaKeyChecker.java:

Testing result is presented in the next tutorial.

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

 DsaKeyGenerator.java - Generating DSA Key Pair

 DSA 512-bit and 1024-bit Key Pair Examples

DsaKeyChecker.java - Reading and Checking DSA Keys

 Example of DSA Key Parameters and Properties

 java.security.Signature - The Data Signing Class

 DsaSignatureGenerator.java - Generating DSA Digital Signature

 DsaSignatureGenerator.java Test Results

 DsaSignatureVerifier.java - Verifying DSA Digital Signature

 DsaSignatureVerifier.java Test Results

 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

 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


 PDF Printing Version