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

DES Algorithm - Java JCE SUN Implementation

Part:   1  2  3 

This tutorial helps you understand:

  • DES Java Implementation by Sun
  • Using DES Algorithm in JDK JCE Package
  • Test Cases of DES Encryption and Decryption
  • What Is PKCS5Padding?
  • JceSunDesPaddingTest.java - JCE DES Padding Testing Program

DES Java Implementation by Sun

DES algorithm has been implement in Java by Sun as part of the JDK JCE (Java Cryptography Extension) package. Sun's implementation includes the following features:

  • Offers both DES and Triple DES algorithms.
  • Offers Electronic Code Book (ECB), Cipher Block Chaining (CBC), Cipher Feedback (CFB), Output Feedback (OFB), and Propagating Cipher Block Chaining (PCBC) modes.
  • Offers PKCS5 padding options

Using DES Algorithm in JDK JCE Package

To use DES algorithm in the JDK JCE package, we need to use several JCE classes and following the steps below:

1. Creating a DESKeySpec object from a 8-byte key material by calling the javax.crypto.spec.DESKeySpec constructor.

2. Creating a SecretKeyFactory object with DES algorithm name, mode name, and padding name by calling the javax.crypto.SecretKeyFactory.getInstance() static method.

3. Converting the DESKeySpec object to a SecretKey object through the SecretKeyFactory object by calling the javax.crypto.SecretKeyFactory.generateSecret() method.

4. Creating a Cipher object with DES algorithm name, mode name, and padding name by calling the javax.crypto.Cipher.getInstance() static method.

5. Initializing the Cipher object with the SecretKey object by calling the javax.crypto.Cipher.init() method.

6. Ciphering through input data as byte arrays by calling the javax.crypto.Cipher.update() method.

7. Finalizing the ciphering process by calling the javax.crypto.Cipher.doFinal() method.

Test Cases of DES Encryption and Decryption

To test out Sun's DES implementation, I created the following testing Java program to performed several test cases to understand those classes and methods.

/**
 * JceSunDesTest.java
 * Copyright (c) 2006 by Dr. Herong Yang, http://www.herongyang.com/
 */
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;
class JceSunDesTest {
   public static void main(String[] a) {
      if (a.length<1) {
         System.out.println("Usage:");
         System.out.println("java JceSunDesTest 1/2");
         return;
      }
      String test = a[0];
      try {
         byte[] theKey = null;
         byte[] theMsg = null; 
         byte[] theExp = null; 
         if (test.equals("1")) { 
            theKey = hexToBytes("133457799BBCDFF1");
            theMsg = hexToBytes("0123456789ABCDEF");
            theExp = hexToBytes("85E813540F0AB405");
         } else if (test.equals("2")) { 
            theKey = hexToBytes("38627974656B6579"); // "8bytekey"
            theMsg = hexToBytes("6D6573736167652E"); // "message."
            theExp = hexToBytes("7CF45E129445D451");
         } else {
            System.out.println("Usage:");
            System.out.println("java JceSunDesTest 1/2");
            return;
         }	
         KeySpec ks = new DESKeySpec(theKey);
         SecretKeyFactory kf 
            = SecretKeyFactory.getInstance("DES");
         SecretKey ky = kf.generateSecret(ks);
         Cipher cf = Cipher.getInstance("DES/ECB/NoPadding");
         cf.init(Cipher.ENCRYPT_MODE,ky);
         byte[] theCph = cf.doFinal(theMsg);
         System.out.println("Key     : "+bytesToHex(theKey));
         System.out.println("Message : "+bytesToHex(theMsg));
         System.out.println("Cipher  : "+bytesToHex(theCph));
         System.out.println("Expected: "+bytesToHex(theExp));
      } catch (Exception e) {
         e.printStackTrace();
         return;
      }
   }

(Continued on next part...)

Part:   1  2  3 

Dr. Herong Yang, updated in 2007
Cryptography Tutorials - Herong's Tutorial Notes - DES Algorithm - Java JCE SUN Implementation