PHP怎样运用AES加密和解密_后端开发

PHP怎样运用AES加密和解密_后端开发

后端开发2019-12-05 2:5650ki4网
AES加密在php5的版本中运用的mcrypt_decrypt 函数,该函数已经在php7.1后弃用了,取而代之的是openssl的openssl_encrypt和openssl_decrypt,而且代码也异常精简,下面是示例代码:

相干引荐:《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加密和解密的细致内容,更多请关注ki4网别的相干文章!

炫龙网络 Copyright ©  本站由阿里云强力驱动
PHP怎样运用AES加密和解密_后端开发