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\Core\Exceptions\InvalidConfigException; |
30
|
|
|
use EasyWeChat\Encryption\EncryptionException; |
31
|
|
|
use EasyWeChat\Encryption\Encryptor as BaseEncryptor; |
32
|
|
|
use EasyWeChat\Support\Collection; |
33
|
|
|
use Exception as BaseException; |
34
|
|
|
|
35
|
|
|
class Encryptor extends BaseEncryptor |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* A non-NULL Initialization Vector. |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $iv; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Encryptor constructor. |
46
|
|
|
* |
47
|
|
|
* @param string $sessionKey |
48
|
|
|
* @param string $iv |
49
|
|
|
*/ |
50
|
|
|
public function __construct($sessionKey, $iv) |
51
|
|
|
{ |
52
|
|
|
$this->iv = base64_decode($iv, true); |
53
|
|
|
|
54
|
|
|
parent::__construct(null, null, $sessionKey); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Decrypt data. |
59
|
|
|
* |
60
|
|
|
* @param $encrypted |
61
|
|
|
* |
62
|
|
|
* @return \EasyWeChat\Support\Collection |
63
|
|
|
*/ |
64
|
|
|
public function decryptData($encrypted) |
65
|
|
|
{ |
66
|
|
|
return new Collection( |
67
|
|
|
$this->decrypt($encrypted) |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Decrypt data. |
73
|
|
|
* |
74
|
|
|
* @param string $encrypted |
75
|
|
|
* |
76
|
|
|
* @return array |
77
|
|
|
* |
78
|
|
|
* @throws EncryptionException |
79
|
|
|
*/ |
80
|
|
|
private function decrypt($encrypted) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
try { |
83
|
|
|
$key = $this->getAESKey(); |
84
|
|
|
$ciphertext = base64_decode($encrypted, true); |
85
|
|
|
$decrypted = openssl_decrypt($ciphertext, 'aes-128-cbc', $key, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING, $this->iv); |
86
|
|
|
} catch (BaseException $e) { |
87
|
|
|
throw new EncryptionException($e->getMessage(), EncryptionException::ERROR_DECRYPT_AES); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$result = json_decode($this->decode($decrypted), true); |
91
|
|
|
|
92
|
|
|
if (is_null($result)) { |
93
|
|
|
throw new EncryptionException('ILLEGAL_BUFFER', EncryptionException::ILLEGAL_BUFFER); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $result; |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Return AESKey. |
101
|
|
|
* |
102
|
|
|
* @return string |
103
|
|
|
* |
104
|
|
|
* @throws InvalidConfigException |
105
|
|
|
*/ |
106
|
|
|
protected function getAESKey() |
107
|
|
|
{ |
108
|
|
|
if (empty($this->AESKey)) { |
109
|
|
|
throw new InvalidConfigException("Configuration mission, 'aes_key' is required."); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return base64_decode($this->AESKey, true); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.