thinkphp微信开发(消息加密解密)_PHP
ThinkPHP
使用thinkphp官方的WeChat包,使用不同模式可以成功,但是安全模式就是不行,现将分析解决结果做下记录。
分析问题:
解密微信服务器消息老是不成功,下载下微信公众平台官方给出的解密文件和WechatCrypt.class.php进行比对发现也没有问题。用file_put_contents函数保存下解密后的文件进行分析。发现官方包解密的xml不是标准的xml格式,所以simplexml_load_string函数无法处理。
/** * 对密文进行解密 * @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); }
解决方法:
输出的xml文件是这样的
<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>
所以需要进行处理才能让simplexml_load_string处理
在输出的明文内容后面加上
//明文内容 $text = substr($decrypt, 4, $size); //去掉多余的内容 $text=str_replace('<\/','</', $text); $text=str_replace('>\n','>', $text); return $text;
以上就是在安全模式下对消息的加密解密方法,希望对大家的学习有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.

ThinkPHP is a high-performance PHP framework with advantages such as caching mechanism, code optimization, parallel processing and database optimization. Official performance tests show that it can handle more than 10,000 requests per second and is widely used in large-scale websites and enterprise systems such as JD.com and Ctrip in actual applications.

ThinkPHP6 backend management system development: Implementing backend functions Introduction: With the continuous development of Internet technology and market demand, more and more enterprises and organizations need an efficient, safe, and flexible backend management system to manage business data and conduct operational management. This article will use the ThinkPHP6 framework to demonstrate through examples how to develop a simple but practical backend management system, including basic functions such as permission control, data addition, deletion, modification and query. Environment preparation Before starting, we need to install PHP, MySQL, Com
