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
http://sourceforge.net/projects/mcrypt 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 http://ftp.emini.dk/pub/php/win32/mcrypt/.
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, http://www.herongyang.com/
#
$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));
}
?>
(Continued on next part...)
Part:
1
2
|