多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
**AES 加密解密,可用于接口数据提供给第三方使用。** 使用 `AES-128-ECB`方式。 ~~~ aes_encode($data,$type='AES-128-ECB',$key='',$iv='') aes_decode($data,$type='AES-128-ECB',$key='',$iv='') ~~~ **事例代码** 1.加密 ~~~ $key = "516610f18f"; $iv = "6fc5328948e9edd17d"; $title = "demo15"; $data = [ 'title'=> $title, 'time' => time(), ]; $data = json_encode($data); echo urlencode(aes_encode($data,$type='AES-128-ECB',$key,$iv)); ~~~ 2.url参数形式解密 ~~~ $sign = $_GET['sign']; $title = $_GET['title']; if(!$sign){ exit('Params error'); } //$res怎么来的,自己看着办 $secret_key = $res['secret_key']; $secret_id = $res['secret_id']; $sign = urldecode($sign); $arr = aes_decode($sign,'AES-128-ECB',$secret_id,$secret_key); $arr = json_decode($arr,true); if($arr['title']){ if($arr['time'] < time()-300){ exit('Access Deny'); } //这里就解密成功了。可以添加自己的逻辑。 } exit('Access Deny'); ~~~ ***** 《《《本系统不用看以下代码》》》 ***** **以下在需要使用到的方法或类,本系统已内置** ~~~ /** * AES加密 // aes 加密 $config['aes_key'] = "123456"; $config['aes_iv'] = md5('app_sun'); $token = urlencode(aes_encode($d)); */ function aes_encode($data,$type='AES-128-ECB',$key='',$iv=''){ global $config; if(!$key){ $key = $config['aes_key']; } if(!$iv){ $iv = $config['aes_iv']; } $obj = new \lib\Aes($key,$type,$key,$iv); return base64_encode($obj->encrypt($data)); } /** * AES解密 $token = $_GET['token']; $token = aes_decode($token); pr($token); */ function aes_decode($data,$type='AES-128-ECB',$key='',$iv=''){ global $config; if(!$key){ $key = $config['aes_key']; } if(!$iv){ $iv = $config['aes_iv']; } $data = base64_decode($data); $obj = new \lib\Aes($key,$type,$key,$iv); return $obj->decrypt($data); } ~~~ 类实现 ~~~ <?php namespace lib; class Aes { /** * var string $method 加解密方法,可通过openssl_get_cipher_methods()获得 */ protected $method; /** * var string $secret_key 加解密的密钥 */ protected $secret_key; /** * var string $iv 加解密的向量,有些方法需要设置比如CBC */ protected $iv; /** * var string $options (不知道怎么解释,目前设置为0没什么问题) */ protected $options; /** * 构造函数 * * @param string $key 密钥 * @param string $method 加密方式 * @param string $iv iv向量 * @param mixed $options 还不是很清楚 * */ public function __construct($key, $method = 'AES-128-ECB', $iv = '', $options = 0) { // key是必须要设置的 $this->secret_key = isset($key) ? $key : 'morefun'; $this->method = $method; $this->iv = $iv; $this->options = $options; } /** * 加密方法,对数据进行加密,返回加密后的数据 * * @param string $data 要加密的数据 * * @return string * */ public function encrypt($data) { return openssl_encrypt($data, $this->method, $this->secret_key, $this->options, $this->iv); } /** * 解密方法,对数据进行解密,返回解密后的数据 * * @param string $data 要解密的数据 * * @return string * */ public function decrypt($data) { return openssl_decrypt($data, $this->method, $this->secret_key, $this->options, $this->iv); } } ~~~