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

DES Algorithm - PHP Implementation in mcrypt

Part:   1  2 

(Continued from previous part...)

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.

Block Padding in mcrypt

"mycypt" offers a very simple padding method which simply adds 0x00 to the end of the plaintext message to make it multiples of 8 bytes (64 bits) for ECB and CBC modes. There is no need for padding for CFB and OFB modes because they are operated on 8-bit blocks.

Other PHP Implementations of DES Algorithm

If you search "DES algorithm implement in PHP" on Google, you will see Paul Tero's DES page at http://www.tero.co.uk/des/. It offers a source code version of DES implementation in PHP.

Conclusion

  • mcrypt extension offers DES 64-bit ECB, 64-bit CBC, 8-bit CFB and 8-bit OFB operation modes.
  • mcrypt pads plaintext messages with 0x00 bytes.

Part:   1  2 

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