JCE DES Stream Ciphers Testing Program

This section provides a tutorial example on how to use DES stream ciphers provided in the JDK JCE package.

To test out JCE DES stream cipher mode implementation, I wrote the following testing program:

/**
 * JceSunDesStreamCipherTest.java
#- Copyright (c) 2013, HerongYang.com, All Rights Reserved.
 */
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;
class JceSunDesStreamCipherTest {
   public static void main(String[] a) {
      if (a.length<1) {
         System.out.println("Usage:");
         System.out.println(
            "java JceSunDesStreamCipherTest 1/2/3/4");
         return;
      }
      String test = a[0];
      try {
         byte[] theKey = null;
         byte[] theIVp = null;
         byte[] theMsg = null; 
         byte[] theExp = null; 
         String algorithm = null;
         if (test.equals("1")) { 
            algorithm = "DES/CFB8/NoPadding";
            theKey = hexToBytes("0123456789ABCDEF");
            theIVp = hexToBytes("1234567890ABCDEF");
            theMsg = hexToBytes("4E6F7720697320746865");
            // "Now is the"
            theExp = hexToBytes("F31FDA07011462EE187F");
         } else if (test.equals("2")) { 
            algorithm = "DES/CFB64/NoPadding";
            theKey = hexToBytes("0123456789ABCDEF");
            theIVp = hexToBytes("1234567890ABCDEF");
            theMsg = hexToBytes(
               "4E6F77206973207468652074696D6520666F7220616C6C20");
            // "Now is the time for all "
            theExp = hexToBytes(
               "F3096249C7F46E51A69E839B1A92F78403467133898EA622");
         } else if (test.equals("3")) { 
            algorithm = "DES/OFB8/NoPadding";
            theKey = hexToBytes("0123456789ABCDEF");
            theIVp = hexToBytes("1234567890ABCDEF");
            theMsg = hexToBytes("4E6F7720697320746865");
            // "Now is the"
            theExp = hexToBytes("F34A2850C9C64985D684");
         } else if (test.equals("4")) { 
            algorithm = "DES/OFB64/NoPadding";
            theKey = hexToBytes("0123456789ABCDEF");
            theIVp = hexToBytes("1234567890ABCDEF");
            theMsg = hexToBytes(
               "4E6F77206973207468652074696D6520666F7220616C6C20");
            // "Now is the time for all "
            theExp = hexToBytes(
               "F3096249C7F46E5135F24A242EEB3D3F3D6D5BE3255AF8C3");
         } else {
            System.out.println("Wrong option. For help enter:");
            System.out.println("java JceSunDesStreamCipherTest");
            return;
         }	
         KeySpec ks = new DESKeySpec(theKey);
         SecretKeyFactory kf 
            = SecretKeyFactory.getInstance("DES");
         SecretKey ky = kf.generateSecret(ks);
         Cipher cf = Cipher.getInstance(algorithm);
         if (theIVp == null) {
            cf.init(Cipher.ENCRYPT_MODE, ky);
         } else {
            AlgorithmParameterSpec aps = new IvParameterSpec(theIVp);
            cf.init(Cipher.ENCRYPT_MODE, ky, aps);
         }
         byte[] theCph = cf.doFinal(theMsg);
         System.out.println("Key     : "+bytesToHex(theKey));
         if (theIVp != null) {
            System.out.println("IV      : "+bytesToHex(theIVp));
         }
         System.out.println("Message : "+bytesToHex(theMsg));
         System.out.println("Cipher  : "+bytesToHex(theCph));
         System.out.println("Expected: "+bytesToHex(theExp));
      } catch (Exception e) {
         e.printStackTrace();
         return;
      }
   }
   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(
                str.substring(i*2,i*2+2),16);
         }
         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();
      }
   }            
}

This program provides 4 tests: CFB8, CFB64, OFB8 and OFB64 with testing plaintext messages, keys and initial vectors used in FIPS81.

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

 Introducting DES Stream Cipher Modes

 CFB (Cipher FeedBack) as a Stream Cipher

 OFB (Output FeedBack) as a Stream Cipher

 CFB and OFB Stream Ciphers Implemented in JCE

JCE DES Stream Ciphers Testing Program

 JCE DES Stream Ciphers Testing Program Result

 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 (Internet Explorer)

 Using Certificates in Firefox

 Using Certificates in Google Chrome

 Outdated Tutorials

 References

 PDF Printing Version