1 | <?php |
||
7 | class Encrypter |
||
8 | { |
||
9 | /** |
||
10 | * $cipher. |
||
11 | * |
||
12 | * @var \phpseclib\Crypt\AES |
||
13 | */ |
||
14 | protected $cipher; |
||
15 | |||
16 | /** |
||
17 | * $key. |
||
18 | * |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $key; |
||
22 | |||
23 | /** |
||
24 | * __construct. |
||
25 | * |
||
26 | * @param string $key |
||
27 | * @param \phpseclib\Crypt\AES $cipher |
||
28 | */ |
||
29 | 3 | public function __construct($key, AES $cipher = null) |
|
34 | |||
35 | /** |
||
36 | * setKey. |
||
37 | * |
||
38 | * @param string $key |
||
39 | * @return static |
||
40 | */ |
||
41 | 2 | public function setKey($key) |
|
47 | |||
48 | /** |
||
49 | * encrypt. |
||
50 | * |
||
51 | * @param array $params |
||
52 | * @return string |
||
53 | */ |
||
54 | 2 | public function encrypt($params) |
|
63 | |||
64 | /** |
||
65 | * encryptByPHP. |
||
66 | * |
||
67 | * @param array $params |
||
68 | * @return string |
||
69 | */ |
||
70 | public function encryptByPHP($params) |
||
71 | { |
||
72 | $plaintext = json_encode($params); |
||
73 | $size = mcrypt_get_iv_size(MCRYPT_CAST_256, MCRYPT_MODE_CBC); |
||
74 | $iv = mcrypt_create_iv($size, MCRYPT_DEV_URANDOM); |
||
75 | $padding = 16 - (strlen($plaintext) % 16); |
||
76 | $plaintext .= str_repeat(chr($padding), $padding); |
||
77 | $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->key, $plaintext, MCRYPT_MODE_CBC, $iv); |
||
78 | |||
79 | return base64_encode($iv.$ciphertext); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * decrypt. |
||
84 | * |
||
85 | * @param string $ciphertext |
||
86 | * @return array |
||
87 | */ |
||
88 | 1 | public function decrypt($ciphertext) |
|
98 | |||
99 | /** |
||
100 | * decryptByPHP. |
||
101 | * |
||
102 | * @param string $ciphertext |
||
103 | * @return array |
||
104 | */ |
||
105 | public function decryptByPHP($ciphertext) |
||
118 | |||
119 | /** |
||
120 | * encryptRequest. |
||
121 | * |
||
122 | * @param string $storeUid |
||
123 | * @param array $params |
||
124 | * @param string $cmd |
||
125 | * @param string $serviceName |
||
126 | * @return array |
||
127 | */ |
||
128 | 1 | public function encryptRequest($storeUid, $params, $cmd = 'api/orders', $serviceName = 'api') |
|
139 | |||
140 | /** |
||
141 | * pkcs5Unpad. |
||
142 | * |
||
143 | * @param string $plaintext |
||
144 | * @return string |
||
145 | */ |
||
146 | protected function pkcs5Unpad($plaintext) |
||
159 | } |
||
160 |