<?php

function get_rnd_iv($iv_len)
{
    $iv = '';
    while ($iv_len-- > 0) {
        $iv .= chr(mt_rand() & 0xff);
    }
    return $iv;
}

function md2_encrypt($plain_text, $password, $iv_len = 16)
{
    $plain_text .= "\x13";
    echo "<br>".$plain_text;
    $n = strlen($plain_text);
    echo "<br>".$n;
    if ($n % 16) $plain_text .= str_repeat("\0", 16 - ($n % 16));
    $i = 0;
    $enc_text = get_rnd_iv($iv_len);
    echo "<br>".$enc_text;
    $iv = substr($password ^ $enc_text, 0, 512);
    echo "<br>".$iv;
    while ($i < $n) {
        $block = substr($plain_text, $i, 16) ^ pack('H*', md2($iv));
        echo "<br>".$block;
        $enc_text .= $block;
        echo "<br>".$enc_text;
        $iv = substr($block . $iv, 0, 512) ^ $password;
        echo "<br>".$iv;
        $i += 16;
    }

    $enc=base64_encode($enc_text); 
    echo "<br>".$enc;
     return $enc;
}

function md2_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*', md2($iv));
        $iv = substr($block . $iv, 0, 512) ^ $password;
        echo "<br>".$iv;
        $i += 16;
    }
    return preg_replace('/\\x13\\x00*$/', '', $plain_text);
}/******************************************/
$plain_text = 'network security';
$password = 'very secret password';
echo "plain text is: [${plain_text}]<br />\n";
echo "password is: [${password}]<br />\n";

$enc_text = md2_encrypt($plain_text, $password);
echo "encrypted text is: [${enc_text}]<br />\n";

$plain_text2 = md2_decrypt($enc_text, $password);
echo "decrypted text is: [${plain_text2}]<br />\n";

?>

this is md5(hash code with encryption) code work for both md5 and SHA1
but generate error with md4.
help me in this code plz.........
i want hash funtion md4 with encryption and decryption.

Please fix your post to use code tags properly. Your code is showing up as text.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.