Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of encryption and decryption of messages developed by thinkphp Wechat

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the example analysis of message encryption and decryption developed by thinkphp Wechat. The article is very detailed and has a certain reference value. Interested friends must finish reading it!

Using thinkphp's official WeChat package, different modes can be successful, but security mode is not. Now record the results of the analysis and solution.

Analyze the problem:

Decrypting Wechat server messages is always unsuccessful, download the Wechat public platform official decryption files and WechatCrypt.class.php comparison found no problem. Use the file_put_contents function to save the decrypted file for analysis. It is 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.

/ * decrypt the ciphertext * @ param string $encrypt ciphertext * @ return string plaintext * / public function decrypt ($encrypt) {/ / BASE64 decode $encrypt = base64_decode ($encrypt); / / Open the encryption algorithm module $td = mcrypt_module_open (MCRYPT_RIJNDAEL_128,', MCRYPT_MODE_CBC,'') / / initialize the encryption algorithm module mcrypt_generic_init ($td, $this- > cyptKey, substr ($this- > cyptKey, 0,16); / / perform decryption $decrypt = mdecrypt_generic ($td, $encrypt); / / remove the PKCS7 complement $decrypt = self::PKCS7Decode ($decrypt, mcrypt_enc_get_key_size ($td)); / / close the encryption algorithm module mcrypt_generic_deinit ($td); mcrypt_module_close ($td); if ($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 ("illegal APP_ID!") ;} / / plaintext content $text = substr ($decrypt, 4, $size); return $text;} / * * PKCS7 padding character * @ param string $text padding character * @ param integer $size Block length * / private static function PKCS7Encode ($text, $size) {/ / string length $str_size = strlen ($text); / / padding length $pad_size = $size-($str_size% $size); $pad_size = $pad_size?: $size / / populated characters $pad_chr = chr ($pad_size); / / execute padding $text = str_pad ($text, $str_size + $pad_size, $pad_chr, STR_PAD_RIGHT); return $text } / * Delete PKCS7 filled character * @ param string $text populated character * @ param integer $size Block length * / private static function PKCS7Decode ($text, $size) {/ / get complement character $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 looks like this

\ n\ n1448944621\ n\ n 6223169761311044588\ n

So it needs to be processed for simplexml_load_string to handle it.

Add after the plaintext of the output

/ / plaintext content $text= substr ($decrypt, 4, $size); / / remove redundant content $text=str_replace ('', $text); return $text; above is "sample analysis of message encryption and decryption developed by thinkphp Wechat". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report