Using the official WeChat package of thinkphp, different modes can be used successfully, but the safe mode is not working. Now the analysis and solution results are recorded.
Analysis of the problem:
Decrypting WeChat server messages is always unsuccessful. Download the decrypted file officially provided by WeChat public platform and compare it with WechatCrypt.class.php and find that there is no problem. Use the file_put_contents function to save the decrypted file for analysis. It was found that the xml decrypted by the official package is not in the standard xml format, so the simplexml_load_string function cannot handle it.
/** * 对密文进行解密 * @param string $encrypt 密文 * @return string 明文 */ public function decrypt($encrypt){ //BASE64解码 $encrypt = base64_decode($encrypt); //打开加密算法模块 $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); //初始化加密算法模块 mcrypt_generic_init($td, $this->cyptKey, substr($this->cyptKey, 0, 16)); //执行解密 $decrypt = mdecrypt_generic($td, $encrypt); //去除PKCS7补位 $decrypt = self::PKCS7Decode($decrypt, mcrypt_enc_get_key_size($td)); //关闭加密算法模块 mcrypt_generic_deinit($td); mcrypt_module_close($td); if(strlen($decrypt) < 16){ throw new \Exception("非法密文字符串!"); } //去除随机字符串 $decrypt = substr($decrypt, 16); //获取网络字节序 $size = unpack("N", substr($decrypt, 0, 4)); $size = $size[1]; //APP_ID $appid = substr($decrypt, $size + 4); //验证APP_ID if($appid !== $this->appId){ throw new \Exception("非法APP_ID!"); } //明文内容 $text = substr($decrypt, 4, $size); return $text; } /** * PKCS7填充字符 * @param string $text 被填充字符 * @param integer $size Block长度 */ private static function PKCS7Encode($text, $size){ //字符串长度 $str_size = strlen($text); //填充长度 $pad_size = $size - ($str_size % $size); $pad_size = $pad_size ? : $size; //填充的字符 $pad_chr = chr($pad_size); //执行填充 $text = str_pad($text, $str_size + $pad_size, $pad_chr, STR_PAD_RIGHT); return $text; } /** * 删除PKCS7填充的字符 * @param string $text 已填充的字符 * @param integer $size Block长度 */ private static function PKCS7Decode($text, $size){ //获取补位字符 $pad_str = ord(substr($text, -1)); if ($pad_str < 1 || $pad_str > $size) { $pad_str= 0; } return substr($text, 0, strlen($text) - $pad_str); }
Solution:
The output xml file is like this
<xml> <ToUserName><![CDATA[gh_249aeb986d99]]><\/ToUserName>\n <FromUserName><![CDATA[oopVmxHZaeQkDPsRcbpwXKkH-J2Q]]><\/FromUserName>\n <CreateTime>1448944621<\/CreateTime>\n <MsgType><![CDATA[text]]><\/MsgType>\n <Content><![CDATA[\u7ecf\u7406]]><\/Content>\n <MsgId>6223169761311044588<\/MsgId>\n <\/xml>
So it needs to be processed before simplexml_load_string can be processed
Add
after the output plain text content//明文内容 $text = substr($decrypt, 4, $size); //去掉多余的内容 $text=str_replace('<\/','</', $text); $text=str_replace('>\n','>', $text); return $text;
The above is the encryption and decryption method of messages in safe mode. I hope it will be helpful to everyone's learning.