|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
// +---------------------------------------------------------------------- |
|
4
|
|
|
// | ThinkLibrary 6.0 for ThinkPhP 6.0 |
|
5
|
|
|
// +---------------------------------------------------------------------- |
|
6
|
|
|
// | 版权所有 2017~2020 [ https://www.dtapp.net ] |
|
7
|
|
|
// +---------------------------------------------------------------------- |
|
8
|
|
|
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary |
|
9
|
|
|
// +---------------------------------------------------------------------- |
|
10
|
|
|
// | 开源协议 ( https://mit-license.org ) |
|
11
|
|
|
// +---------------------------------------------------------------------- |
|
12
|
|
|
// | gitee 仓库地址 :https://gitee.com/liguangchun/ThinkLibrary |
|
13
|
|
|
// | github 仓库地址 :https://github.com/GC0202/ThinkLibrary |
|
14
|
|
|
// | gitlab 仓库地址 :https://gitlab.com/liguangchun/thinklibrary |
|
15
|
|
|
// | weixin 仓库地址 :https://git.weixin.qq.com/liguangchun/ThinkLibrary |
|
16
|
|
|
// | huaweicloud 仓库地址 :https://codehub-cn-south-1.devcloud.huaweicloud.com/composer00001/ThinkLibrary.git |
|
17
|
|
|
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library |
|
18
|
|
|
// +---------------------------------------------------------------------- |
|
19
|
|
|
|
|
20
|
|
|
namespace DtApp\ThinkLibrary\service\crypt; |
|
21
|
|
|
|
|
22
|
|
|
use DtApp\ThinkLibrary\Service; |
|
23
|
|
|
|
|
24
|
|
|
class AesService extends Service |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var |
|
28
|
|
|
*/ |
|
29
|
|
|
private $key, $iv; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param $str |
|
33
|
|
|
* @return $this |
|
34
|
|
|
*/ |
|
35
|
|
|
public function key($str): self |
|
36
|
|
|
{ |
|
37
|
|
|
$this->key = $str; |
|
38
|
|
|
return $this; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param $str |
|
43
|
|
|
* @return $this |
|
44
|
|
|
*/ |
|
45
|
|
|
public function iv($str) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->iv = $str; |
|
48
|
|
|
return $this; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* 加密 |
|
53
|
|
|
* @param $data |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
|
|
public function encrypt($data) |
|
57
|
|
|
{ |
|
58
|
|
|
if (!empty(is_array($data))) { |
|
59
|
|
|
$data = json_encode($data, JSON_UNESCAPED_UNICODE); |
|
60
|
|
|
} |
|
61
|
|
|
return urlencode(base64_encode(openssl_encrypt($data, 'AES-128-CBC', $this->key, 1, $this->iv))); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* 解密 |
|
66
|
|
|
* @param $data |
|
67
|
|
|
* @return false|string |
|
68
|
|
|
*/ |
|
69
|
|
|
public function decrypt($data) |
|
70
|
|
|
{ |
|
71
|
|
|
return openssl_decrypt(base64_decode(urldecode($data)), "AES-128-CBC", $this->key, true, $this->iv); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|