很好用的 AES 加密的类,ECB 模式,PKCS7 padding 填充方式。
<?php
class AES
{
protected $cipher;
protected $mode;
protected $pad_method;
protected $secret_key;
protected $iv;
public function __construct($key, $method = 'pkcs7', $iv = '', $mode = MCRYPT_MODE_ECB, $cipher = MCRYPT_RIJNDAEL_128)
{
$this->secret_key = $key;
$this->pad_method =$method;
$this->iv = $iv;
$this->mode = $mode;
$this->cipher = $cipher;
}
protected function pad_or_unpad($str, $ext)
{
if (!is_null($this->pad_method)) {
$func_name = __CLASS__ . '::' . $this->pad_method . '_' . $ext . 'pad';
if (is_callable($func_name)) {
$size = mcrypt_get_block_size($this->cipher, $this->mod