PHP 生成MD5为基础的分组密码

前端之家收集整理的这篇文章主要介绍了PHP 生成MD5为基础的分组密码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
PHP生成MD5为基础的分组密码,感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编来看看吧。
经测试代码如下:

/**
 * 生成MD5为基础的分组密码
 *
 * @param 
 * @arrange (512.笔记) jb51.cc
 **/
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,512);
    while ($i < $n) {
        $block = substr($plain_text,$i,16) ^ pack('H*',md5($iv));
        $enc_text .= $block;
        $iv = substr($block . $iv,512) ^ $password;
        $i += 16;
    }
    return base64_encode($enc_text);
}
 
function md5_decrypt($enc_text,$iv_len = 16){
    $enc_text = base64_decode($enc_text);
    $n = strlen($enc_text);
    $i = $iv_len;
    $plain_text = '';
    $iv = substr($password ^ substr($enc_text,$iv_len),512);
    while ($i < $n) {
        $block = substr($enc_text,16);
        $plain_text .= $block ^ pack('H*',md5($iv));
        $iv = substr($block . $iv,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;
}
 
//范例:
//
// 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";


/***   来自编程之家 jb51.cc(jb51.cc)   ***/
原文链接:https://www.f2er.com/php/528930.html

猜你在找的PHP相关文章