php使用AES-128-CBC加密解密

注:需要开启openssl扩展

 
    public static $key; // 秘钥
    public static $iv; // 偏移量
    public function __construct()
    {    
        self::$key = '123123';
        self::$iv  = '567567';
    }
    
    public function encryptWithOpenssl($data = '')
    {   
        return base64_encode(openssl_encrypt($data, "AES-128-CBC", self::$key, OPENSSL_RAW_DATA, self::$iv));
    }
    
    public function decryptWithOpenssl($data = '')
    {   
        
        return openssl_decrypt(base64_decode($data), "AES-128-CBC", self::$key, OPENSSL_RAW_DATA, self::$iv);
    }

// 使用
$arr = ['status' => '1', 'info' => 'success'];
$str = json_encode($arr);
$encrypt_str = $that->encryptWithOpenssl($str);
var_dump($encrypt_str);
echo '
'
; $decrypt_str = that->decryptWithOpenssl($encrypt_str); var_dump($decrypt_str);

你可能感兴趣的:(php,tp5,tp3整理,php)