PHP加密解密 第3章 passport PHP加密解密 第3章 passport

2023-06-15

函数

function passportEncrypt($txt, $key = 'lulublog'): string
{
    srand((double)microtime() * 1000000);
    $encryptKey = md5(rand(0, 32000));
    $ctr = 0;
    $tmp = '';
    for($i = 0;$i < strlen($txt); $i++) {
        $ctr = $ctr == strlen($encryptKey) ? 0 : $ctr;
        $tmp .= $encryptKey[$ctr].($txt[$i] ^ $encryptKey[$ctr++]);
    }
    return urlencode(base64_encode(passportKey($tmp, $key)));
}

function passportDecrypt($txt, $key = 'lulublog'): string
{
    $txt = passportKey(base64_decode(urldecode($txt)), $key);
    $tmp = '';
    for($i = 0;$i < strlen($txt); $i++) {
        $md5 = $txt[$i];
        $tmp .= $txt[++$i] ^ $md5;
    }
    return $tmp;
}

function passportKey($txt, $encryptKey): string
{
    $encryptKey = md5($encryptKey);
    $ctr = 0;
    $tmp = '';
    for($i = 0; $i < strlen($txt); $i++) {
        $ctr = $ctr == strlen($encryptKey) ? 0 : $ctr;
        $tmp .= $txt[$i] ^ $encryptKey[$ctr++];
    }
    return $tmp;
}

测试

$encrypt = passportEncrypt('lulublog欢迎您');
var_dump($encrypt);
$decrypt = passportDecrypt($encrypt);
var_dump($decrypt);

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开微信扫一扫,即可进行扫码打赏哦

阅读 224