Chinaunix首页 | 论坛 | 博客
  • 博客访问: 460465
  • 博文数量: 141
  • 博客积分: 211
  • 博客等级: 入伍新兵
  • 技术积分: 1049
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-17 16:25
个人简介

如此经年,望尽千帆。

文章分类

全部博文(141)

文章存档

2014年(73)

2013年(65)

2012年(3)

我的朋友

分类: PHP

2014-03-23 22:29:13

php实现的aes加密类,代码中有使用方法。
php
//php aes加密类 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));
阅读(1131) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~