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

Thinkphp Wechat Security Mode message encryption and decryption unsuccessful solution

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

Share

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

This article mainly explains "thinkphp Wechat security mode message encryption and decryption of unsuccessful solutions", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn the "thinkphp Wechat security model message encryption and decryption of unsuccessful solutions" it!

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 (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 ("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\ 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, $size); / / remove redundant content $text=str_replace ('', $text); return $text

Safe mode will work properly.

At this point, I believe that everyone on the "thinkphp Wechat security mode message encryption and decryption of unsuccessful solutions" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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