php aes加密教程,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));

www.bkjia.comtrueTechArticle复制代码 代码如下: ?php class AESMcrypt { public $iv = null; public $key = null; public $bit = 128; private $cipher; public function __construct($bit, $key, $iv, $mode) { i…

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值