一个简单的PHP字符串加减密函数

2022年12月27日 1929点热度 0人点赞 0条评论
    /**
     * 字符串加密
     * @param $str
     * @param string $key1
     * @param string $key2
     * @return string
     */
    function encode_z($str, $key1 = "", $key2 = "") {
        global $config;
        $b = array();
        $c = array();
        $s = '';
        if (strlen($key1) != 16) $key1 = $config['SECURE_KEY1'];
        if (strlen($key2) != 16) $key2 = $config['SECURE_KEY2'];
        if (function_exists('gzcompress'))
            $str = gzcompress($str);
        for ($i = 0; $i < strlen($str); $i++) {
            $s = ord(substr($str, $i, 1));
            $y = $s % 16;
            $n = intval($s / 16);
            $b [] = substr($key2, $y, 1);
            $c [] = substr($key1, $n, 1);
        }
        $m = substr(md5(md5(implode('', $c)) . implode('', $b)), 3, 12);
        return implode('', $b) . implode('', $c) . $m;
    }

    /**
     * 字符串解密
     * @param $str
     * @param string $key1
     * @param string $key2
     * @return bool|string
     */
    function decode_z($str, $key1 = "", $key2 = "") {
        global $config;
        if (strlen($key1) != 16) $key1 = $config['SECURE_KEY1'];
        if (strlen($key2) != 16) $key2 = $config['SECURE_KEY2'];
        $str = strtolower($str);
        $m = substr($str, strlen($str) - 12);
        $str = substr($str, 0, strlen($str) - 12);
        $s1 = substr($str, 0, strlen($str) / 2);
        $s2 = substr($str, strlen($str) / 2);
        if ($m != substr(md5(md5($s2) . $s1), 3, 12))
            return false;
        $ret = '';
        for ($i = 0; $i < strlen($s1); $i++) {
            $s3 = substr($s1, $i, 1);
            $s4 = substr($s2, $i, 1);
            $y = strpos($key2, $s3);
            $n = strpos($key1, $s4);
            $ret .= chr($n * 16 + $y);
        }
        if (function_exists('gzuncompress'))
            $ret = gzuncompress($ret);
        return $ret;
    }

路灯

这个人很懒,什么都没留下

文章评论