php实现aes加密类分享
<?php
class AESMcrypt {
public $iv = null;
public $key = null;
public $bit = 128;
private $cipher;
public function __construct($bit, $key, $iv, $mode) {
if(empty($bit) || empty($key) || empty($iv) || empty($mode))
return NULL;
$this->bit = $bit;
$this->key = $key;
$this->iv = $iv;
$this->mode = $mode;
switch($this->bit) {
case 192:$this->cipher = MCRYPT_RIJNDAEL_192; break;
case 256:$this->cipher = MCRYPT_RIJNDAEL_256; break;
default: $this->cipher = MCRYPT_RIJNDAEL_128;
}
switch($this->mode) {
case 'ecb':$this->mode = MCRYPT_MODE_ECB; break;
case 'cfb':$this->mode = MCRYPT_MODE_CFB; break;
case 'ofb':$this->mode = MCRYPT_MODE_OFB; break;
case 'nofb':$this->mode = MCRYPT_MODE_NOFB; break;
default: $this->mode = MCRYPT_MODE_CBC;
}
}
public function encrypt($data) {
$data = base64_encode(mcrypt_encrypt( $this->cipher, $this->key, $data, $this->mode, $this->iv));
return $data;
}
public function decrypt($data) {
$data = mcrypt_decrypt( $this->cipher, $this->key, base64_decode($data), $this->mode, $this->iv);
$data = rtrim(rtrim($data), "\x00..\x1F");
return $data;
}
}
//使用方法
$aes = new AESMcrypt($bit = 128, $key = 'abcdef1234567890', $iv = '0987654321fedcba', $mode = 'cbc');
$c = $aes->encrypt('haowei.me');
var_dump($aes->decrypt($c));
相关文章
thinkPHP+PHPExcel实现读取文件日期的方法(含时分秒)
这篇文章主要介绍了thinkPHP+PHPExcel实现读取文件日期的方法,可实现针对时分秒的形式进行读取的功能,涉及PHPExcel中ExcelToPHP函数的相关使用方法,需要的朋友可以参考下2016-07-07php+ffmpeg如何获取视频缩略图、视频分辨率等相关信息
这篇文章主要给大家介绍了关于php+ffmpeg如何获取视频缩略图、视频分辨率等相关信息的相关资料,文中通过实例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2022-03-03PHP date()函数警告: It is not safe to rely on the system解决方法
这篇文章主要介绍了PHP date()函数警告: It is not safe to rely on the system解决方法,其实就是时区设置不正确造成的,本文提供了两种方法来解决这个问题,需要的朋友可以参考下2014-08-08
最新评论