PHP 加密解密

有些话、适合烂在心里 提交于 2020-03-18 11:26:51

某厂面试归来,发现自己落伍了!>>>


function enToken($txt) {
    $key   = 'colin';
    $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-";
    $nh    = rand(0, 64);
    $ch    = $chars[ $nh ];
    $mdKey = md5($key . $ch);
    $mdKey = substr($mdKey, $nh % 8, $nh % 8 + 7);
    $txt   = base64_encode($txt);
    $tmp   = '';
    $k     = 0;
    for ($i = 0; $i < strlen($txt); $i++) {
        $k   = $k == strlen($mdKey) ? 0 : $k;
        $j   = ($nh + strpos($chars, $txt[ $i ]) + ord($mdKey[ $k++ ])) % 64;
        $tmp .= $chars[ $j ];
    }

    return urlencode($ch . $tmp);
}

function unToken($txt) {
    $key   = 'colin';
    $txt   = urldecode($txt);
    $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-";
    $ch    = $txt[0];
    $nh    = strpos($chars, $ch);
    $mdKey = md5($key . $ch);
    $mdKey = substr($mdKey, $nh % 8, $nh % 8 + 7);
    $txt   = substr($txt, 1);
    $tmp   = '';
    $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 += 64;
        $tmp .= $chars[ $j ];
    }

    return base64_decode($tmp);
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!