Skip to content

Commit e04062d

Browse files
committed
[更新]微信加密解密改用OpenSSL,兼容php7.1版本
1 parent a759072 commit e04062d

File tree

5 files changed

+55
-76
lines changed

5 files changed

+55
-76
lines changed

Wechat/Lib/Prpcrypt.php

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,13 @@ class PKCS7Encoder {
1212

1313
/**
1414
* 对需要加密的明文进行填充补位
15-
* @param $text 需要进行填充补位操作的明文
16-
* @return 补齐明文字符串
15+
* @param string $text 需要进行填充补位操作的明文
16+
* @return string 补齐明文字符串
1717
*/
1818
function encode($text) {
19-
$block_size = PKCS7Encoder::$block_size;
20-
$text_length = strlen($text);
21-
$amount_to_pad = PKCS7Encoder::$block_size - ($text_length % PKCS7Encoder::$block_size);
19+
$amount_to_pad = PKCS7Encoder::$block_size - (strlen($text) % PKCS7Encoder::$block_size);
2220
if ($amount_to_pad == 0) {
23-
$amount_to_pad = PKCS7Encoder::block_size;
21+
$amount_to_pad = PKCS7Encoder::$block_size;
2422
}
2523
$pad_chr = chr($amount_to_pad);
2624
$tmp = "";
@@ -32,8 +30,8 @@ function encode($text) {
3230

3331
/**
3432
* 对解密后的明文进行补位删除
35-
* @param decrypted 解密后的明文
36-
* @return 删除填充补位后的明文
33+
* @param string $text 解密后的明文
34+
* @return string 删除填充补位后的明文
3735
*/
3836
function decode($text) {
3937
$pad = ord(substr($text, -1));
@@ -62,46 +60,39 @@ function __construct($k) {
6260
/**
6361
* 对明文进行加密
6462
* @param string $text 需要加密的明文
63+
* @param string $appid 公众号APPID
6564
* @return string 加密后的密文
6665
*/
6766
public function encrypt($text, $appid) {
6867
try {
69-
$random = $this->getRandomStr();
68+
//获得16位随机字符串,填充到明文之前
69+
$random = $this->getRandomStr();//"aaaabbbbccccdddd";
7070
$text = $random . pack("N", strlen($text)) . $text . $appid;
71-
$size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
72-
$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
7371
$iv = substr($this->key, 0, 16);
74-
$pkc_encoder = new PKCS7Encoder();
72+
$pkc_encoder = new PKCS7Encoder;
7573
$text = $pkc_encoder->encode($text);
76-
mcrypt_generic_init($module, $this->key, $iv);
77-
$encrypted = mcrypt_generic($module, $text);
78-
mcrypt_generic_deinit($module);
79-
mcrypt_module_close($module);
80-
return array(ErrorCode::$OK, base64_encode($encrypted));
74+
$encrypted = openssl_encrypt($text, 'AES-256-CBC', substr($this->key, 0, 32), OPENSSL_ZERO_PADDING, $iv);
75+
return array(ErrorCode::$OK, $encrypted);
8176
} catch (Exception $e) {
82-
return array(ErrorCode::$EncryptAESError, ErrorCode::getErrText(ErrorCode::$EncryptAESError));
77+
return array(ErrorCode::$EncryptAESError, null);
8378
}
8479
}
8580

8681
/**
8782
* 对密文进行解密
8883
* @param string $encrypted 需要解密的密文
84+
* @param string $appid 公众号APPID
8985
* @return string 解密得到的明文
9086
*/
9187
public function decrypt($encrypted, $appid) {
9288
try {
93-
$ciphertext_dec = base64_decode($encrypted);
94-
$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
9589
$iv = substr($this->key, 0, 16);
96-
mcrypt_generic_init($module, $this->key, $iv);
97-
$decrypted = mdecrypt_generic($module, $ciphertext_dec);
98-
mcrypt_generic_deinit($module);
99-
mcrypt_module_close($module);
90+
$decrypted = openssl_decrypt($encrypted, 'AES-256-CBC', substr($this->key, 0, 32), OPENSSL_ZERO_PADDING, $iv);
10091
} catch (Exception $e) {
101-
return array(ErrorCode::$DecryptAESError, ErrorCode::getErrText(ErrorCode::$DecryptAESError));
92+
return array(ErrorCode::$DecryptAESError, null);
10293
}
10394
try {
104-
$pkc_encoder = new PKCS7Encoder();
95+
$pkc_encoder = new PKCS7Encoder;
10596
$result = $pkc_encoder->decode($decrypted);
10697
if (strlen($result) < 16) {
10798
return "";
@@ -115,7 +106,7 @@ public function decrypt($encrypted, $appid) {
115106
$appid = $from_appid;
116107
}
117108
} catch (Exception $e) {
118-
return array(ErrorCode::$IllegalBuffer, ErrorCode::getErrText(ErrorCode::$IllegalBuffer));
109+
return array(ErrorCode::$IllegalBuffer, null);
119110
}
120111
return array(0, $xml_content, $from_appid);
121112
}
@@ -137,10 +128,9 @@ function getRandomStr() {
137128
}
138129

139130
/**
140-
* 仅用作类内部使用,不用于官方API接口的errCode码
141-
* @category WechatSDK
142-
* @subpackage library
143-
* @date 2016/06/28 11:59
131+
* 仅用作类内部使用
132+
* 不用于官方API接口的errCode码
133+
* Class ErrorCode
144134
*/
145135
class ErrorCode {
146136

@@ -173,15 +163,14 @@ class ErrorCode {
173163

174164
/**
175165
* 获取错误消息内容
176-
* @param type $err
166+
* @param string $err
177167
* @return bool
178168
*/
179169
public static function getErrText($err) {
180170
if (isset(self::$errCode[$err])) {
181171
return self::$errCode[$err];
182-
} else {
183-
return false;
184172
}
173+
return false;
185174
}
186175

187176
}

Wechat/Lib/Tools.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ static public function httpGet($url) {
136136
/**
137137
* 以post方式提交请求
138138
* @param string $url
139-
* @param array|string $postdata
139+
* @param array|string $data
140140
* @return bool|mixed
141141
*/
142142
static public function httpPost($url, $data) {

Wechat/Loader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ static public function & get($type, $config = array()) {
7676
if (!isset(self::$cache[$index])) {
7777
$basicName = 'Wechat' . ucfirst(strtolower($type));
7878
$className = "\\Wechat\\{$basicName}";
79-
/* 注册类的无命名空间别名,兼容未带命名空间的老版本SDK */
79+
// 注册类的无命名空间别名,兼容未带命名空间的老版本SDK
8080
!class_exists($basicName, FALSE) && class_alias($className, $basicName);
8181
self::$cache[$index] = new $className(self::config($config));
8282
}

Wechat/WechatCard.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class WechatCard extends Common {
4646
const CARD_PAYCELL_SET = '/card/paycell/set?';
4747
/*设置开卡字段接口*/
4848
const CARD_MEMBERCARD_ACTIVATEUSERFORM_SET = '/card/membercard/activateuserform/set?';
49+
4950
/**
5051
* 获取微信卡券 api_ticket
5152
* @param string $appid
@@ -722,19 +723,20 @@ public function setSelfconsumecell($card_id, $is_openid = false, $need_verify_co
722723
}
723724
return false;
724725
}
726+
725727
/**
726728
* 设置买单接口
727-
* @DateTime 2016-12-02T19:19:45+0800
728-
* @param [type] $card_id [description]
729-
* @param boolean $is_openid [description]
729+
* @param string $card_id
730+
* @param bool $is_openid
731+
* @return bool|mixed
730732
*/
731-
public function setPaycell($card_id,$is_openid = true){
733+
public function setPaycell($card_id, $is_openid = true) {
732734
if (!$this->access_token && !$this->getAccessToken()) {
733735
return false;
734736
}
735737
$data = array(
736-
'card_id' => $card_id,
737-
'is_open' => $is_openid,
738+
'card_id' => $card_id,
739+
'is_open' => $is_openid,
738740
);
739741
$result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CARD_PAYCELL_SET . "access_token={$this->access_token}", Tools::json_encode($data));
740742
if ($result) {
@@ -751,10 +753,10 @@ public function setPaycell($card_id,$is_openid = true){
751753

752754
/**
753755
* 设置开卡字段信息接口
754-
* @DateTime 2016-12-02T20:31:43+0800
755-
* @param [type] $data [description]
756+
* @param array $data
757+
* @return bool|array
756758
*/
757-
public function setMembercardActivateuserform($data){
759+
public function setMembercardActivateuserform($data) {
758760
if (!$this->access_token && !$this->getAccessToken()) {
759761
return false;
760762
}

Wechat/WechatReceive.php

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function getRev() {
5151
return $this;
5252
}
5353
$postStr = !empty($this->postxml) ? $this->postxml : file_get_contents("php://input");
54-
!empty($postStr) && $this->_receive = (array) simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
54+
!empty($postStr) && $this->_receive = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
5555
return $this;
5656
}
5757

@@ -70,9 +70,8 @@ public function getRevData() {
7070
public function getRevFrom() {
7171
if (isset($this->_receive['FromUserName'])) {
7272
return $this->_receive['FromUserName'];
73-
} else {
74-
return false;
7573
}
74+
return false;
7675
}
7776

7877
/**
@@ -82,9 +81,8 @@ public function getRevFrom() {
8281
public function getRevTo() {
8382
if (isset($this->_receive['ToUserName'])) {
8483
return $this->_receive['ToUserName'];
85-
} else {
86-
return false;
8784
}
85+
return false;
8886
}
8987

9088
/**
@@ -94,9 +92,8 @@ public function getRevTo() {
9492
public function getRevType() {
9593
if (isset($this->_receive['MsgType'])) {
9694
return $this->_receive['MsgType'];
97-
} else {
98-
return false;
9995
}
96+
return false;
10097
}
10198

10299
/**
@@ -106,9 +103,8 @@ public function getRevType() {
106103
public function getRevID() {
107104
if (isset($this->_receive['MsgId'])) {
108105
return $this->_receive['MsgId'];
109-
} else {
110-
return false;
111106
}
107+
return false;
112108
}
113109

114110
/**
@@ -118,9 +114,8 @@ public function getRevID() {
118114
public function getRevCtime() {
119115
if (isset($this->_receive['CreateTime'])) {
120116
return $this->_receive['CreateTime'];
121-
} else {
122-
return false;
123117
}
118+
return false;
124119
}
125120

126121
/**
@@ -131,9 +126,8 @@ public function getRevCtime() {
131126
public function getRevCardPass() {
132127
if (isset($this->_receive['CardId'])) {
133128
return $this->_receive['CardId'];
134-
} else {
135-
return false;
136129
}
130+
return false;
137131
}
138132

139133
/**
@@ -155,9 +149,8 @@ public function getRevCardGet() {
155149
}
156150
if (isset($array) && count($array) > 0) {
157151
return $array;
158-
} else {
159-
return false;
160152
}
153+
return false;
161154
}
162155

163156
/**
@@ -174,9 +167,8 @@ public function getRevCardDel() {
174167
}
175168
if (isset($array) && count($array) > 0) {
176169
return $array;
177-
} else {
178-
return false;
179170
}
171+
return false;
180172
}
181173

182174
/**
@@ -188,9 +180,8 @@ public function getRevContent() {
188180
return $this->_receive['Content'];
189181
} else if (isset($this->_receive['Recognition'])) { //获取语音识别文字内容,需申请开通
190182
return $this->_receive['Recognition'];
191-
} else {
192-
return false;
193183
}
184+
return false;
194185
}
195186

196187
/**
@@ -201,11 +192,10 @@ public function getRevPic() {
201192
if (isset($this->_receive['PicUrl'])) {
202193
return array(
203194
'mediaid' => $this->_receive['MediaId'],
204-
'picurl' => (string) $this->_receive['PicUrl'], //防止picurl为空导致解析出错
195+
'picurl' => (string)$this->_receive['PicUrl'], //防止picurl为空导致解析出错
205196
);
206-
} else {
207-
return false;
208197
}
198+
return false;
209199
}
210200

211201
/**
@@ -219,9 +209,8 @@ public function getRevLink() {
219209
'title' => $this->_receive['Title'],
220210
'description' => $this->_receive['Description']
221211
);
222-
} else {
223-
return false;
224212
}
213+
return false;
225214
}
226215

227216
/**
@@ -236,9 +225,8 @@ public function getRevGeo() {
236225
'scale' => $this->_receive['Scale'],
237226
'label' => $this->_receive['Label']
238227
);
239-
} else {
240-
return false;
241228
}
229+
return false;
242230
}
243231

244232
/**
@@ -284,7 +272,7 @@ public function getRevEvent() {
284272
public function getRevScanInfo() {
285273
if (isset($this->_receive['ScanCodeInfo'])) {
286274
if (!is_array($this->_receive['ScanCodeInfo'])) {
287-
$array = (array) $this->_receive['ScanCodeInfo'];
275+
$array = (array)$this->_receive['ScanCodeInfo'];
288276
$this->_receive['ScanCodeInfo'] = $array;
289277
} else {
290278
$array = $this->_receive['ScanCodeInfo'];
@@ -319,13 +307,13 @@ public function getRevScanInfo() {
319307
public function getRevSendPicsInfo() {
320308
if (isset($this->_receive['SendPicsInfo'])) {
321309
if (!is_array($this->_receive['SendPicsInfo'])) {
322-
$array = (array) $this->_receive['SendPicsInfo'];
310+
$array = (array)$this->_receive['SendPicsInfo'];
323311
if (isset($array['PicList'])) {
324-
$array['PicList'] = (array) $array['PicList'];
312+
$array['PicList'] = (array)$array['PicList'];
325313
$item = $array['PicList']['item'];
326314
$array['PicList']['item'] = array();
327315
foreach ($item as $key => $value) {
328-
$array['PicList']['item'][$key] = (array) $value;
316+
$array['PicList']['item'][$key] = (array)$value;
329317
}
330318
}
331319
$this->_receive['SendPicsInfo'] = $array;
@@ -358,7 +346,7 @@ public function getRevSendPicsInfo() {
358346
public function getRevSendGeoInfo() {
359347
if (isset($this->_receive['SendLocationInfo'])) {
360348
if (!is_array($this->_receive['SendLocationInfo'])) {
361-
$array = (array) $this->_receive['SendLocationInfo'];
349+
$array = (array)$this->_receive['SendLocationInfo'];
362350
if (empty($array['Poiname'])) {
363351
$array['Poiname'] = "";
364352
}

0 commit comments

Comments
 (0)