美文网首页
PHP加密与解密函数

PHP加密与解密函数

作者: 哈叽哈叽叽歪歪 | 来源:发表于2018-09-27 09:43 被阅读0次
    //解密函数
        public static function decode($txt, $key = '*****')
        {
            $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 = '';
            $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 += 64;
                $tmp .= $chars[$j];
            }
            return base64_decode($tmp);
        }
    
        //加密函数
        public static function encrypt($txt, $key = '******')
        {
            $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 = '';
            $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++])) % 64;
                $tmp .= $chars[$j];
            }
            return urlencode($ch . $tmp);
        }
    

    相关文章

      网友评论

          本文标题:PHP加密与解密函数

          本文链接:https://www.haomeiwen.com/subject/yhgsoftx.html