<?php
/**
* 功能:Aes字符串加解密
*/
class AES
{
protected $key;
protected $method;
protected $iv;
protected $options;
public function __construct($method = 'AES-128-ECB', $iv = '', $options = OPENSSL_RAW_DATA )
{
$this->key = substr(md5("123456"),0,16);
#密钥参数
$this->method = $method;
$this->iv = $iv;
$this->options = $options;
}
/**
*
* @param string $string 需要加密的字符串
* @return string
*/
public function encrypt( $string )
{
// $key = substr(openssl_digest(openssl_digest($string, 'sha1', true), 'sha1', true), 0, 16);
// $data = openssl_encrypt($string, 'AES-128-ECB', $key, OPENSSL_RAW_DATA);
// return urlencode(base64_encode($data));
// if (strlen($string) % 16) {
// $string = str_pad($string,strlen($string) + 16 - strlen($string) % 16, "\0");
// }
// $encrypted = openssl_encrypt($string, 'AES-128-ECB',$this->key,OPENSSL_ZERO_PADDING,$this->iv);
// return base64_encode($encrypted);
return base64_encode(openssl_encrypt($string, $this->method,$this->key,$this->options,$this->iv));
}
/**
* @param string $string 需要解密的字符串
* @param string $key 密钥
* @return string
*/
public function decrypt($string)
{
return openssl_decrypt(base64_decode($string), $this->method, $this->key, $this->options,$this->iv);
}
}