Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
sbzhu committed Oct 11, 2018
1 parent cc7bffb commit 1c371a5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
25 changes: 10 additions & 15 deletions callback/WXBizMsgCrypt.php
@@ -1,7 +1,7 @@
<?php

/**
* 对公众平台发送给公众账号的消息加解密示例代码.
* 企业微信回调消息加解密示例代码.
*
* @copyright Copyright (c) 1998-2014 Tencent Inc.
*/
Expand All @@ -12,27 +12,23 @@
include_once "pkcs7Encoder.php";
include_once "errorCode.php";

/**
* 1.第三方回复加密消息给公众平台;
* 2.第三方收到公众平台发送的消息,验证消息的安全性,并对消息进行解密。
*/
class WXBizMsgCrypt
{
private $m_sToken;
private $m_sEncodingAesKey;
private $m_sCorpid;
private $m_sReceiveId;

/**
* 构造函数
* @param $token string 公众平台上,开发者设置的token
* @param $encodingAesKey string 公众平台上,开发者设置的EncodingAESKey
* @param $Corpid string 公众平台的Corpid
* @param $token string 开发者设置的token
* @param $encodingAesKey string 开发者设置的EncodingAESKey
* @param $receiveId string, 不同应用场景传不同的id
*/
public function __construct($token, $encodingAesKey, $Corpid)
public function __construct($token, $encodingAesKey, $receiveId)
{
$this->m_sToken = $token;
$this->m_sEncodingAesKey = $encodingAesKey;
$this->m_sCorpid = $Corpid;
$this->m_sReceiveId = $receiveId;
}

/*
Expand Down Expand Up @@ -65,7 +61,7 @@ public function VerifyURL($sMsgSignature, $sTimeStamp, $sNonce, $sEchoStr, &$sRe
return ErrorCode::$ValidateSignatureError;
}

$result = $pc->decrypt($sEchoStr, $this->m_sCorpid);
$result = $pc->decrypt($sEchoStr, $this->m_sReceiveId);
if ($result[0] != 0) {
return $result[0];
}
Expand Down Expand Up @@ -94,7 +90,7 @@ public function EncryptMsg($sReplyMsg, $sTimeStamp, $sNonce, &$sEncryptMsg)
$pc = new Prpcrypt($this->m_sEncodingAesKey);

//加密
$array = $pc->encrypt($sReplyMsg, $this->m_sCorpid);
$array = $pc->encrypt($sReplyMsg, $this->m_sReceiveId);
$ret = $array[0];
if ($ret != 0) {
return $ret;
Expand Down Expand Up @@ -159,7 +155,6 @@ public function DecryptMsg($sMsgSignature, $sTimeStamp = null, $sNonce, $sPostDa
}

$encrypt = $array[1];
$touser_name = $array[2];

//验证安全签名
$sha1 = new SHA1;
Expand All @@ -175,7 +170,7 @@ public function DecryptMsg($sMsgSignature, $sTimeStamp = null, $sNonce, $sPostDa
return ErrorCode::$ValidateSignatureError;
}

$result = $pc->decrypt($encrypt, $this->m_sCorpid);
$result = $pc->decrypt($encrypt, $this->m_sReceiveId);
if ($result[0] != 0) {
return $result[0];
}
Expand Down
14 changes: 7 additions & 7 deletions callback/pkcs7Encoder.php
Expand Up @@ -76,14 +76,14 @@ public function __construct($k)
* 加密
*
* @param $text
* @param $corpid
* @param $receiveId
* @return array
*/
public function encrypt($text, $corpid)
public function encrypt($text, $receiveId)
{
try {
//拼接
$text = $this->getRandomStr() . pack('N', strlen($text)) . $text . $corpid;
$text = $this->getRandomStr() . pack('N', strlen($text)) . $text . $receiveId;
//添加PKCS#7填充
$pkc_encoder = new PKCS7Encoder;
$text = $pkc_encoder->encode($text);
Expand All @@ -100,10 +100,10 @@ public function encrypt($text, $corpid)
* 解密
*
* @param $encrypted
* @param $corpid
* @param $receiveId
* @return array
*/
public function decrypt($encrypted, $corpid)
public function decrypt($encrypted, $receiveId)
{
try {
//解密
Expand All @@ -123,12 +123,12 @@ public function decrypt($encrypted, $corpid)
$len_list = unpack('N', substr($content, 0, 4));
$xml_len = $len_list[1];
$xml_content = substr($content, 4, $xml_len);
$from_corpid = substr($content, $xml_len + 4);
$from_receiveId = substr($content, $xml_len + 4);
} catch (Exception $e) {
print $e;
return [ErrorCode::$IllegalBuffer, null];
}
if ($from_corpid != $corpid) {
if ($from_receiveId != $receiveId) {
return [ErrorCode::$ValidateCorpidError, null];
}
return [0, $xml_content];
Expand Down
17 changes: 12 additions & 5 deletions callback/xmlparse.php
Expand Up @@ -20,13 +20,11 @@ public function extract($xmltext)
$xml = new DOMDocument();
$xml->loadXML($xmltext);
$array_e = $xml->getElementsByTagName('Encrypt');
$array_a = $xml->getElementsByTagName('ToUserName');
$encrypt = $array_e->item(0)->nodeValue;
$tousername = $array_a->item(0)->nodeValue;
return array(0, $encrypt, $tousername);
return array(0, $encrypt);
} catch (Exception $e) {
print $e . "\n";
return array(ErrorCode::$ParseXmlError, null, null);
return array(ErrorCode::$ParseXmlError, null);
}
}

Expand All @@ -50,5 +48,14 @@ public function generate($encrypt, $signature, $timestamp, $nonce)

}

//
// Test
/*
$sPostData = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><AgentID><![CDATA[toAgentID]]></AgentID><Encrypt><![CDATA[msg_encrypt]]></Encrypt></xml>";
$xmlparse = new XMLParse;
$array = $xmlparse->extract($sPostData);
var_dump($array);
*/

?>

?>

0 comments on commit 1c371a5

Please sign in to comment.