1 | <?php |
||
31 | class Encryptor |
||
32 | { |
||
33 | /** |
||
34 | * App id. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $appId; |
||
39 | |||
40 | /** |
||
41 | * App token. |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $token; |
||
46 | |||
47 | /** |
||
48 | * AES key. |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $AESKey; |
||
53 | |||
54 | /** |
||
55 | * Block size. |
||
56 | * |
||
57 | * @var int |
||
58 | */ |
||
59 | protected $blockSize; |
||
60 | |||
61 | /** |
||
62 | * Constructor. |
||
63 | * |
||
64 | * @param string $appId |
||
65 | * @param string $token |
||
66 | * @param string $AESKey |
||
67 | * |
||
68 | * @throws RuntimeException |
||
69 | */ |
||
70 | public function __construct($appId, $token, $AESKey) |
||
71 | { |
||
72 | if (!extension_loaded('mcrypt')) { |
||
73 | throw new RuntimeException("The ext 'mcrypt' is required."); |
||
74 | } |
||
75 | |||
76 | $this->appId = $appId; |
||
77 | $this->token = $token; |
||
78 | $this->AESKey = $AESKey; |
||
79 | $this->blockSize = 32; // mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Encrypt the message and return XML. |
||
84 | * |
||
85 | * @param string $xml |
||
86 | * @param string $nonce |
||
87 | * @param int $timestamp |
||
88 | * |
||
89 | * @return string |
||
90 | */ |
||
91 | public function encryptMsg($xml, $nonce = null, $timestamp = null) |
||
92 | { |
||
93 | $encrypt = $this->encrypt($xml, $this->appId); |
||
94 | |||
95 | !is_null($nonce) || $nonce = substr($this->appId, 0, 10); |
||
96 | !is_null($timestamp) || $timestamp = time(); |
||
97 | |||
98 | //生成安全签名 |
||
99 | $signature = $this->getSHA1($this->token, $timestamp, $nonce, $encrypt); |
||
100 | |||
101 | $response = [ |
||
102 | 'Encrypt' => $encrypt, |
||
103 | 'MsgSignature' => $signature, |
||
104 | 'TimeStamp' => $timestamp, |
||
105 | 'Nonce' => $nonce, |
||
106 | ]; |
||
107 | |||
108 | //生成响应xml |
||
109 | return XML::build($response); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Decrypt message. |
||
114 | * |
||
115 | * @param string $msgSignature |
||
116 | * @param string $nonce |
||
117 | * @param string $timestamp |
||
118 | * @param string $postXML |
||
119 | * |
||
120 | * @return array |
||
121 | * |
||
122 | * @throws EncryptionException |
||
123 | */ |
||
124 | public function decryptMsg($msgSignature, $nonce, $timestamp, $postXML) |
||
125 | { |
||
126 | try { |
||
127 | $array = XML::parse($postXML); |
||
128 | } catch (BaseException $e) { |
||
129 | throw new EncryptionException('Invalid xml.', EncryptionException::ERROR_PARSE_XML); |
||
130 | } |
||
131 | |||
132 | $encrypted = $array['Encrypt']; |
||
133 | |||
134 | $signature = $this->getSHA1($this->token, $timestamp, $nonce, $encrypted); |
||
135 | |||
136 | if ($signature !== $msgSignature) { |
||
137 | throw new EncryptionException('Invalid Signature.', EncryptionException::ERROR_INVALID_SIGNATURE); |
||
138 | } |
||
139 | |||
140 | return XML::parse($this->decrypt($encrypted, $this->appId)); |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Get SHA1. |
||
145 | * |
||
146 | * @return string |
||
147 | * |
||
148 | * @throws EncryptionException |
||
149 | */ |
||
150 | public function getSHA1() |
||
151 | { |
||
152 | try { |
||
153 | $array = func_get_args(); |
||
154 | sort($array, SORT_STRING); |
||
155 | |||
156 | return sha1(implode($array)); |
||
157 | } catch (BaseException $e) { |
||
158 | throw new EncryptionException($e->getMessage(), EncryptionException::ERROR_CALC_SIGNATURE); |
||
159 | } |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Encode string. |
||
164 | * |
||
165 | * @param string $text |
||
166 | * |
||
167 | * @return string |
||
168 | */ |
||
169 | public function encode($text) |
||
170 | { |
||
171 | $padAmount = $this->blockSize - (strlen($text) % $this->blockSize); |
||
172 | |||
173 | $padAmount = $padAmount !== 0 ? $padAmount : $this->blockSize; |
||
174 | |||
175 | $padChr = chr($padAmount); |
||
176 | |||
177 | $tmp = ''; |
||
178 | |||
179 | for ($index = 0; $index < $padAmount; ++$index) { |
||
180 | $tmp .= $padChr; |
||
181 | } |
||
182 | |||
183 | return $text.$tmp; |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Decode string. |
||
188 | * |
||
189 | * @param string $decrypted |
||
190 | * |
||
191 | * @return string |
||
192 | */ |
||
193 | public function decode($decrypted) |
||
194 | { |
||
195 | $pad = ord(substr($decrypted, -1)); |
||
196 | |||
197 | if ($pad < 1 || $pad > $this->blockSize) { |
||
198 | $pad = 0; |
||
199 | } |
||
200 | |||
201 | return substr($decrypted, 0, (strlen($decrypted) - $pad)); |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Return AESKey. |
||
206 | * |
||
207 | * @return string |
||
208 | * |
||
209 | * @throws InvalidConfigException |
||
210 | */ |
||
211 | protected function getAESKey() |
||
212 | { |
||
213 | if (empty($this->AESKey) || strlen($this->AESKey) !== 43) { |
||
214 | throw new InvalidConfigException("Configuration mission, 'aes_key' is required."); |
||
215 | } |
||
216 | |||
217 | return base64_decode($this->AESKey.'=', true); |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * Encrypt string. |
||
222 | * |
||
223 | * @param string $text |
||
224 | * @param string $appId |
||
225 | * |
||
226 | * @return string |
||
227 | * |
||
228 | * @throws EncryptionException |
||
229 | */ |
||
230 | private function encrypt($text, $appId) |
||
231 | { |
||
232 | try { |
||
233 | $key = $this->getAESKey(); |
||
234 | $random = $this->getRandomStr(); |
||
235 | $text = $random.pack('N', strlen($text)).$text.$appId; |
||
236 | |||
237 | // $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); |
||
238 | $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); |
||
239 | $iv = substr($key, 0, 16); |
||
240 | |||
241 | $text = $this->encode($text); |
||
242 | |||
243 | mcrypt_generic_init($module, $key, $iv); |
||
244 | |||
245 | $encrypted = mcrypt_generic($module, $text); |
||
246 | mcrypt_generic_deinit($module); |
||
247 | mcrypt_module_close($module); |
||
248 | |||
249 | return base64_encode($encrypted); |
||
250 | } catch (BaseException $e) { |
||
251 | throw new EncryptionException($e->getMessage(), EncryptionException::ERROR_ENCRYPT_AES); |
||
252 | } |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Decrypt message. |
||
257 | * |
||
258 | * @param string $encrypted |
||
259 | * @param string $appId |
||
260 | * |
||
261 | * @return string |
||
262 | * |
||
263 | * @throws EncryptionException |
||
264 | */ |
||
265 | private function decrypt($encrypted, $appId) |
||
266 | { |
||
267 | try { |
||
268 | $key = $this->getAESKey(); |
||
269 | $ciphertext = base64_decode($encrypted, true); |
||
270 | $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); |
||
271 | $iv = substr($key, 0, 16); |
||
272 | |||
273 | mcrypt_generic_init($module, $key, $iv); |
||
274 | |||
275 | $decrypted = mdecrypt_generic($module, $ciphertext); |
||
276 | mcrypt_generic_deinit($module); |
||
277 | mcrypt_module_close($module); |
||
278 | } catch (BaseException $e) { |
||
279 | throw new EncryptionException($e->getMessage(), EncryptionException::ERROR_DECRYPT_AES); |
||
280 | } |
||
281 | |||
282 | try { |
||
283 | $result = $this->decode($decrypted); |
||
284 | |||
285 | if (strlen($result) < 16) { |
||
286 | return ''; |
||
287 | } |
||
288 | |||
289 | $content = substr($result, 16, strlen($result)); |
||
290 | $listLen = unpack('N', substr($content, 0, 4)); |
||
291 | $xmlLen = $listLen[1]; |
||
292 | $xml = substr($content, 4, $xmlLen); |
||
293 | $fromAppId = trim(substr($content, $xmlLen + 4)); |
||
294 | } catch (BaseException $e) { |
||
295 | throw new EncryptionException($e->getMessage(), EncryptionException::ERROR_INVALID_XML); |
||
296 | } |
||
297 | |||
298 | if ($fromAppId !== $appId) { |
||
299 | throw new EncryptionException('Invalid appId.', EncryptionException::ERROR_INVALID_APPID); |
||
300 | } |
||
301 | |||
302 | return $xml; |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * Generate random string. |
||
307 | * |
||
308 | * @return string |
||
309 | */ |
||
310 | private function getRandomStr() |
||
314 | } |
||
315 |