1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wechat\Utils\Code; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Wechat\Utils\XML; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Prpcrypt class |
10
|
|
|
* |
11
|
|
|
* 提供接收和推送给公众平台消息的加解密接口. |
12
|
|
|
*/ |
13
|
|
|
class Prpcrypt |
14
|
|
|
{ |
15
|
|
|
protected $AESKey; |
16
|
|
|
protected $blockSize; |
17
|
|
|
|
18
|
|
|
public function __construct($k) |
19
|
|
|
{ |
20
|
|
|
$this->AESKey = $k; |
21
|
|
|
|
22
|
|
|
$this->blockSize = 32; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* 对明文进行加密 |
27
|
|
|
* |
28
|
|
|
* @param string $text 需要加密的明文 |
29
|
|
|
* |
30
|
|
|
* @param string $appId |
31
|
|
|
* |
32
|
|
|
* @return array |
33
|
|
|
*/ |
34
|
|
|
public function encrypt($text, $appId) |
35
|
|
|
{ |
36
|
|
|
try { |
37
|
|
|
//获得16位随机字符串,填充到明文之前 |
38
|
|
|
$key = $this->getAESKey(); |
39
|
|
|
$random = $this->getRandomStr(); |
40
|
|
|
$text = $this->encode($random . pack('N', strlen($text)) . $text . $appId); |
41
|
|
|
|
42
|
|
|
$iv = substr($key, 0, 16); |
43
|
|
|
|
44
|
|
|
$encrypted = openssl_encrypt($text, 'aes-256-cbc', $key, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING, $iv); |
45
|
|
|
|
46
|
|
|
//print(base64_encode($encrypted)); |
|
|
|
|
47
|
|
|
//使用BASE64对加密后的字符串进行编码 |
48
|
|
|
return [ErrorCode::$OK, base64_encode($encrypted)]; |
49
|
|
|
} catch (Exception $e) { |
50
|
|
|
//print $e; |
51
|
|
|
return [ErrorCode::$EncryptAESError, null]; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $encrypted 需要解密的密文 |
57
|
|
|
* @param string $appId APPID |
58
|
|
|
* |
59
|
|
|
* @return array|string |
60
|
|
|
*/ |
61
|
|
|
public function decrypt($encrypted, $appId) |
62
|
|
|
{ |
63
|
|
|
try { |
64
|
|
|
//使用BASE64对需要解密的字符串进行解码 |
65
|
|
|
$key = $this->getAESKey(); |
66
|
|
|
$ciphertext = base64_decode($encrypted, true); |
67
|
|
|
$iv = substr($key, 0, 16); |
68
|
|
|
|
69
|
|
|
$decrypted = openssl_decrypt($ciphertext, 'aes-256-cbc', $key, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING, $iv); |
70
|
|
|
} catch (Exception $e) { |
71
|
|
|
return [ErrorCode::$DecryptAESError, null]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
try { |
75
|
|
|
$result = $this->decode($decrypted); |
76
|
|
|
|
77
|
|
|
if (strlen($result) < 16) { |
78
|
|
|
return ''; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$content = substr($result, 16, strlen($result)); |
82
|
|
|
$listLen = unpack('N', substr($content, 0, 4)); |
83
|
|
|
$xmlLen = $listLen[1]; |
84
|
|
|
$xml = substr($content, 4, $xmlLen); |
85
|
|
|
$fromAppId = trim(substr($content, $xmlLen + 4)); |
86
|
|
|
} catch (Exception $e) { |
87
|
|
|
//print $e; |
88
|
|
|
return [ErrorCode::$IllegalBuffer, null]; |
89
|
|
|
} |
90
|
|
|
if ($fromAppId !== $appId) { |
91
|
|
|
return [ErrorCode::$ValidateAppidError, null]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$dataSet = json_decode($xml, true); |
95
|
|
|
if ($dataSet && (JSON_ERROR_NONE === json_last_error())) { |
96
|
|
|
// For mini-program JSON formats. |
97
|
|
|
// Convert to XML if the given string can be decode into a data array. |
98
|
|
|
$xml = XML::build($dataSet); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return [ErrorCode::$OK, $xml]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* 随机生成16位字符串 |
106
|
|
|
* |
107
|
|
|
* @return string 生成的字符串 |
108
|
|
|
*/ |
109
|
|
View Code Duplication |
public function getRandomStr() |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
$str = ""; |
112
|
|
|
$str_pol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz"; |
113
|
|
|
$max = strlen($str_pol) - 1; |
114
|
|
|
for ($i = 0; $i < 16; $i++) { |
115
|
|
|
$str .= $str_pol[mt_rand(0, $max)]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $str; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Return AESKey. |
123
|
|
|
* |
124
|
|
|
* @return string |
125
|
|
|
* |
126
|
|
|
* @throws Exception |
127
|
|
|
*/ |
128
|
|
|
protected function getAESKey() |
129
|
|
|
{ |
130
|
|
|
if (empty($this->AESKey)) { |
131
|
|
|
throw new Exception("Configuration mission, 'aes_key' is required."); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if (strlen($this->AESKey) !== 43) { |
135
|
|
|
throw new Exception("The length of 'aes_key' must be 43."); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return base64_decode($this->AESKey . '=', true); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Decode string. |
143
|
|
|
* |
144
|
|
|
* @param string $decrypted |
145
|
|
|
* |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
|
View Code Duplication |
public function decode($decrypted) |
|
|
|
|
149
|
|
|
{ |
150
|
|
|
$pad = ord(substr($decrypted, -1)); |
151
|
|
|
|
152
|
|
|
if ($pad < 1 || $pad > $this->blockSize) { |
153
|
|
|
$pad = 0; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return substr($decrypted, 0, (strlen($decrypted) - $pad)); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Encode string. |
161
|
|
|
* |
162
|
|
|
* @param string $text |
163
|
|
|
* |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
|
|
public function encode($text) |
167
|
|
|
{ |
168
|
|
|
$padAmount = $this->blockSize - (strlen($text) % $this->blockSize); |
169
|
|
|
|
170
|
|
|
$padAmount = $padAmount !== 0 ? $padAmount : $this->blockSize; |
171
|
|
|
|
172
|
|
|
$padChr = chr($padAmount); |
173
|
|
|
|
174
|
|
|
$tmp = ''; |
175
|
|
|
|
176
|
|
|
for ($index = 0; $index < $padAmount; ++$index) { |
177
|
|
|
$tmp .= $padChr; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $text . $tmp; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
} |
184
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.