Home php教程 php手册 thinkphp微信开发(消息加密解密)

thinkphp微信开发(消息加密解密)

Jun 06, 2016 pm 07:36 PM
thinkphp WeChat development

这篇文章主要介绍了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) 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 $size) { $pad_str= 0; } return substr($text, 0, strlen($text) - $pad_str); }

解决方法:
           输出的xml文件是这样的

\n \n 1448944621\n \n \n 6223169761311044588\n

所以需要进行处理才能让simplexml_load_string处理

在输出的明文内容后面加上

//明文内容 $text = substr($decrypt, 4, $size); //去掉多余的内容 $text=str_replace('\n','>', $text); return $text;

以上就是在安全模式下对消息的加密解密方法,希望对大家的学习有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

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.

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

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.

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

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.

Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Nov 22, 2023 pm 12:01 PM

"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.

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

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.

How to install thinkphp How to install thinkphp Apr 09, 2024 pm 05:42 PM

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.

How is the performance of thinkphp? How is the performance of thinkphp? Apr 09, 2024 pm 05:24 PM

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.

Development suggestions: How to use the ThinkPHP framework for API development Development suggestions: How to use the ThinkPHP framework for API development Nov 22, 2023 pm 05:18 PM

Development suggestions: How to use the ThinkPHP framework for API development. With the continuous development of the Internet, the importance of API (Application Programming Interface) has become increasingly prominent. API is a bridge for communication between different applications. It can realize data sharing, function calling and other operations, and provides developers with a relatively simple and fast development method. As an excellent PHP development framework, the ThinkPHP framework is efficient, scalable and easy to use.

See all articles