|
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 EasyWeChat\Support\Collection; |
|
32
|
|
|
use Exception as BaseException; |
|
33
|
|
|
|
|
34
|
|
|
class Encryptor extends BaseEncryptor |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* {@inheritdoc}. |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $aesKeyLength = 24; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* A non-NULL Initialization Vector. |
|
43
|
|
|
* |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $iv; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Encryptor constructor. |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $sessionKey |
|
52
|
|
|
* @param string $iv |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct($sessionKey, $iv) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->iv = base64_decode($iv, true); |
|
57
|
|
|
|
|
58
|
|
|
parent::__construct(null, null, $sessionKey); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Decrypt data. |
|
63
|
|
|
* |
|
64
|
|
|
* @param $encrypted |
|
65
|
|
|
* |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
|
|
public function decryptData($encrypted) |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->decrypt($encrypted); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Decrypt data. |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $encrypted |
|
77
|
|
|
* |
|
78
|
|
|
* @return Collection |
|
79
|
|
|
* |
|
80
|
|
|
* @throws EncryptionException |
|
81
|
|
|
*/ |
|
82
|
|
|
private function decrypt($encrypted) |
|
|
|
|
|
|
83
|
|
|
{ |
|
84
|
|
|
try { |
|
85
|
|
|
$key = $this->getAESKey(); |
|
86
|
|
|
$ciphertext = base64_decode($encrypted, true); |
|
87
|
|
|
|
|
88
|
|
|
$decrypted = openssl_decrypt($ciphertext, 'aes-128-cbc', $key, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING, $this->iv); |
|
89
|
|
|
|
|
90
|
|
|
$result = $this->decode($decrypted); |
|
91
|
|
|
} catch (BaseException $e) { |
|
92
|
|
|
throw new EncryptionException($e->getMessage(), EncryptionException::ERROR_DECRYPT_AES); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$result = json_decode($result, true); |
|
96
|
|
|
|
|
97
|
|
|
return new Collection($result); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
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.