js encryption PHP decryption

Usage scenario: When searching or passing values ​​in the browser, many special characters will be encountered in many cases, and they will be lost when getting, so when passing the value to the browser, js encrypts it, and then decrypts it in the backend php. You won't get lost in the middle!

I tried to use crypto-js, but this kind of encryption will bring special symbols such as /+, and the browser will also block it!

//js字符加密
function forJs(s){
    var es = [],c='',ec='';
    s = s.split('');//10.19补 忘记ie不能下标访问字符串
    for(var i=0,length=s.length;i<length;i++){
        c = s[i];
        ec = encodeURIComponent(c);
        if(ec==c){
            ec = c.charCodeAt().toString(16);
            ec = ('00' + ec).slice(-2);
        }
        es.push(ec);
    }
    return es.join('').replace(/%/g,'').toUpperCase();
}
// php解密函数
public function forPhp($s){
    $a = str_split($s,2);
    $s = '%' . implode('%',$a);
    return urldecode($s);
}

Guess you like

Origin blog.csdn.net/munchmills/article/details/131671501