mcrypt DES Encryption Testing Program

This section provides a tutorial example on how to use the DES algorithm in a specific operation mode as block or stream cipher.

To test out the DES operation modes and stream cipher modes implemented in the "mcrypt" extension, I wrote the following test PHP script:

<?php # des_mcrypt_operation_mode_test.php
#- Copyright (c) 2013, HerongYang.com, All Rights Reserved.
# 
   $key = pack("H*", "0123456789ABCDEF");
   $iv = pack("H*", "1234567890ABCDEF");
   # "Now is the time for all "   
   $plain_text = pack("H*", 
      "4E6F77206973207468652074696D6520666F7220616C6C20");

   $mode = MCRYPT_MODE_ECB;
   $expected = pack("H*", 
      "3FA40E8A984D43156A271787AB8883F9893D51EC4B563B53");
   print("\n\nTest 1 - DES ECB 64-bit:");
   des_test($mode);

   $mode = MCRYPT_MODE_CBC;
   $expected = pack("H*", 
      "E5C7CDDE872BF27C43E934008C389C0F683788499A7C05F6");
   print("\n\nTest 2 - DES CBC 64-bit:");
   des_test($mode);

   $mode = MCRYPT_MODE_CFB; # 8-bit
   $expected = pack("H*", 
      "F31FDA07011462EE187F43D80A7CD9B5B0D290DA6E5B9A87");
   print("\n\nTest 3 - DES CFB 8-bit:");
   des_test($mode);

   $mode = MCRYPT_MODE_OFB; # 8-bit
   $expected = pack("H*", 
      "F34A2850C9C64985D684AD96D772E2F243EA499ABEE8AE95");
   print("\n\nTest 4 - DES OFB 8-bit:");
   des_test($mode);

function des_test($mode) {
   global $key, $iv, $plain_text, $expected;
   $cipher = mcrypt_module_open(MCRYPT_DES, '', $mode, '');
   mcrypt_generic_init($cipher, $key, $iv);
   $cipher_text = mcrypt_generic($cipher, $plain_text);
   mcrypt_generic_deinit($cipher);
   mcrypt_module_close($cipher);
   print("\nThe key       : ".bin2Hex($key));
   print("\nThe IV        : ".bin2Hex($iv));
   print("\nThe plaintext : ".bin2Hex($plain_text));
   print("\nThe ciphertext: ".bin2Hex($cipher_text));
   print("\nThe expected  : ".bin2Hex($expected));
}
?>

If you run this script, you should get:

Test 1 - DES ECB 64-bit:
The key       : 0123456789abcdef
The IV        : 1234567890abcdef
The plaintext : 4e6f77206973207468652074696d6520666f7220616c6c20
The ciphertext: 3fa40e8a984d48156a271787ab8883f9893d51ec4b563b53
The expected  : 3fa40e8a984d43156a271787ab8883f9893d51ec4b563b53

Test 2 - DES CBC 64-bit:
The key       : 0123456789abcdef
The IV        : 1234567890abcdef
The plaintext : 4e6f77206973207468652074696d6520666f7220616c6c20
The ciphertext: e5c7cdde872bf27c43e934008c389c0f683788499a7c05f6
The expected  : e5c7cdde872bf27c43e934008c389c0f683788499a7c05f6

Test 3 - DES CFB 8-bit:
The key       : 0123456789abcdef
The IV        : 1234567890abcdef
The plaintext : 4e6f77206973207468652074696d6520666f7220616c6c20
The ciphertext: f31fda07011462ee187f43d80a7cd9b5b0d290da6e5b9a87
The expected  : f31fda07011462ee187f43d80a7cd9b5b0d290da6e5b9a87

Test 4 - DES OFB 8-bit:
The key       : 0123456789abcdef
The IV        : 1234567890abcdef
The plaintext : 4e6f77206973207468652074696d6520666f7220616c6c20
The ciphertext: f34a2850c9c64985d684ad96d772e2f243ea499abee8ae95
The expected  : f34a2850c9c64985d684ad96d772e2f243ea499abee8ae95

Output looks very good. All 4 test cases match the expected ciphertext message documented in http://www.itl.nist.gov/fipspubs/fip81.htm.

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

 mcrypt Library for PHP

 mcrypt Encryption Functions

mcrypt DES Encryption Testing Program

 Block Padding in 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