一种可逆的PHP加密方法

//仅适用于小字母数字和-混合的字符串加密/解密
function mine_encode($txt,$key='www.zwtt8.com'){
    $chars = "abcdefghijklmnopqrstuvwxyz0123456789-";
    $chars_len = strlen($chars);
    $nh = rand(0,36);
    $ch = $chars[$nh];
    $mdKey = md5($key.$ch);
    $mdKey = substr($mdKey,$nh%8, $nh%8+7);
    $tmp = '';
    $i=0;$j=0;$k = 0;
    for ($i=0; $i<strlen($txt); $i++) {
        $k = $k == strlen($mdKey) ? 0 : $k;
        $j = ($nh+strpos($chars,$txt[$i])+ord($mdKey[$k++]))%$chars_len;
        $tmp .= $chars[$j];
    }
    return urlencode($ch.$tmp);
}

function mine_decode($txt,$key='www.zwtt8.com'){
	$txt = urldecode($txt);
    $chars = "abcdefghijklmnopqrstuvwxyz0123456789-";
    $chars_len = strlen($chars);
    $ch = $txt[0];
    $nh = strpos($chars,$ch);
    $mdKey = md5($key.$ch);
    $mdKey = substr($mdKey,$nh%8, $nh%8+7);
    $txt = substr($txt,1);
    $tmp = '';
    $i=0;$j=0; $k = 0;
    for ($i=0; $i<strlen($txt); $i++) {
        $k = $k == strlen($mdKey) ? 0 : $k;
        $j = strpos($chars,$txt[$i])-$nh - ord($mdKey[$k++]);
        while ($j<0) $j+=$chars_len;
        $tmp .= $chars[$j];
    }
    return ($tmp);
}


$stra = '606-bb4358sdf-3';
$strl = mine_encode($stra);
var_dump($stra);
var_dump($strl);
var_dump(mine_decode($strl));

Mine吧 » 一种可逆的PHP加密方法

发表评论