php Aes加解密方法

使用aes加密传输,并解密的方法

class Aesuntils{

    private $aes_key = "duobakejihxhb220";
    private $aes_vi = "EkpTEA3FbZlbh050";

    function aes_encrypt($content){
        $content = json_encode($content);
        $content = $this->pkcs7_pad($content); //支持openssl_decrypt 解密,
        $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128,$this->aes_key,$content,MCRYPT_MODE_CBC,$this->aes_vi);
        $based_encrypted = base64_encode($encrypted);
        return $based_encrypted;
    }
    /*
     * mcrypt_encrypt 解密
     * */
    function aes_decrypt($content){
         
        $content = base64_decode($content);
        $encrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128,$this->aes_key,$content,MCRYPT_MODE_CBC,$this->aes_vi);
        $str_res = rtrim($encrypted, ""); //由于字符编码问题,解密出来会出现 特殊字符,需要进行移除
        return $str_res;
    }
    function pkcs7_pad($str){
        $len = mb_strlen($str, '8bit');
        $c = 16 - ($len % 16);
        $str .= str_repeat(chr($c), $c);
        return $str;
    }
 
}

使用方法:

use app\api\controller\Aesuntils;

class Wechattiaozhuan{
		
		public function ceshi(){
			$arss = [
            "order_id" => $data['order_id'],
            "act_num" => "19244292",
            "user_id" => $res['user_id']
        ];

	        $Aesuntils = new Aesuntils();
	        $comquery= $Aesuntils->aes_encrypt($arss); //加密字符串
			$desc = $Aesuntils->aes_decrypt($comquery); //解密字符串
		}


}

你可能感兴趣的:(php,安全)