Skip to content

Commit

Permalink
Merge pull request #141 from binsee/master
Browse files Browse the repository at this point in the history
公众号类库修改解密时订阅号appid问题的解决代码/代码同步修改/注释异常输出代码
  • Loading branch information
dodgepudding committed Oct 17, 2014
2 parents e683dd0 + 2c95fa6 commit 05466f5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 20 deletions.
18 changes: 12 additions & 6 deletions Thinkphp/Wechat.class.php
Expand Up @@ -176,22 +176,24 @@ public function valid($return=false)
}
}
$this->postxml = $array[1];
if (!$this->appid)
$this->appid = $array[2];//为了没有appid的订阅号。
} else {
$this->postxml = $postStr;
}
} else {
$echoStr = isset($_GET["echostr"]) ? $_GET["echostr"]: '';
}
if ($return) {
if ($echoStr) {
if (isset($echoStr)) {
if ($this->checkSignature($encryptStr))
return $echoStr;
else
return false;
} else
return $this->checkSignature($encryptStr);
} else {
if ($echoStr) {
if (isset($echoStr)) {
if ($this->checkSignature($encryptStr))
die($echoStr);
else
Expand Down Expand Up @@ -2076,7 +2078,7 @@ public function encrypt($text, $appid)

try {
//获得16位随机字符串,填充到明文之前
$random = "aaaabbbbccccdddd"; //$this->getRandomStr();
$random = $this->getRandomStr();//"aaaabbbbccccdddd";
$text = $random . pack("N", strlen($text)) . $text . $appid;
// 网络字节序
$size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
Expand All @@ -2095,7 +2097,7 @@ public function encrypt($text, $appid)
//使用BASE64对加密后的字符串进行编码
return array(ErrorCode::$OK, base64_encode($encrypted));
} catch (Exception $e) {
print $e;
//print $e;
return array(ErrorCode::$EncryptAESError, null);
}
}
Expand Down Expand Up @@ -2135,13 +2137,17 @@ public function decrypt($encrypted, $appid)
$xml_len = $len_list[1];
$xml_content = substr($content, 4, $xml_len);
$from_appid = substr($content, $xml_len + 4);
if (!$appid)
$appid = $from_appid;
//如果传入的appid是空的,则认为是订阅号,使用数据中提取出来的appid
} catch (Exception $e) {
print $e;
//print $e;
return array(ErrorCode::$IllegalBuffer, null);
}
if ($from_appid != $appid)
return array(ErrorCode::$ValidateAppidError, null);
return array(0, $xml_content);
//不注释上边两行,避免传入appid是错误的情况
return array(0, $xml_content, $from_appid); //增加appid,为了解决后面加密回复消息的时候没有appid的订阅号会无法回复

}

Expand Down
6 changes: 3 additions & 3 deletions Thinkphp/qywechat.class.php
Expand Up @@ -1793,7 +1793,7 @@ public function encrypt($text, $appid)

try {
//获得16位随机字符串,填充到明文之前
$random = "aaaabbbbccccdddd"; //$this->getRandomStr();
$random = $this->getRandomStr();//"aaaabbbbccccdddd";
$text = $random . pack("N", strlen($text)) . $text . $appid;
// 网络字节序
$size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
Expand All @@ -1812,7 +1812,7 @@ public function encrypt($text, $appid)
//使用BASE64对加密后的字符串进行编码
return array(ErrorCode::$OK, base64_encode($encrypted));
} catch (Exception $e) {
print $e;
//print $e;
return array(ErrorCode::$EncryptAESError, null);
}
}
Expand Down Expand Up @@ -1853,7 +1853,7 @@ public function decrypt($encrypted, $appid)
$xml_content = substr($content, 4, $xml_len);
$from_appid = substr($content, $xml_len + 4);
} catch (Exception $e) {
print $e;
//print $e;
return array(ErrorCode::$IllegalBuffer, null);
}
if ($from_appid != $appid)
Expand Down
6 changes: 3 additions & 3 deletions qywechat.class.php
Expand Up @@ -1784,7 +1784,7 @@ public function encrypt($text, $appid)

try {
//获得16位随机字符串,填充到明文之前
$random = "aaaabbbbccccdddd"; //$this->getRandomStr();
$random = $this->getRandomStr();//"aaaabbbbccccdddd";
$text = $random . pack("N", strlen($text)) . $text . $appid;
// 网络字节序
$size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
Expand All @@ -1803,7 +1803,7 @@ public function encrypt($text, $appid)
//使用BASE64对加密后的字符串进行编码
return array(ErrorCode::$OK, base64_encode($encrypted));
} catch (Exception $e) {
print $e;
//print $e;
return array(ErrorCode::$EncryptAESError, null);
}
}
Expand Down Expand Up @@ -1844,7 +1844,7 @@ public function decrypt($encrypted, $appid)
$xml_content = substr($content, 4, $xml_len);
$from_appid = substr($content, $xml_len + 4);
} catch (Exception $e) {
print $e;
//print $e;
return array(ErrorCode::$IllegalBuffer, null);
}
if ($from_appid != $appid)
Expand Down
19 changes: 11 additions & 8 deletions wechat.class.php
Expand Up @@ -176,7 +176,8 @@ public function valid($return=false)
}
}
$this->postxml = $array[1];
$this->appid = $array[2];//为了没有appid的订阅号。
if (!$this->appid)
$this->appid = $array[2];//为了没有appid的订阅号。
} else {
$this->postxml = $postStr;
}
Expand Down Expand Up @@ -2067,7 +2068,7 @@ public function encrypt($text, $appid)

try {
//获得16位随机字符串,填充到明文之前
$random = "aaaabbbbccccdddd"; //$this->getRandomStr();
$random = $this->getRandomStr();//"aaaabbbbccccdddd";
$text = $random . pack("N", strlen($text)) . $text . $appid;
// 网络字节序
$size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
Expand All @@ -2086,7 +2087,7 @@ public function encrypt($text, $appid)
//使用BASE64对加密后的字符串进行编码
return array(ErrorCode::$OK, base64_encode($encrypted));
} catch (Exception $e) {
print $e;
//print $e;
return array(ErrorCode::$EncryptAESError, null);
}
}
Expand Down Expand Up @@ -2126,14 +2127,16 @@ public function decrypt($encrypted, $appid)
$xml_len = $len_list[1];
$xml_content = substr($content, 4, $xml_len);
$from_appid = substr($content, $xml_len + 4);
if (!$appid)
$appid = $from_appid;
//如果传入的appid是空的,则认为是订阅号,使用数据中提取出来的appid
} catch (Exception $e) {
print $e;
//print $e;
return array(ErrorCode::$IllegalBuffer, null);
}
//if ($from_appid != $appid)
//return array(ErrorCode::$ValidateAppidError, null);
//注释上面两行是为了没有appid的订阅号

if ($from_appid != $appid)
return array(ErrorCode::$ValidateAppidError, null);
//不注释上边两行,避免传入appid是错误的情况
return array(0, $xml_content, $from_appid); //增加appid,为了解决后面加密回复消息的时候没有appid的订阅号会无法回复

}
Expand Down

0 comments on commit 05466f5

Please sign in to comment.