gongzhonghao.png
扫码关注我们
最新赞助活动温馨提示:自愿赞助服务器费用,学生和没有工作的整站资源免费下载!
头像

php实现的aes加密类

来源:http://erdangjiade.com/topic/1664.html 你好,世界。 2017-09-25 22:42浏览(33)

高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。经过五年的甄选流程,高级加密标准由美国国家标准与技术研究院(NIST)于2001年11月26日发布于FIPS PUB 197,并在2002年5月26日成为有效的标准。2006年,高级加密标准已然成为对称密钥加密中最流行的算法之一。这篇文章主要介绍了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));

以上就是php实现的aes加密类的详细内容,更多请关注二当家的素材网其它相关文章!

评论0
头像

友情提示:垃圾评论一律封号 加我微信:826096331拉你进VIP群学习群

1 2