php加密解密算法,也可以用于短域名生成

作者:袖梨 2022-06-25

加密:

function string_secret($string)
{
    $key = "yadgen";
 
    //密锁串,不能出现重复字符,内有A-Z,a-z,0-9,/,=,+,_,
    $lockstream = 'st=lDEFABCNOPyzghi_jQRST-UwxkVWXYZabcdef+IJK6/7nopqr89LMmGH012345uv';
    //随机找一个数字,并从密锁串中找到一个密锁值
    $lockLen = strlen($lockstream);
    $lockCount = rand(0,$lockLen-1);
    $randomLock = $lockstream[$lockCount];
    //结合随机密锁值生成MD5后的密码
    $password = md5($key.$randomLock);
    //开始对字符串加密
    $txtStream = base64_encode($string);
    $tmpStream = '';
    $i=0;$j=0;$k = 0;
    for ($i=0; $i         $k = ($k == strlen($password)) ? 0 : $k;
        $j = (strpos($lockstream,$txtStream[$i])+$lockCount+ord($password[$k]))%($lockLen);
        $tmpStream .= $lockstream[$j];
        $k++;
    }
    return $tmpStream.$randomLock;
}

解密:


function secret_string($string)
{
    $key = "yadgen";
 
    //密锁串,不能出现重复字符,内有A-Z,a-z,0-9,/,=,+,_,
    $lockstream = 'st=lDEFABCNOPyzghi_jQRST-UwxkVWXYZabcdef+IJK6/7nopqr89LMmGH012345uv';
 
    $lockLen = strlen($lockstream);
    //获得字符串长度
    $txtLen = strlen($string);
    //截取随机密锁值
    $randomLock = $string[$txtLen - 1];
    //获得随机密码值的位置
    $lockCount = strpos($lockstream,$randomLock);
    //结合随机密锁值生成MD5后的密码
    $password = md5($key.$randomLock);
    //开始对字符串解密
    $txtStream = substr($string,0,$txtLen-1);
    $tmpStream = '';
    $i=0;$j=0;$k = 0;
    for($i=0; $i     $k = ($k == strlen($password)) ? 0 : $k;
    $j = strpos($lockstream,$txtStream[$i]) - $lockCount - ord($password[$k]);
    while($j < 0){
    $j = $j + ($lockLen);
    }
    $tmpStream .= $lockstream[$j];
    $k++;
    }
    return base64_decode($tmpStream);
}

相关文章

精彩推荐