正在显示
1 个修改的文件
包含
11 行增加
和
171 行删除
1 | <?php | 1 | <?php |
2 | - | ||
3 | -namespace lib; | ||
4 | - | 2 | +namespace wx; |
3 | +use wx\ErrorCode; | ||
5 | class WXBizDataCrypt | 4 | class WXBizDataCrypt |
6 | { | 5 | { |
7 | - | ||
8 | private $appid; | 6 | private $appid; |
9 | private $sessionKey; | 7 | private $sessionKey; |
10 | 8 | ||
@@ -13,9 +11,8 @@ class WXBizDataCrypt | @@ -13,9 +11,8 @@ class WXBizDataCrypt | ||
13 | * @param $sessionKey string 用户在小程序登录后获取的会话密钥 | 11 | * @param $sessionKey string 用户在小程序登录后获取的会话密钥 |
14 | * @param $appid string 小程序的appid | 12 | * @param $appid string 小程序的appid |
15 | */ | 13 | */ |
16 | - public function __construct($appid, $sessionKey) | 14 | + public function __construct( $appid, $sessionKey) |
17 | { | 15 | { |
18 | - | ||
19 | $this->sessionKey = $sessionKey; | 16 | $this->sessionKey = $sessionKey; |
20 | $this->appid = $appid; | 17 | $this->appid = $appid; |
21 | } | 18 | } |
@@ -30,190 +27,33 @@ class WXBizDataCrypt | @@ -30,190 +27,33 @@ class WXBizDataCrypt | ||
30 | * @return int 成功0,失败返回对应的错误码 | 27 | * @return int 成功0,失败返回对应的错误码 |
31 | */ | 28 | */ |
32 | public function decryptData( $encryptedData, $iv, &$data ) | 29 | public function decryptData( $encryptedData, $iv, &$data ) |
33 | - | ||
34 | { | 30 | { |
35 | - | ||
36 | if (strlen($this->sessionKey) != 24) { | 31 | if (strlen($this->sessionKey) != 24) { |
37 | - | ||
38 | return ErrorCode::$IllegalAesKey; | 32 | return ErrorCode::$IllegalAesKey; |
39 | - | ||
40 | } | 33 | } |
41 | - | ||
42 | $aesKey=base64_decode($this->sessionKey); | 34 | $aesKey=base64_decode($this->sessionKey); |
43 | 35 | ||
44 | - if (strlen($iv) != 24) { | ||
45 | 36 | ||
37 | + if (strlen($iv) != 24) { | ||
46 | return ErrorCode::$IllegalIv; | 38 | return ErrorCode::$IllegalIv; |
47 | - | ||
48 | } | 39 | } |
49 | - | ||
50 | $aesIV=base64_decode($iv); | 40 | $aesIV=base64_decode($iv); |
51 | 41 | ||
52 | -// $aesCipher=base64_decode($encryptedData); | ||
53 | - | ||
54 | - $aesCipher=$encryptedData; | ||
55 | - | ||
56 | - $pc = new Prpcrypt($aesKey); | ||
57 | - | ||
58 | - $result = $pc->decrypt($aesCipher,$aesIV); | ||
59 | - | ||
60 | - var_dump($result); | ||
61 | - | ||
62 | - if ($result[0] != 0) { | ||
63 | - | ||
64 | - return $result[0]; | ||
65 | - | ||
66 | - } | ||
67 | - | ||
68 | - $dataObj=json_decode( $result[1] ); | 42 | + $aesCipher=base64_decode($encryptedData); |
69 | 43 | ||
70 | - if( $dataObj == NULL ) | 44 | + $result=openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV); |
71 | 45 | ||
46 | + $dataObj=json_decode( $result ); | ||
47 | + if( $dataObj == NULL ) | ||
72 | { | 48 | { |
73 | - | ||
74 | - return ErrorCode::$IllegalBuffer.'--'; | ||
75 | - | 49 | + return ErrorCode::$IllegalBuffer; |
76 | } | 50 | } |
77 | - | ||
78 | if( $dataObj->watermark->appid != $this->appid ) | 51 | if( $dataObj->watermark->appid != $this->appid ) |
79 | - | ||
80 | { | 52 | { |
81 | - | ||
82 | - return ErrorCode::$IllegalBuffer.';;'; | ||
83 | - | 53 | + return ErrorCode::$IllegalBuffer; |
84 | } | 54 | } |
85 | - | ||
86 | - $data = $result[1]; | ||
87 | - | 55 | + $data = $result; |
88 | return ErrorCode::$OK; | 56 | return ErrorCode::$OK; |
89 | - | ||
90 | } | 57 | } |
91 | 58 | ||
92 | -} | ||
93 | - | ||
94 | -/** | ||
95 | - * PKCS7Encoder class | ||
96 | - * | ||
97 | - * 提供基于PKCS7算法的加解密接口. | ||
98 | - */ | ||
99 | -class PKCS7Encoder | ||
100 | -{ | ||
101 | - public static $block_size = 16; | ||
102 | - | ||
103 | - /** | ||
104 | - * 对需要加密的明文进行填充补位 | ||
105 | - * @param $text 需要进行填充补位操作的明文 | ||
106 | - * @return 补齐明文字符串 | ||
107 | - */ | ||
108 | - function encode($text) | ||
109 | - { | ||
110 | - $block_size = PKCS7Encoder::$block_size; | ||
111 | - $text_length = strlen($text); | ||
112 | - //计算需要填充的位数 | ||
113 | - $amount_to_pad = PKCS7Encoder::$block_size - ($text_length % PKCS7Encoder::$block_size); | ||
114 | - if ($amount_to_pad == 0) { | ||
115 | - $amount_to_pad = PKCS7Encoder::block_size; | ||
116 | - } | ||
117 | - //获得补位所用的字符 | ||
118 | - $pad_chr = chr($amount_to_pad); | ||
119 | - $tmp = ""; | ||
120 | - for ($index = 0; $index < $amount_to_pad; $index++) { | ||
121 | - $tmp .= $pad_chr; | ||
122 | - } | ||
123 | - return $text . $tmp; | ||
124 | - } | ||
125 | - | ||
126 | - /** | ||
127 | - * 对解密后的明文进行补位删除 | ||
128 | - * @param decrypted 解密后的明文 | ||
129 | - * @return 删除填充补位后的明文 | ||
130 | - */ | ||
131 | - function decode($text) | ||
132 | - { | ||
133 | - | ||
134 | - $pad = ord(substr($text, -1)); | ||
135 | - if ($pad < 1 || $pad > 32) { | ||
136 | - $pad = 0; | ||
137 | - } | ||
138 | - return substr($text, 0, (strlen($text) - $pad)); | ||
139 | - } | ||
140 | - | ||
141 | -} | ||
142 | - | ||
143 | -/** | ||
144 | - * Prpcrypt class | ||
145 | - * | ||
146 | - * | ||
147 | - */ | ||
148 | -class Prpcrypt | ||
149 | -{ | ||
150 | - public $key; | ||
151 | - | ||
152 | - function __construct($k) | ||
153 | - { | ||
154 | - $this->key = $k; | ||
155 | - } | ||
156 | - | ||
157 | - /** | ||
158 | - * 对密文进行解密 | ||
159 | - * @param string $aesCipher 需要解密的密文 | ||
160 | - * @param string $aesIV 解密的初始向量 | ||
161 | - * @return string 解密得到的明文 | ||
162 | - */ | ||
163 | - public function decrypt( $aesCipher, $aesIV ) | ||
164 | - | ||
165 | - { | ||
166 | - | ||
167 | - try { | ||
168 | - | ||
169 | -// $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); | ||
170 | - | ||
171 | -// mcrypt_generic_init($module, $this->key, $aesIV); | ||
172 | - | ||
173 | -// //解密 | ||
174 | - | ||
175 | -// $decrypted = mdecrypt_generic($module, $aesCipher); | ||
176 | - | ||
177 | -// mcrypt_generic_deinit($module); | ||
178 | - | ||
179 | -// mcrypt_module_close($module); | ||
180 | - | ||
181 | - $decrypted = openssl_decrypt($aesCipher,'AES-128-CBC',$this->key,OPENSSL_ZERO_PADDING,$aesIV); | ||
182 | - | ||
183 | - var_dump($decrypted); | ||
184 | - | ||
185 | - } catch (Exception $e) { | ||
186 | - | ||
187 | - return array(ErrorCode::$IllegalBuffer, null); | ||
188 | - | ||
189 | - } | ||
190 | - | ||
191 | - try { | ||
192 | - | ||
193 | -//去除补位字符 | ||
194 | - | ||
195 | - $pkc_encoder = new PKCS7Encoder; | ||
196 | - | ||
197 | - $result = $pkc_encoder->decode($decrypted); | ||
198 | - | ||
199 | - } catch (Exception $e) { | ||
200 | - | ||
201 | -//print $e; | ||
202 | - | ||
203 | - return array(ErrorCode::$IllegalBuffer, null); | ||
204 | - | ||
205 | - } | ||
206 | - | ||
207 | - return array(0, $result); | ||
208 | - | ||
209 | - } | ||
210 | -} | ||
211 | - | ||
212 | -class ErrorCode | ||
213 | -{ | ||
214 | - public static $OK = 0; | ||
215 | - public static $IllegalAesKey = -41001; | ||
216 | - public static $IllegalIv = -41002; | ||
217 | - public static $IllegalBuffer = -41003; | ||
218 | - public static $DecodeBase64Error = -41004; | ||
219 | } | 59 | } |
-
请 注册 或 登录 后发表评论