Testing DES Algorithm in JDK JCE

This section provides a tutorial example to test DES algorithm implemented in the JDK JCE package.

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

/* JceSunDesTest.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
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("java JceSunDesTest 1/2");
      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("java JceSunDesTest 1/2");
         KeySpec ks = new DESKeySpec(theKey);
         SecretKeyFactory kf 
            = SecretKeyFactory.getInstance("DES");
         SecretKey ky = kf.generateSecret(ks);
         Cipher cf = Cipher.getInstance("DES/ECB/NoPadding");
         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) {
   public static byte[] hexToBytes(String str) {
      if (str==null) {
         return null;
      } else if (str.length() < 2) {
         return null;
      } else {
         int len = str.length() / 2;
         byte[] buffer = new byte[len];
         for (int i=0; i<len; i++) {
             buffer[i] = (byte) Integer.parseInt(
         return buffer;
   public static String bytesToHex(byte[] data) {
      if (data==null) {
         return null;
      } else {
         int len = data.length;
         String str = "";
         for (int i=0; i<len; i++) {
            if ((data[i]&0xFF)<16) str = str + "0" 
               + java.lang.Integer.toHexString(data[i]&0xFF);
            else str = str
               + java.lang.Integer.toHexString(data[i]&0xFF);
         return str.toUpperCase();

Note that this program, CipherDesTest.java, can be easily modified to add other testing cases. I am using the ECB mode and NoPadding option to make it compatible with original DES standard.

Case 1 - Taken from "The DES Algorithm Illustrated" at http://www.orlingrabbe.com/des.htm:

herong> javac JceSunDesTest.java
herong> java JceSunDesTest 1

Key     : 133457799BBCDFF1
Message : 0123456789ABCDEF
Cipher  : 85E813540F0AB405
Expected: 85E813540F0AB405

Case 2 - Modified from "DES Source Code" at http://www.tero.co.uk/des/code.php:

herong> java JceSunDesTest 2

Key     : 38627974656B6579
Message : 6D6573736167652E
Cipher  : 7CF45E129445D451
Expected: 7CF45E129445D451

This result is identical to my Java implementation program described in the previous chapter!

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 Java Implementation in JDK by Sun

 Steps of Using DES Algorithm in JDK JCE

Testing DES Algorithm in JDK JCE

 What Is PKCS5Padding?

 JceSunDesPaddingTest.java - JCE DES Padding Test

 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

 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

 Archived Tutorials


 Full Version in PDF/EPUB