微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

PHP生成MD5为基础的分组密码,感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编来看看吧。
经测试代码如下:

/**
 * 生成MD5为基础的分组密码
 *
 * @param 
 * @arrange (编程之家) 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)   ***/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐