函数
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);