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

Private key and Public Key Pair Generation

This chapter provides tutorial notes and example codes on private key and public key pair generation. Topics include public key encryption algorithms, RSA, DSA and DiffieHellman; private key and public key pair generation class and sample program; RSA, DSA, and DiffieHellman key pair samples.

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

Conclusion:

  • JDK supports 3 algorithms to generate private and public pairs: RSA, DSA, and DiffieHellman.
  • JDK allows you to store private keys in files with PKCS#8 encoded format.
  • JDK allows you to store public keys in files with X.509 encoded format.

Sample programs listed in this chapter have been tested with JDK 1.3.1, 1.4.1 and 1.5.0.

Question: Key pairs are used to encrypt and decrypt data. But how to encrypt and decrypt data? Which classes and methods to use?

Dr. Herong Yang, updated in 2008
Private key and Public Key Pair Generation