Cryptography Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 4.00

JCE - Cipher for Encryption and Decryption

Part:   1  2  3 

This chapter describes some parts of the JCE (Java Cryptography Extension) which has been introduced in JDK since 1.1:

  • The Cipher class.
  • How to use cipher with secret keys for encryption and decryption.
  • How to use cipher with private and public key pairs for encryption and decryption.

The Cipher Class

javax.crypto.Cipher is a class that provides functionality of a cryptographic cipher for encryption and decryption. It has the following major methods.

getInstance() - Returns a Cipher object of the specified transformation (algorithm plus options) from the implementation of the specified provider. If provider is not specified, the default implementation is used. This is a static method.

init() - Initializes this cipher for the specified operation mode with the specified key or public key certificate. Two important operation modes are Cipher.ENCRYPT_MODE and Cipher.DECRYPT_MODE.

update() - Feeds additional input data to this cipher and generates partial output data.

doFinal() - Feeds the last part of the input data to this cipher and generates the last part of the output data.

getBlockSize() - Returns the block size of this cipher.

getAlgorithm() - Returns the algorithm name of this cipher.

getProvider() - Returns the provider as a Provider object of this cipher. - Cipher with Secret Key

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

 * Copyright (c) 2002 by Dr. Herong Yang
import javax.crypto.*;
import javax.crypto.spec.*;
class JceSecretCipher {
   public static void main(String[] a) {
      if (a.length<5) {
         System.out.println("java JceSecretCipher 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 {
         SecretKey ky = readKey(keyFile,algorithm);
         secretCipher(algorithm, mode, ky, input, output);
      } catch (Exception e) {
         System.out.println("Exception: "+e);
   private static SecretKey readKey(String input, String algorithm) 
      throws Exception {
      String fl = input;
      FileInputStream fis = new FileInputStream(fl);
      int kl = fis.available();
      byte[] kb = new byte[kl];;
      KeySpec ks = null;
      SecretKey ky = null;
      SecretKeyFactory kf = null;
      if (algorithm.equalsIgnoreCase("DES")) {
      	 ks = new DESKeySpec(kb);
         kf = SecretKeyFactory.getInstance("DES");
         ky = kf.generateSecret(ks);
      } else if (algorithm.equalsIgnoreCase("DESede")) {
      	 ks = new DESedeKeySpec(kb);
         kf = SecretKeyFactory.getInstance("DESede");
         ky = kf.generateSecret(ks);
      } else {
         ks = new SecretKeySpec(kb,algorithm);
         ky = new SecretKeySpec(kb,algorithm);

(Continued on next part...)

Part:   1  2  3 

Dr. Herong Yang, updated in 2007
Cryptography Tutorials - Herong's Tutorial Notes - JCE - Cipher for Encryption and Decryption