Home WeChat Applet WeChat Development Share an example tutorial on data decryption in WeChat development

Share an example tutorial on data decryption in WeChat development

May 20, 2017 pm 04:19 PM

Recently I am using the thinkphp framework to write the server side of the WeChat applet. Perhaps it is because of Virgo that I downloaded a PHP WeChat decryption demo from the official website. It can be integrated into a class without much code, so I have to It is written in several categories. Considering that the thinkphp 5.0 framework seems too painful for extended class references, it is integrated into one class for easy calling. Friends in need can download it.

Share an example tutorial on data decryption in WeChat development


##Baidu disk download address:

pan.baidu.com/s/1kURMQ2b

<?php
/**
 * 对微信小程序用户加密数据的解密示例代码.
 *
 * @copyright Copyright (c) 1998-2014 Tencent Inc.
 */

class WXBizDataCrypt
{
    private $appid;
    private $sessionKey;
    private $blockSize = 16;

    private $OKs = 0;
    private $IllegalAesKey = -41001;
    private $IllegalIv = -41002;
    private $IllegalBuffer = -41003;
    private $DecodeBase64Error = -41004;

    /**
     * 检验数据的真实性,并且获取解密后的明文.
     * @param $encryptedData string 加密的用户数据
     * @param $iv string 与用户数据一同返回的初始向量
     * @param $data string 解密后的原文
     *
     * @return int 成功0,失败返回对应的错误码
     */
    public function decryptData($appid,$sessionKey,$encryptedData, $iv, &$data )
    {
        $this->sessionKey = $sessionKey;
        $this->appid = $appid;

        if (strlen($this->sessionKey) != 24) {
            return $this->IllegalAesKey;
        }
        $aesKey=base64_decode($this->sessionKey);

        if (strlen($iv) != 24) {
            return $this->IllegalIv;
        }
        $aesIV=base64_decode($iv);

        $aesCipher=base64_decode($encryptedData);

        $result = $this->decrypt($aesKey,$aesCipher,$aesIV);

        if ($result[0] != 0) {
            return $result[0];
        }

        $dataObj=json_decode( $result[1] );

        if( $dataObj  == NULL )
        {
            return $this->IllegalBuffer;
        }
        if( $dataObj->watermark->appid != $this->appid )
        {
            return $this->IllegalBuffer;
        }
        $data = $result[1];
        return $this->OKs;
    }

    /**
     * 对密文进行解密
     * @param string $aesCipher 需要解密的密文
     * @param string $aesIV 解密的初始向量
     * @return string 解密得到的明文
     */
    private function decrypt($key, $aesCipher, $aesIV )
    {
        try {

            $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, &#39;&#39;, MCRYPT_MODE_CBC, &#39;&#39;);

            mcrypt_generic_init($module, $key, $aesIV);

            //解密
            $decrypted = mdecrypt_generic($module, $aesCipher);
            mcrypt_generic_deinit($module);
            mcrypt_module_close($module);
        } catch (Exception $e) {
            return array($this->IllegalBuffer, null);
        }


        try {
            //去除补位字符
            $result = $this->decode($decrypted);

        } catch (Exception $e) {
            //print $e;
            return array($this->IllegalBuffer, null);
        }
        return array(0, $result);
    }

    /**
     * 对需要加密的明文进行填充补位
     * @param $text 需要进行填充补位操作的明文
     * @return 补齐明文字符串
     */
    private function encode( $text )
    {
        $block_size = $this->blockSize;
        $text_length = strlen( $text );
        //计算需要填充的位数
        $amount_to_pad = $this->blockSize - ( $text_length % $this->blockSize );
        if ( $amount_to_pad == 0 ) {
            $amount_to_pad = $this->blockSize;
        }
        //获得补位所用的字符
        $pad_chr = chr( $amount_to_pad );
        $tmp = "";
        for ( $index = 0; $index < $amount_to_pad; $index++ ) {
            $tmp .= $pad_chr;
        }
        return $text . $tmp;
    }

    /**
     * 对解密后的明文进行补位删除
     * @param decrypted 解密后的明文
     * @return 删除填充补位后的明文
     */
    private function decode($text)
    {

        $pad = ord(substr($text, -1));
        if ($pad < 1 || $pad > 32) {
            $pad = 0;
        }
        return substr($text, 0, (strlen($text) - $pad));
    }

}
Copy after login

If necessary, please download it yourself. ps: If you are confused about the WeChat login authorization process, you can leave a message for consultation.

【Related recommendations】

1.

WeChat public account platform source code download

2.

小 Pigcms (PigCms) micro-e-commerce System operation version (independent Weidian mall + three-level distribution system)

3.

WeChat People Network v3.4.5 Advanced Commercial Edition WeChat Rubik’s Cube Source Code

The above is the detailed content of Share an example tutorial on data decryption in WeChat development. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
Java development: How to implement data encryption and decryption Java development: How to implement data encryption and decryption Sep 20, 2023 am 08:46 AM

Java development: How to implement data encryption and decryption, specific code examples are required. Introduction: In the context of the modern information era, data security has become increasingly important. For developers, how to protect the security of user data is an essential skill. Data encryption and decryption is one of the important means of data security. This article will introduce how to use Java language to implement data encryption and decryption, and give specific code examples. 1. Introduction to data encryption algorithm Data encryption refers to processing original data through a certain algorithm so that it can be processed without

How to encrypt and decrypt data in Python How to encrypt and decrypt data in Python Oct 18, 2023 am 10:15 AM

How to encrypt and decrypt data in Python, specific code examples are required Data encryption and decryption are very important concepts in the field of information security. In practical applications, we often need to encrypt sensitive data to prevent unauthorized access and information leakage. Python is a powerful programming language that provides a wealth of libraries and functions to implement data encryption and decryption operations. This article will introduce some commonly used encryption algorithms and specific code examples for implementing data encryption and decryption in Python. 1. MD5

How to use Laravel to implement data encryption and decryption functions How to use Laravel to implement data encryption and decryption functions Nov 03, 2023 pm 04:19 PM

Overview of how to use Laravel to implement data encryption and decryption functions: In modern Internet applications, it is very important to protect the security of user data. One of the important security measures is the encrypted storage of sensitive data. Laravel, as a popular PHP framework, provides support for various encryption and decryption functions. This article will introduce how to use Laravel's encryption library to encrypt and decrypt data. Step 1: Install Laravel First, make sure Larav is installed locally or on the server

PHP WeChat development: How to implement message encryption and decryption PHP WeChat development: How to implement message encryption and decryption May 13, 2023 am 11:40 AM

PHP is an open source scripting language that is widely used in web development and server-side programming, especially in WeChat development. Today, more and more companies and developers are starting to use PHP for WeChat development because it has become a truly easy-to-learn and easy-to-use development language. In WeChat development, message encryption and decryption are a very important issue because they involve data security. For messages without encryption and decryption methods, hackers can easily obtain the data, posing a threat to users.

Methods and techniques for data encryption and decryption using PHP arrays Methods and techniques for data encryption and decryption using PHP arrays Jul 16, 2023 pm 04:02 PM

Methods and techniques for data encryption and decryption using PHP arrays Summary: Data encryption plays an important role in information security. This article will introduce methods and techniques for using PHP arrays to implement data encryption and decryption in order to protect the security of sensitive information. Introduction In modern society, network security issues have attracted increasing attention. To protect the security of sensitive information, data encryption is a common method. PHP array is a powerful and flexible data structure that can be used to store and manipulate data. By storing sensitive data in PHP array

Use Gin framework to implement data encryption and decryption functions Use Gin framework to implement data encryption and decryption functions Jun 23, 2023 am 08:12 AM

In the modern Internet field, data security has always been an important topic. When transmitting, storing, and processing data, we need to use various technical means to ensure data security. One of the most important technical means is data encryption. Gin is a web framework based on the Go language. Its simplicity, ease of use, efficiency and stability have been favored by many developers. This article will introduce how to use the Gin framework to implement data encryption and decryption functions. Let us learn about it together. First, we need to understand some basic knowledge of encryption algorithms.

How to implement database data encryption and decryption through thinkorm How to implement database data encryption and decryption through thinkorm Jul 29, 2023 pm 04:03 PM

How to implement database data encryption and decryption through thinkorm Background: In development, database data security is a crucial part. In order to protect users' privacy information and comply with relevant regulations, we often need to encrypt and store sensitive data. This article will introduce how to use thinkorm and related encryption algorithms to implement database data encryption and decryption. Encryption and decryption of database fields can be implemented in thinkorm by defining mutator and accessor methods.

RPC service based on ThinkPHP6 and Swoole to implement data encryption and decryption RPC service based on ThinkPHP6 and Swoole to implement data encryption and decryption Oct 12, 2023 pm 02:57 PM

Implementing data encryption and decryption using RPC services based on ThinkPHP6 and Swoole As network security issues become increasingly prominent, the need for data encryption and decryption becomes more and more important. In web applications, communication between different servers can be achieved through RPC (remote procedure call) technology, and data encryption and decryption can ensure the security of data during the communication process. This article will introduce how to implement an RPC service based on ThinkPHP6 and Swoole framework, and add data encryption and decryption to it.

See all articles