怎么在php中实现一个aes加密类(aes,php,开发技术)

时间:2024-04-28 03:03:19 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

    %E6%80%8E%E4%B9%88%E5%9C%A8php%E4%B8%AD%E5%AE%9E%E7%8E%B0%E4%B8%80%E4%B8%AAaes%E5%8A%A0%E5%AF%86%E7%B1%BB

复制代码 代码如下:


<?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加密类的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:如何编写回帖脱衣服的图片的代码下一篇:

15 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18