JDK (Java Development Kit) Tutorials
Dr. Herong Yang, Version 5.00

Key Pair Sample Program - JcaKeyPair.java

This section provides a tutorial example on how to write simple program to generate a pair of private key and public key for the RSA or DSA algorithm.

The following sample program shows you how to invoke the key pair generation algorithms implemented by the default provider, Sun, and generate key pairs.

/**
 * JcaKeyPair.java
 * Copyright (c) 2002 by Dr. Herong Yang
 */
import java.io.*;
import java.math.*;
import java.security.*;
import java.security.interfaces.*;
class JcaKeyPair {
   public static void main(String[] a) {
      if (a.length<3) {
         System.out.println("Usage:");
         System.out.println("java JcaKeyPair keySize output"
            +" algorithm");
         return;
      }
      int keySize = Integer.parseInt(a[0]);
      String output = a[1];
      String algorithm = a[2]; // RSA, DSA
      try {
         getKeys(keySize,output,algorithm);
      } catch (Exception e) {
         System.out.println("Exception: "+e);
         return;
      }
   }
   private static void getKeys(int keySize, String output, 
         String algorithm) throws Exception {
      KeyPairGenerator kg = KeyPairGenerator.getInstance(algorithm);
      kg.initialize(keySize);
      System.out.println();
      System.out.println("KeyPairGenerator Object Info: ");
      System.out.println("Algorithm = "+kg.getAlgorithm());
      System.out.println("Provider = "+kg.getProvider());
      System.out.println("Key Size = "+keySize);
      System.out.println("toString = "+kg.toString());
      KeyPair pair = kg.generateKeyPair();
      PrivateKey priKey = pair.getPrivate();
      PublicKey pubKey = pair.getPublic();
      String fl = output+".pri";
      FileOutputStream out = new FileOutputStream(fl);
      byte[] ky = priKey.getEncoded();
      out.write(ky);
      out.close();
      System.out.println();
      System.out.println("Private Key Info: ");
      System.out.println("Algorithm = "+priKey.getAlgorithm());
      System.out.println("Saved File = "+fl);
      System.out.println("Size = "+ky.length);
      System.out.println("Format = "+priKey.getFormat());
      System.out.println("toString = "+priKey.toString());
      fl = output+".pub";
      out = new FileOutputStream(fl);
      ky = pubKey.getEncoded();
      out.write(ky);
      out.close();
      System.out.println();
      System.out.println("Public Key Info: ");
      System.out.println("Algorithm = "+pubKey.getAlgorithm());
      System.out.println("Saved File = "+fl);
      System.out.println("Size = "+ky.length);
      System.out.println("Format = "+pubKey.getFormat());
      System.out.println("toString = "+pubKey.toString());
   }
}

Note that:

  • KeyPairGenerator.getInstance(algorithm) method is called to get default key pair generator of the specified algorithm.
  • generateKeyPair() method is called to return a new pair of private key and public key.
  • getPrivate() method is called to return the private key from the key pair.
  • getPublic() method is called to return the public key from the key pair.
  • getEncoded() method is called to convert the key to an encoded byte stream.

See next sections on test result of this sample program.

Last update: 2006.

Sections in This Chapter

Private and Public Keys and Related Interfaces

KeyPair and KeyPairGenerator Classes

Key Pair Sample Program - JcaKeyPair.java

DSA Private Key and Public Key Pair Sample

RSA Private Key and Public Key Pair Sample

DiffieHellman Private Key and Public Key Pair Sample

Dr. Herong Yang, updated in 2008
Key Pair Sample Program - JcaKeyPair.java