MD5-based block cipher

Below is a MD5-based block cipher (MDC-like), which works in 128bit CFB mode.
It is very useful to encrypt secret data before transfer it over the network.

function md5_encrypt($plain_text, $password, $iv_len = 16){
    $plain_text .= "\x13";
    $n = strlen($plain_text);
    if ($n % 16) $plain_text .= str_repeat("\0", 16 - ($n % 16));
    $i = 0;
    $enc_text = get_rnd_iv($iv_len);
    $iv = substr($password ^ $enc_text, 0, 512);
    while ($i < $n) {
        $block = substr($plain_text, $i, 16) ^ pack('H*', md5($iv));
        $enc_text .= $block;
        $iv = substr($block . $iv, 0, 512) ^ $password;
        $i += 16;
    }
    return base64_encode($enc_text);
}
 
function md5_decrypt($enc_text, $password, $iv_len = 16){
    $enc_text = base64_decode($enc_text);
    $n = strlen($enc_text);
    $i = $iv_len;
    $plain_text = '';
    $iv = substr($password ^ substr($enc_text, 0, $iv_len), 0, 512);
    while ($i < $n) {
        $block = substr($enc_text, $i, 16);
        $plain_text .= $block ^ pack('H*', md5($iv));
        $iv = substr($block . $iv, 0, 512) ^ $password;
        $i += 16;
    }
    return preg_replace('/\\x13\\x00*$/', '', $plain_text);
}
 
function get_rnd_iv($iv_len){
    $iv = '';
    while ($iv_len-- > 0) {
        $iv .= chr(mt_rand() & 0xff);
    }
    return $iv;
}
Snippet Details



//
// Example
//
 
$plain_text = 'very secret string';
$password = 'very secret password';
 
print "plain text is: [${plain_text}]<br />\n";
print "password is: [${password}]<br />\n";
 
$enc_text = md5_encrypt($plain_text, $password);
print "encrypted text is: [${enc_text}]<br />\n";
 
$plain_text2 = md5_decrypt($enc_text, $password);
print "decrypted text is: [${plain_text2}]<br />\n";

Sorry folks, comments have been deactivated for now due to the large amount of spam.

Please try to post your questions or problems on a related programming board, a suitable mailing list, a programming chat-room,
or use a QA website like stackoverflow because I'm usually too busy to answer any mails related
to my code snippets. Therefore please just mail me if you found a serious bug... Thank you!


Older comments:

None.