AES加密解密CBC

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a114469/article/details/87800685
    //test aes加密解密
    public function aes(){
        //输入明文
	$data = input('a');

        //加密 
        //privateKey 为16、24或32字节密钥
        $privateKey = "1234567812345678";
        $enc = $this->encrypt($data,$privateKey,$privateKey);
        echo '加密:<br>';
        echo $enc;
        echo '<br>';

        //解密
        $res = $this->decrypt($enc,$privateKey,$privateKey);
        echo '解密:<br>';
        echo $res;
        echo '<br>';
        //return $decrypted;
    }

    # === 注意 ===
    #MCRYPT_RIJNDAEL_128 、MCRYPT_RIJNDAEL_192、MCRYPT_RIJNDAEL_256 对应密钥大小

    /**
     * AES加密
     * @param $content
     * @param $key
     * @param $iv
     * @return string
     */
    function encrypt($content, $key, $iv)
    {
        return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $content, MCRYPT_MODE_CBC, $iv));
    }

    /**
     * AES解密
     * @param $content
     * @param $key
     * @param $iv
     * @return string
     */
    function decrypt($content, $key, $iv)
    {
        return mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, base64_decode($content), MCRYPT_MODE_CBC, $iv);
    }

需要安装Libmcrypt,mhash,mcrypt

猜你喜欢

转载自blog.csdn.net/a114469/article/details/87800685