相關推薦:《PHP教學》
class Aes { public $key = ''; public $iv = ''; public function __construct($config) { foreach($config as $k => $v){ $this->$k = $v; } } //加密 public function aesEn($data){ return base64_encode(openssl_encrypt($data, $this->method,$this->key, OPENSSL_RAW_DATA , $this->iv)); } //解密 public function aesDe($data){ return openssl_decrypt(base64_decode($data), $this->method, $this->key, OPENSSL_RAW_DATA, $this->iv); } } $config = [ 'key'=>'reter4446fdfgdfgdfg', //加密key 'iv'=> md5(time(). uniqid(),true), //保證偏移量為16位元 'method'=> 'AES-128-CBC' //加密方式 # AES-256-CBC等 ]; $obj = new Aes($config); $res = $obj->aesEn('aaaddee44');//加密資料 echo $res; echo '<hr>'; echo $obj->aesDe($res);//解密
以上就是PHP如何使用AES加密和解密的詳細內容,更多請關注TW511.COM其它相關文章!