php 加密 解密 密码传输

php 加密 解密 密码传输

 

<?php
    /* *
     * 使用按位异或运算 加密
     * $str 明文
     * $salt 盐
     * */
    public static function xor_encrypt($str,$salt = 'app') {
        $key = sha1($salt);
        $result =  base64_encode($str ^ $key);
        return $result;
    }

    /* *
     * 使用按位异或运算 解密
     * $encrypt_str 密文
     * $salt 盐
     * */
    public static function xor_decrypt($encrypt_str,$salt = 'app') {
        $key = sha1($salt);
        $str = base64_decode($encrypt_str);
        $result = $str ^ $key;
        return $result;
        if(json_decode($result) === null){
            //解密失败,出现乱码
            return '';
        }
        return $result;
    }
    //加密 
    $encrypt_str = (xor_encrypt('321')); 
    //解密 
    $str = xor_decrypt($encrypt_str);
    print_r(['encrypt_str'=>$encrypt_str,'str'=>$str]);
?>

 

posted @ 2018-07-12 10:32  一个人的孤独自白  阅读(437)  评论(0编辑  收藏  举报