View Full Version : Crypting with DES in CBC mode

01-05-2006, 01:24 PM
Hi !

Is this algorithm difficult to implement in PHP code?
1. Concatenate the username and the password to produce a plaintext string;
2. Convert the plaintext string to uppercase characters;
3. Convert the plaintext string to multi-byte storage format; ASCII
characters have the high byte set to 0x00;
4. Encrypt the plaintext string (padded with 0s if necessary to the
next even block length) using the DES algorithm in cipher block
chaining (CBC) mode with a fixed key value of 0x0123456789ABCDEF;
5. Encrypt the plaintext string again with DES-CBC, but using the
last block of the output of the previous step (ignoring parity
bits) as the encryption key. The last block of the output is
converted into a printable string to produce the password hash

(Point 1 and 2 is, of course, not THAT difficult ;))

I came so far:

$chiper = MCRYPT_3DES;
$key1 = ?; //What is this?
$data = "USERNAMEPASSWORD"; // (How to fix this acording to point 3 in the algorithm?)
$iv = ?; //: What is this?

// First time encryption
$res1 = mcrypt_encrypt($chiper, $key1, $data, $mode, $iv);

// Get the last block to use as key in second encryption
$last_block1 = substr($res1, strlen($res1)-8, 8);

// Second time encryption
$res2 = mcrypt_encrypt($chiper, $last_block1, $data, $mode, $iv);

// Then get the last block of $res2 and convert to hex
$last_block2 = substr($res2, strlen($res2)-8, 8);
$hash = bin2hex($last_block2);

Regards Jostein Skaar