|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the overtrue/wechat. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) overtrue <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Encryptor.php. |
|
14
|
|
|
* |
|
15
|
|
|
* Part of Overtrue\WeChat. |
|
16
|
|
|
* |
|
17
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
18
|
|
|
* file that was distributed with this source code. |
|
19
|
|
|
* |
|
20
|
|
|
* @author mingyoung <[email protected]> |
|
21
|
|
|
* @copyright 2016 |
|
22
|
|
|
* |
|
23
|
|
|
* @see https://github.com/overtrue |
|
24
|
|
|
* @see http://overtrue.me |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
namespace EasyWeChat\MiniProgram\Encryption; |
|
28
|
|
|
|
|
29
|
|
|
use EasyWeChat\Encryption\EncryptionException; |
|
30
|
|
|
use EasyWeChat\Encryption\Encryptor as BaseEncryptor; |
|
31
|
|
|
use Exception; |
|
32
|
|
|
|
|
33
|
|
|
class Encryptor extends BaseEncryptor |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* Decrypt data. |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $sessionKey |
|
39
|
|
|
* @param string $iv |
|
40
|
|
|
* @param string $encrypted |
|
41
|
|
|
* |
|
42
|
|
|
* @return array |
|
43
|
|
|
*/ |
|
44
|
1 |
|
public function decryptData($sessionKey, $iv, $encrypted) |
|
45
|
|
|
{ |
|
46
|
|
|
try { |
|
47
|
1 |
|
$decrypted = openssl_decrypt( |
|
48
|
1 |
|
base64_decode($encrypted, true), 'aes-128-cbc', base64_decode($sessionKey, true), |
|
49
|
1 |
|
OPENSSL_RAW_DATA | OPENSSL_NO_PADDING, base64_decode($iv, true) |
|
50
|
1 |
|
); |
|
51
|
1 |
|
} catch (Exception $e) { |
|
52
|
|
|
throw new EncryptionException($e->getMessage(), EncryptionException::ERROR_DECRYPT_AES); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
1 |
|
if (is_null($result = json_decode($this->decode($decrypted), true))) { |
|
56
|
|
|
throw new EncryptionException('ILLEGAL_BUFFER', EncryptionException::ILLEGAL_BUFFER); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
return $result; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|