Skip to main content

解密

    //解密
public function decrypt()
{
$appId = config('wechat.yqcf.app_id');
$encodingAesKey = config('wechat.yqcf.symmetric_key');
$token = config('wechat.yqcf.verify_token');
$timeStamp = $_POST['timestamp'];
$nonce = $_POST['nonce'];

$pc = new WXBizMsgCrypt($token, $encodingAesKey, $appId);
$encryptMsg = file_get_contents("php://input");
$xml_tree = new DOMDocument();
$xml_tree->loadXML($encryptMsg);
$array_e = $xml_tree->getElementsByTagName('Encrypt');
$array_s = $xml_tree->getElementsByTagName('MsgSignature');
// $array_t = $xml_tree->getElementsByTagName('TimeStamp');
// $array_n = $xml_tree->getElementsByTagName('Nonce');

$encrypt = $array_e->item(0)->nodeValue;
$msg_sign = $array_s->item(0)->nodeValue;
// $timeStamp = $array_t->item(0)->nodeValue;
// $nonce = $array_n->item(0)->nodeValue;


$format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
$from_xml = sprintf($format, $encrypt);
// 第三方收到公众号平台发送的消息
$msg = '';
$errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg);
if ($errCode == 0) {
print("解密后: " . $msg . "\n");
} else {
print($errCode . "\n");
}

}