找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1699|回复: 0
打印 上一主题 下一主题

php实现aes加密类分享

[复制链接]

2617

主题

2617

帖子

7789

积分

论坛元老

Rank: 8Rank: 8

积分
7789
跳转到指定楼层
楼主
发表于 2018-2-14 06:00:31 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

            [U]复制代码[/U] 代码如下:
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 192this->cipher = MCRYPT_RIJNDAEL_192; break;
   case 256this->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加密解密的例子小结
  • php写的AES加密解密类分享
  • PHP实现AES256加密算法实例
  • PHP aes (ecb)解密后乱码问题
  • PHP的AES加密算法完整实例
  • PHP 7.1中AES加解密方法mcrypt_module_open()的替换方案
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端