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

DES Algorithm - PHP Implementation in mcrypt

Part:   1  2 

This tutorial help you to understand:

  • mcrypt Library for PHP
  • mcrypt Encryption Functions
  • des_mcrypt_operation_mode_test.php - mcrypt Operation Mode Test PHP Script
  • Block Padding in mcrypt
  • Other PHP Implementations of DES Algorithm

mcrypt Library for PHP

"mcrypt" is the suggested encryption extension for PHP. It supports a wide variety of block algorithms such as DES, TripleDES, Blowfish (default), 3-WAY, SAFER-SK64, SAFER-SK128, TWOFISH, TEA, RC2 and GOST in CBC, OFB, CFB and ECB cipher modes. See for details.

The best way to download and install "mcrypt" for your Windows system is to download the pre-compiled "mcrypt" version by the steps below:

1. Go to

2. Download the binary file, libmcrypt.dll, 19-Jan-2004 02:27, 163k.

3. Save libmcrypt.dll to your PHP directory, like \php\.

4. Open PHP initialization file, \php\php.ini.

5. Uncomment line "extension=php_mcrypt.dll".

You are ready to use "mcrypt" encryption functions.

mcrypt Encryption Functions

"mcrypt" encryption extension offers the following main functions to perform an encryption operation:

  • mcrypt_module_open() - Creates a resource handle for the specified encryption algorithm and operation mode.
  • mcrypt_generic_init() - Initializes an encryption handle with a key and an initial vector.
  • mcrypt_generic() - Encrypts a plaintext message with an initialized encryption handle.
  • mcrypt_generic_deinit() - De-initializes an encryption handle.
  • mcrypt_module_close() - Closes an encryption resource handle.

"mcrypt" does support all 4 DES operation modes through predefined constants:

  • MCRYPT_MODE_ECB - The 64-bit ECB (Electronic CodeBook) operation mode.
  • MCRYPT_MODE_CBC - The 64-bit CBC (Cipher Block Chaining) operation mode.
  • MCRYPT_MODE_CFB - The 8-bit CFB (Cipher FeedBack) operation mode
  • MCRYPT_MODE_OFB - The 8-bit OFB (Output FeedBack) operation mode

des_mcrypt_operation_mode_test.php - mcrypt Operation Mode Test PHP Script

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

<?php # des_mcrypt_operation_mode_test.php
# Copyright (c) 2006 by Dr. Herong Yang,
   $key = pack("H*", "0123456789ABCDEF");
   $iv = pack("H*", "1234567890ABCDEF");
   # "Now is the time for all "   
   $plain_text = pack("H*", 

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

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

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

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

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);
   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));

(Continued on next part...)

Part:   1  2 

Dr. Herong Yang, updated in 2007
Cryptography Tutorials - Herong's Tutorial Notes - DES Algorithm - PHP Implementation in mcrypt