Gaoming13\WechatPhpSdk\Utils\Prpcrypt::decrypt PHP Method

decrypt() public method

对密文进行解密
public decrypt ( string $encrypted, $appid ) : string
$encrypted string 需要解密的密文
return string 解密得到的明文
    public function decrypt($encrypted, $appid)
    {
        try {
            //使用BASE64对需要解密的字符串进行解码
            $ciphertext_dec = base64_decode($encrypted);
            $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
            $iv = substr($this->key, 0, 16);
            mcrypt_generic_init($module, $this->key, $iv);
            //解密
            $decrypted = mdecrypt_generic($module, $ciphertext_dec);
            mcrypt_generic_deinit($module);
            mcrypt_module_close($module);
        } catch (\Exception $e) {
            @error_log('Decrypt AES Error: ' . $e->getMessage(), 0);
            return FALSE;
        }
        try {
            //去除补位字符
            $pkc_encoder = new Pkcs7Encoder();
            $result = $pkc_encoder->decode($decrypted);
            //去除16位随机字符串,网络字节序和AppId
            if (strlen($result) < 16) {
                return "";
            }
            $content = substr($result, 16, strlen($result));
            $len_list = unpack("N", substr($content, 0, 4));
            $xml_len = $len_list[1];
            $xml_content = substr($content, 4, $xml_len);
            $from_appid = substr($content, $xml_len + 4);
        } catch (\Exception $e) {
            @error_log('Illegal Buffer: ' . $e->getMessage(), 0);
            return FALSE;
        }
        if ($from_appid != $appid) {
            @error_log('Validate Appid Error', 0);
            return FALSE;
        }
        return $xml_content;
    }

Usage Example

	 		 *	'media_id' => 'rVT43tfDwjh4p1BV2gJ5D7Zl2BswChO5L_llmlphLaTPytcGcguBAEJ1qK4cg4r_'
			 * ));
			 * ```
    		 */
            /**
    		 * 3 回复语音消息
    		 *
    		 * Examples:
             * ```
    		 * $wechat->reply(array(
	 		 * 	'type' => 'voice',
	 		 *	'media_id' => 'rVT43tfDwjh4p1BV2gJ5D7Zl2BswChO5L_llmlphLaTPytcGcguBAEJ1qK4cg4r_'
			 * ));
			 * ```
    		 */
            case 'voice':
                $xml = sprintf('<xml>
						<ToUserName><![CDATA[%s]]></ToUserName>
						<FromUserName><![CDATA[%s]]></FromUserName>
						<CreateTime>%s</CreateTime>
						<MsgType><![CDATA[voice]]></MsgType>
						<Voice>
						<MediaId><![CDATA[%s]]></MediaId>
						</Voice>
						</xml>', $this->message->FromUserName, $this->message->ToUserName, time(), $msg['media_id']);
                break;
                /**
    		 * 4 回复视频消息
    		 *
    		 * Examples:
             * ```
    		 * $wechat->reply(array(
	 		 *	'type' => 'video',
	 		 *	'media_id' => 'yV0l71NL0wtpRA8OMX0-dBRQsMVyt3fspPUzurIS3psi6eWOrb_WlEeO39jasoZ8',
	 		 *	'title' => '视频消息的标题',			//可选
	 		 *	'description' => '视频消息的描述'		//可选
			 * ));
			 * ```
    		 */
            /**
    		 * 4 回复视频消息
    		 *
    		 * Examples:
             * ```
    		 * $wechat->reply(array(
	 		 *	'type' => 'video',
	 		 *	'media_id' => 'yV0l71NL0wtpRA8OMX0-dBRQsMVyt3fspPUzurIS3psi6eWOrb_WlEeO39jasoZ8',