def _encrypt_message(self, msg, nonce, timestamp=None):
        """将公众号回复用户的消息加密打包

        :param msg: 待回复用户的消息，xml格式的字符串
        :param nonce: 随机串，可以自己生成，也可以用URL参数的nonce
        :param timestamp: 时间戳，可以自己生成，也可以用URL参数的timestamp,如为None则自动用当前时间
        :return: 加密后的可以直接回复用户的密文，包括msg_signature, timestamp, nonce, encrypt的xml格式的字符串
        """
        xml = """<xml>
<Encrypt><![CDATA[{encrypt}]]></Encrypt>
<MsgSignature><![CDATA[{signature}]]></MsgSignature>
<TimeStamp>{timestamp}</TimeStamp>
<Nonce><![CDATA[{nonce}]]></Nonce>
</xml>"""
        nonce = to_binary(nonce)
        timestamp = to_binary(timestamp) or to_binary(int(time.time()))
        encrypt = self.__pc.encrypt(to_text(msg), self.__id)
        # 生成安全签名
        signature = get_sha1_signature(self.__token, timestamp, nonce, encrypt)
        return to_text(xml.format(
            encrypt=to_text(encrypt),
            signature=to_text(signature),
            timestamp=to_text(timestamp),
            nonce=to_text(nonce)
        ))
def _encrypt_message(self, msg, nonce, timestamp=None):
        """将公众号回复用户的消息加密打包

        :param msg: 待回复用户的消息，xml格式的字符串
        :param nonce: 随机串，可以自己生成，也可以用URL参数的nonce
        :param timestamp: 时间戳，可以自己生成，也可以用URL参数的timestamp,如为None则自动用当前时间
        :return: 加密后的可以直接回复用户的密文，包括msg_signature, timestamp, nonce, encrypt的xml格式的字符串
        """
        xml = """<xml>
<Encrypt><![CDATA[{encrypt}]]></Encrypt>
<MsgSignature><![CDATA[{signature}]]></MsgSignature>
<TimeStamp>{timestamp}</TimeStamp>
<Nonce><![CDATA[{nonce}]]></Nonce>
</xml>"""
        nonce = to_binary(nonce)
        timestamp = to_binary(timestamp) or to_binary(int(time.time()))
        encrypt = self.__pc.encrypt(to_text(msg), self.__id)
        # 生成安全签名
        signature = get_sha1_signature(self.__token, timestamp, nonce, encrypt)
        return to_text(xml.format(
            encrypt=to_text(encrypt),
            signature=to_text(signature),
            timestamp=to_text(timestamp),
            nonce=to_text(nonce)
        ))
def _encrypt_message ( self , msg , nonce , timestamp = None ) : xml = """<xml>
<Encrypt><![CDATA[{encrypt}]]></Encrypt>
<MsgSignature><![CDATA[{signature}]]></MsgSignature>
<TimeStamp>{timestamp}</TimeStamp>
<Nonce><![CDATA[{nonce}]]></Nonce>
</xml>""" nonce = to_binary ( nonce ) timestamp = to_binary ( timestamp ) or to_binary ( int ( time . time ( ) ) ) encrypt = self . __pc . encrypt ( to_text ( msg ) , self . __id ) # 生成安全签名 signature = get_sha1_signature ( self . __token , timestamp , nonce , encrypt ) return to_text ( xml . format ( encrypt = to_text ( encrypt ) , signature = to_text ( signature ) , timestamp = to_text ( timestamp ) , nonce = to_text ( nonce ) ) )
将公众号回复用户的消息加密打包

        :param msg: 待回复用户的消息，xml格式的字符串
        :param nonce: 随机串，可以自己生成，也可以用URL参数的nonce
        :param timestamp: 时间戳，可以自己生成，也可以用URL参数的timestamp,如为None则自动用当前时间
        :return: 加密后的可以直接回复用户的密文，包括msg_signature, timestamp, nonce, encrypt的xml格式的字符串
将公众号回复用户的消息加密打包