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

encrypt() public method

对明文进行加密
public encrypt ( string $text, $appid ) : string
$text string 需要加密的明文
return string 加密后的密文
    public function encrypt($text, $appid)
    {
        try {
            //获得16位随机字符串,填充到明文之前
            $random = $this->getRandomStr();
            $text = $random . pack("N", strlen($text)) . $text . $appid;
            // 网络字节序
            $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
            $iv = substr($this->key, 0, 16);
            //使用自定义的填充方式对明文进行补位填充
            $pkc_encoder = new Pkcs7Encoder();
            $text = $pkc_encoder->encode($text);
            mcrypt_generic_init($module, $this->key, $iv);
            //加密
            $encrypted = mcrypt_generic($module, $text);
            mcrypt_generic_deinit($module);
            mcrypt_module_close($module);
            //使用BASE64对加密后的字符串进行编码
            return base64_encode($encrypted);
        } catch (\Exception $e) {
            @error_log('Encrypt AES Error: ' . $e->getMessage(), 0);
            return FALSE;
        }
    }

Usage Example

Esempio n. 1
0
     */
    public function __construct($config)
    {
        $this->appId = $config['appId'];
        $this->token = $config['token'];
        $this->encodingAESKey = isset($config['encodingAESKey']) && !empty($config['encodingAESKey']) ? $config['encodingAESKey'] : FALSE;
    }
    /**
	 * 微信消息处理主入口
	 * - 自动处理服务器配置验证
	 *
	 * @return string
	 */
    public function serve()
    {
        // 微信消息GET参数处理
        $this->checkParams();
        // 处理微信URL接入
        $this->accessAuth();
        // 获取微信消息
        return $this->getMessage();
    }
    /**
     * 被动回复微信消息
     *
     * @param $msg array
     *        
     */
    public function reply($msg)
    {
        // 获取消息类型
        $msg_type = '';
        if (gettype($msg) == 'string') {
            $msg_type = 'text_simple';
        } elseif (gettype($msg) == 'array') {
            $msg_type = $msg['type'];
        }
        $xml = '';
        switch ($msg_type) {
            /**
    		 * 1.1 回复文本消息(简洁输入)
    		 *
    		 * Examples:
             * ```
    		 * $wechat->reply('hello world!');
    		 * ```
    		 */
            case 'text_simple':
                $xml = sprintf('<xml>
						<ToUserName><![CDATA[%s]]></ToUserName>
						<FromUserName><![CDATA[%s]]></FromUserName>
						<CreateTime>%s</CreateTime>
						<MsgType><![CDATA[text]]></MsgType>
						<Content><![CDATA[%s]]></Content>
						</xml>', $this->message->FromUserName, $this->message->ToUserName, time(), $msg);
                break;
                /**
    		 * 1.2 回复文本消息
    		 *
    		 * Examples:
             * ```
    		 * $wechat->reply(array(
			 *	'type' => 'text',
			 *	'content' => '嘿嘿,呵呵~~'
			 * ));
			 * ```
    		 */
            /**
    		 * 1.2 回复文本消息
    		 *
    		 * Examples:
             * ```
    		 * $wechat->reply(array(
			 *	'type' => 'text',
			 *	'content' => '嘿嘿,呵呵~~'
			 * ));
			 * ```
    		 */
            case 'text':
                $xml = sprintf('<xml>
						<ToUserName><![CDATA[%s]]></ToUserName>
						<FromUserName><![CDATA[%s]]></FromUserName>
						<CreateTime>%s</CreateTime>
						<MsgType><![CDATA[text]]></MsgType>
						<Content><![CDATA[%s]]></Content>
						</xml>', $this->message->FromUserName, $this->message->ToUserName, time(), $msg['content']);
                break;
                /**
    		 * 2 回复图片消息
    		 *
    		 * Examples:
             * ```
    		 * $wechat->reply(array(
	 		 * 	'type' => 'image',