PHP、js加密与解密函数

PHP版本

//解密函数
    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);
    }

JS版本

function: encrypt(txt, key){ //加密
                    varchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
                    var nh = Math.floor(Math.random() * 65) ; 
                    var ch = chars[nh];
                    var mdKey = md5(key + ch);
                    var mdKey = mdKey.substr(nh % 8, nh % 8 + 7);
                    txt = Base64.encode(txt);
            
                    var tmp = '',j = 0, k = 0;
                    for (var i = 0; i < txt.length; i++) {
                        k = k == mdKey.length ? 0 : k;
                        j = (nh + chars.indexOf(txt[i]) + mdKey[k++].charCodeAt(0)) % 64;
                        tmp += chars[j];
                    }
                    return encodeURI(ch + tmp);
                },
 function: decode(txt, key){//解密
                    txt = decodeURI(txt);
                    var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
                    var ch = txt[0];
                    var nh = chars.indexOf(ch);
                    var mdKey = md5(key + ch);
                    mdKey = mdKey.substr(nh % 8, nh % 8 + 7);
                    txt = txt.substr(1);
                    var tmp = '', j = 0, k = 0;
                    for (var i = 0; i < txt.length; i++) {
                        k = k == mdKey.length ? 0 : k;
                        j = chars.indexOf(txt[i]) - nh - mdKey[k++].charCodeAt(0);
                        while (j < 0) {
                            j += 64;
                        }
                        tmp += chars[j];
                    }
                    return Base64.decode(tmp);
                },

你可能感兴趣的:(PHP、js加密与解密函数)