<?php
namespace app\common\lib;
/**
-
aes 加密 解密类库
-
@by singwa
-
Class Aes
-
@package app\common\lib
*/
class Aes {private $key = null;
/**
*- @param $key 密钥
- @return String
*/
public function __construct() {
// 需要小伙伴在配置文件app.php中定义aeskey
$this->key = config(‘app.aes_key’);
}
/**
-
加密
-
@param String input 加密的字符串
-
@param String key 解密的key
-
@return HexString
*/
public function encrypt($input = ‘’) {
$size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
$input = input, $size);
$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, ‘’, MCRYPT_MODE_ECB, ‘’);
td), MCRYPT_RAND);
mcrypt_generic_init($td, $this->key, $iv);td, td);
mcrypt_module_close($td);
data);return $data;
}
/**- 填充方式 pkcs5
- @param String text 原始字符串
- @param String blocksize 加密长度
- @return String
*/
private function pkcs5_pad($text, $blocksize) {
$pad = text) % $blocksize);
return pad), $pad);
}
/**
-
解密
-
@param String input 解密的字符串
-
@param String key 解密的key
-
@return String
*/
public function decrypt($sStr) {
this->key,base64_decode($sStr), MCRYPT_MODE_ECB);
decrypted);
decrypted[$dec_s-1]);
decrypted, 0, -$padding);return $decrypted;
}
}