JcePublicCipher.java - Public Key Encryption Sample Program

This section provides a tutorial example on how to write a public key encryption and private key decryption program using the javax.crypto.Cipher class.

The following sample program shows you how to do encryption and decryption with a private and public key pair:

/* JcePublicCipher.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
import java.io.*;
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;
class JcePublicCipher {
   public static void main(String[] a) {
      if (a.length<5) {
         System.out.println("java JcePublicCipher algorithm mode"
            +" keyFile input output");
      String algorithm = a[0];
      String mode = a[1];
      String keyFile = a[2];
      String input = a[3];
      String output = a[4];
      try {
         Key ky = readKey(algorithm, mode, keyFile);
         publicCipher(algorithm, mode, ky, input, output);
      } catch (Exception e) {
   private static Key readKey(String algorithm, String mode,
      String input) throws Exception {
      KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
      System.out.println("KeyFactory Object Info: ");
      System.out.println("Algorithm = "+keyFactory.getAlgorithm());
      System.out.println("Provider = "+keyFactory.getProvider());
      System.out.println("toString = "+keyFactory.toString());

      FileInputStream fis = new FileInputStream(input);
      int kl = fis.available();
      byte[] kb = new byte[kl];
      Key ky = null;
      if (mode.equalsIgnoreCase("encrypt")) {
         X509EncodedKeySpec pubKeySpec 
            = new X509EncodedKeySpec(kb);
         ky = keyFactory.generatePublic(pubKeySpec);
      } else if (mode.equalsIgnoreCase("decrypt")) {
         PKCS8EncodedKeySpec priKeySpec 
            = new PKCS8EncodedKeySpec(kb);
         ky = keyFactory.generatePrivate(priKeySpec);
      } else
         throw new Exception("Invalid mode: "+mode);
      System.out.println("Key Object Info: ");
      System.out.println("Algorithm = "+ky.getAlgorithm());
      System.out.println("Saved File = "+input);
      System.out.println("Length = "+kl);
      System.out.println("Format = "+ky.getFormat());
      System.out.println("toString = "+ky.toString());
      return ky;
   private static void publicCipher(String algorithm, String mode,
      Key ky, String input, String output) throws Exception {
      Cipher cf = Cipher.getInstance(algorithm);
      if (mode.equalsIgnoreCase("encrypt")) 
      else if (mode.equalsIgnoreCase("decrypt"))
         throw new Exception("Invalid mode: "+mode);
      System.out.println("Cipher Object Info: ");
      System.out.println("Block Size = "+cf.getBlockSize());
      System.out.println("Algorithm = "+cf.getAlgorithm());
      System.out.println("Provider = "+cf.getProvider());
      System.out.println("toString = "+cf.toString());

      FileInputStream fis = new FileInputStream(input);
      FileOutputStream fos = new FileOutputStream(output);
      int bufSize = 1024;
      byte[] buf = new byte[bufSize];
      int n = fis.read(buf,0,bufSize);
      int fisSize = 0;
      int fosSize = 0;
      while (n!=-1) {
         fisSize += n;
         byte[] out = cf.update(buf,0,n);
         fosSize += out.length;
         n = fis.read(buf,0,bufSize);
      byte[] out = cf.doFinal();
      fosSize += out.length;
      System.out.println("Cipher Process Info: ");
      System.out.println("Input Size = "+fisSize);
      System.out.println("Output Size = "+fosSize);

Note that:

See the next section for test result of this sample program.

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

 javax.crypto.Cipher - The Public Key Encryption Class

JcePublicCipher.java - Public Key Encryption Sample Program

 DSA Public Key Encryption Tests

 RSA Public Key Encryption Tests

 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

 Using Certificates in Google Chrome

 Using Certificates in Firefox

 Outdated Tutorials


 Full Version in PDF/EPUB