Esempio n. 1
0
    def encrypt_conn_data(cls, conn_id, data):
        if not SysConfig.is_proto_encrypt():
            return RET_OK, '', data

        if type(data) is not bytes:
            data = bytes_utf8(str(data))

        len_src = len(data)
        mod_tail_len = (len_src % 16)

        # AES 要求源数据长度是16的整数倍, 不足的话要补0
        if mod_tail_len != 0:
            data += (b'\x00' * (16 - mod_tail_len))

        aes_cryptor = FutuConnMng.get_conn_aes_cryptor(conn_id)
        if aes_cryptor:
            data = aes_cryptor.encrypt(data)

            # 增加一个16字节的数据块(目前只有最后一个字节有用),如果对原数据有补数据,记录原数据最后一个数据块真实长度
            data_tail = b'\x00' * 15 + bytes_utf8(chr(mod_tail_len))

            data_tail = data_tail[-16:]
            data += data_tail
            return RET_OK, '', data

        return RET_ERROR, 'invalid connid', data
Esempio n. 2
0
    def send_msg_to_users_customer_service_news(self, openid, msg):

        ret, access_token = self.get_access_token()
        if ret != RET_OK:
            return ret, access_token
        body = {"touser": openid, "msgtype": "text", "text": {"content": msg}}
        response = requests.post(
            url="https://api.weixin.qq.com/cgi-bin/message/custom/send",
            params={'access_token': access_token},
            data=bytes_utf8(json.dumps(body, ensure_ascii=False)))

        result = response.json()
        if result['errmsg'] != 'ok':
            return RET_ERROR, result
        return RET_OK, result
Esempio n. 3
0
    def encrypt(cls, data):
        if RsaCrypt.CHIPPER is None:
            rsa = SysConfig.get_init_rsa_obj()
            RsaCrypt.CHIPPER = Cipher_pkcs1.new(rsa)

        if type(data) is not bytes:
            data = bytes_utf8(str(data))

        # 单次加密串的长度最大为(key_size / 8) - 11
        # 1024 bit的证书用100, 2048 bit的证书用 200
        one_len = 100
        ret_data = b''
        for i in range(0, len(data), one_len):
            ret_data += RsaCrypt.CHIPPER.encrypt(data[i:i + one_len])
        return ret_data
Esempio n. 4
0
    def send_template_msg(self, openid, msg_list):

        ret, access_token = self.get_access_token()
        if ret != RET_OK:
            return ret, access_token

        body = self.list_to_template(openid, msg_list)

        response = requests.post(
            url="https://api.weixin.qq.com/cgi-bin/message/template/send",
            params={'access_token': access_token},
            data=bytes_utf8(json.dumps(body, ensure_ascii=False)))
        result = response.json()
        if result['errmsg'] != 'ok':
            return RET_ERROR, result
        return RET_OK, result
Esempio n. 5
0
    def get_conn_aes_cryptor(cls, conn_id):
        conn_info = FutuConnMng.get_conn_info(conn_id)
        if not conn_info:
            return None

        if 'aes_cryptor' not in conn_info:
            key = FutuConnMng.get_conn_key(conn_id)
            if not key:
                return None

            key_tmp = bytes_utf8(str(key))
            cryptor = AES.new(key_tmp, AES.MODE_ECB)
            conn_info['aes_cryptor'] = cryptor
            return cryptor

        return conn_info['aes_cryptor']