[PHP高可用后端]②④--Sign机制解剖

[PHP高可用后端]②④--Sign机制解剖_第1张图片
image.png
[PHP高可用后端]②④--Sign机制解剖_第2张图片
image.png
[PHP高可用后端]②④--Sign机制解剖_第3张图片
image.png

Aes.php

key = config('app.aeskey');
    }

    /**
     * 加密 客户端工程师也需要相应的加密模式和填充方式
     * @param string $input
     * @return string
     */
    public function encryt($input = '')
    {
        $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
        $input = $this->pkcs5_pad($input, $size);
        $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
        $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
        mcrypt_generic_init($td, $this->key, $iv);

        $data = mcrypt_generic($td, $input);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        $data = base64_encode($data);

        return $data;

    }

    /**
     * 填充方式 pkcs5
     * @param string $text 原始字符串
     * @param string $blocksize 加密长度
     * @return  string
     */
    private function pkcs5_pad($text, $blocksize)
    {
        $pad = $blocksize - (strlen($text) % $blocksize);
        return $text . str_repeat(chr($pad), $pad);
    }

    /**
     * 解密
     * @param string $sStr 解密的字符串
     * @return string bool|string  解密的key
     * @return string
     */
    public function decrypt($sStr)
    {
        $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128,
            $this->key, base64_decode($sStr), MCRYPT_MODE_ECB);
        $dec_s = strlen($decrypted);
        $padding = ord($decrypted[$dec_s - 1]);
        $decrypted = substr($decrypted, 0, -$padding);

        return $decrypted;
    }

}

app.php

 '_#sing_ty',
    'aeskey' => 'sgg45747ss223455',//aes密钥,服务端和客户端必须保持一致
];

IAuth.php

encryt($string);
        return $string;
    }

}

Common.php

checkRequestAuth();
    }

    /**
     * 检查每次app请求的数据是否合法
     */
    public function checkRequestAuth()
    {
        //首先需要获取headers
        $headers = request()->header();
        $this->testAes();
        /**
         * array (size=16)
         * 'host' => string 'singwa.com' (length=10)
         * 'connection' => string 'keep-alive' (length=10)
         * 'content-length' => string '19' (length=2)
         * 'origin' => string 'chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop' (length=51)
         * 'model' => string 'sanxing5.6' (length=10)
         * 'user-agent' => string 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36' (length=115)
         * 'content-type' => string 'application/x-www-form-urlencoded' (length=33)
         * 'did' => string '231456' (length=6)
         * 'app_type' => string 'android' (length=7)
         * 'cache-control' => string 'no-cache' (length=8)
         * 'postman-token' => string '851c0def-ae2f-baa3-a2fd-c772f1d9f939' (length=36)
         * 'sign' => string 'sdjskjdskj' (length=10)
         * 'version' => string '1' (length=1)
         * 'accept' => string  (length=3)
         * 'accept-encoding' => string 'gzip, deflate' (length=13)
         * 'accept-language' => string 'zh-CN,zh;q=0.8' (length=14)
         */
        //halt($headers);

        //sign 加密需要 客户端工程师 解密:服务端工程师

    }

    public function testAes()
    {
        //$str = "id=1&ms=45&username=singwa";
        //6dDiaoQrSC2tPepBYWGFh8ri8FNeKXBwRFKbn3hv8qA=
        //echo (new Aes())->encryt($str);

        //$str = "6dDiaoQrSC2tPepBYWGFh8ri8FNeKXBwRFKbn3hv8qA=";
        //id=1&ms=45&username=singwa
        //echo (new Aes())->decrypt($str);

        $data = [
            'did'=>'12345dg',
            'version'=>1,
        ];

        //sRCvj52mZ8G+u2OdHYwmysvczmCw+RrAYWiEaXFI/5A=
        //echo IAuth::setSign($data);

        $str="sRCvj52mZ8G+u2OdHYwmysvczmCw+RrAYWiEaXFI/5A=";
        echo (new Aes())->decrypt($str);//did=12345dg&version=1

        exit;
    }
}

Test.php

[PHP高可用后端]②④--Sign机制解剖_第4张图片
image.png

你可能感兴趣的:([PHP高可用后端]②④--Sign机制解剖)