tero.co.uk

Having Trouble?

Lots of people of have emailed me because they are having problems decrypting data using another package. So this page provides some recommendations, some things to test against, and some proof that the Javascript DES version does actually work.

Firstly, I recommend you try encrypting and decrypting using the examples below to see if you get the same results. If your results bear no relation at all to the examples below check you are using the same mode, same algorithm (eg single or triple DES) and same input vector (for CBC mode). If only the last part of your result is wrong, check you are using the same padding method.

Encrypted data might also become corrupted in transport (eg from the client to server) or when displayed on the screen (see first example below). For these reasons I convert the strings to and from hexidecimal. (Note that this conversion is not done automatically by the algorithm. You have to do it explicitly before or after running DES.) So also check that your package uses hexidecimal (and not base64 or some other method), check whether or not it needs the 0x at the beginning of the hexidecimal string, and check that the hexidecimal conversion works the same (for example "test string" should convert to 0x7465737420737472696e67).

Luckily this server has the PHP mcrypt library available, so I can show (below) that this Javascript DES (and the PHP translation) do in fact produce the correct results. The mcrypt library pads with null bytes, so that is the padding used below. You can also test the Javascript implementation with other keys and messages.

Single DES Encryption in ECB mode

Key12345678
MessageThis is the message to encrypt!!
PHP mcrypt7[Z]3ؗķ
In hex0x05c9c4cafb9937d95bbfbedfc5d77f19a6cd5a5dab188a33dfd8979fc4b7b2be
Javascript
PHP version0x05c9c4cafb9937d95bbfbedfc5d77f19a6cd5a5dab188a33dfd8979fc4b7b2be

Code used to call the above

PHP mcryptmcrypt_encrypt (MCRYPT_DES, "12345678", "This is the message to encrypt!!", MCRYPT_MODE_ECB);
In hexstringToHex (mcrypt_encrypt (MCRYPT_DES, "12345678", "This is the message to encrypt!!", MCRYPT_MODE_ECB));
Javascript/PHPstringToHex (des ("12345678", "This is the message to encrypt!!", 1));

Single DES Decryption in ECB mode

Key12345678
Message0x05c9c4cafb9937d95bbfbedfc5d77f19a6cd5a5dab188a33dfd8979fc4b7b2be
PHP mcryptThis is the message to encrypt!!
Javascript
PHP versionThis is the message to encrypt!!

Single DES Encryption in CBC mode

Key12345678
MessageThis is the message to encrypt!!
Input vectorabcdefgh
PHP mcrypt0x6ca9470c849d1cc1a59ffc148f1cb5e9cf1f5c0328a7e8756387ff4d0fe46050
Javascript
PHP version0x6ca9470c849d1cc1a59ffc148f1cb5e9cf1f5c0328a7e8756387ff4d0fe46050

Single DES Decryption in CBC mode

Key12345678
Message0x6ca9470c849d1cc1a59ffc148f1cb5e9cf1f5c0328a7e8756387ff4d0fe46050
Input vectorabcdefgh
PHP mcryptThis is the message to encrypt!!
Javascript
PHP versionThis is the message to encrypt!!

Triple DES Encryption in ECB mode

Key12345678abcdefghstuvwxyz
MessageThis is the message to encrypt, it will be padded.
PHP mcrypt0x8c5c1e4affcaf69cc8c274a95f57c4a41b63c78e2ae3c67e6192c492157fac73623f2d96ae4ecfa6bc7e61ee2557ed203f144d2cc8e8e10e
Javascript
PHP version0x8c5c1e4affcaf69cc8c274a95f57c4a41b63c78e2ae3c67e6192c492157fac73623f2d96ae4ecfa6bc7e61ee2557ed203f144d2cc8e8e10e

Triple DES Decryption in ECB mode

Key12345678abcdefghstuvwxyz
Message0x8c5c1e4affcaf69cc8c274a95f57c4a41b63c78e2ae3c67e6192c492157fac73623f2d96ae4ecfa6bc7e61ee2557ed203f144d2cc8e8e10e
PHP mcryptThis is the message to encrypt, it will be padded.
Javascript
PHP versionThis is the message to encrypt, it will be padded.

Triple DES Encryption in CBC mode

Key12345678abcdefghstuvwxyz
MessageThis is the message to encrypt, it will be padded.
Input vectorabcdefgh
PHP mcrypt0x61b0cefb60b56d1885fcf647d7ebf44c9031b2f2c2c06018d749f99620abe1b226ce32411e55d2ba23b8704f6e19de22461d8f2bf1cc82cf
Javascript
PHP version0x61b0cefb60b56d1885fcf647d7ebf44c9031b2f2c2c06018d749f99620abe1b226ce32411e55d2ba23b8704f6e19de22461d8f2bf1cc82cf

Triple DES Decryption in CBC mode

Key12345678abcdefghstuvwxyz
Message0x61b0cefb60b56d1885fcf647d7ebf44c9031b2f2c2c06018d749f99620abe1b226ce32411e55d2ba23b8704f6e19de22461d8f2bf1cc82cf
Input vectorabcdefgh
PHP mcryptThis is the message to encrypt, it will be padded.
Javascript
PHP versionThis is the message to encrypt, it will be padded.

Code used to call the above

PHP mcryptmcrypt_decrypt (MCRYPT_TRIPLEDES, "12345678abcdefghstuvwxyz", hexToString ("0x61b0cefb60b56d1885fcf647d7ebf44c9031b2f2c2c06018d749f99620abe1b226ce32411e55d2ba23b8704f6e19de22461d8f2bf1cc82cf"), MCRYPT_MODE_CBC, "abcdefgh");
Javascript/PHPdes ("12345678abcdefghstuvwxyz", hexToString ("0x61b0cefb60b56d1885fcf647d7ebf44c9031b2f2c2c06018d749f99620abe1b226ce32411e55d2ba23b8704f6e19de22461d8f2bf1cc82cf"), 0, 1, "abcdefgh");

Standard ANSI test for single DES in ECB Mode

Key0x0123456789abcdef
MessageNow is the time for all
In hex0x4e6f77206973207468652074696d6520666f7220616c6c20
Expected0x3fa40e8a984d48156a271787ab8883f9893d51ec4b563b53
PHP mcrypt0x3fa40e8a984d48156a271787ab8883f9893d51ec4b563b53
Javascript
PHP version0x3fa40e8a984d48156a271787ab8883f9893d51ec4b563b53