python aes-cbc 加解密&md5签名并发起api请求

不多废话直接上菜......


from Crypto.Cipher import AES
import base64
import json
import time
import hashlib
import requests

key = 'aaaaaaaaaaaaaaaa'.encode('utf-8')
iv = 'aaaaaaaaaaaaaaaa'.encode('utf-8')
app_id = 'ab3rnynfttcylmq_115_69'


def pad(data):
    text = data + chr(16 - len(data) % 16) * (16 - len(data) % 16)
    return text


def un_pad(s):
    last_num = s[-1]
    text = s[:-last_num]
    return text


def aes_cbc_encrypt(data): 
    data = pad(data)
    data = data.encode('utf-8')
    aes = AES.new(key=key, mode=AES.MODE_CBC, iv=iv) 
 
    result = base64.b64encode(aes.encrypt(data))
    return str(result, 'utf-8') 


def aes_cbc_decrypt(data): 
    aes = AES.new(key=key, mode=AES.MODE_CBC, iv=iv) 
    result = aes.decrypt(base64.b64decode(data))
    result = un_pad(result)
    return str(result, 'utf-8')


def request_api():
    data = {
        "timestamp": int(time.time() * 1000),
        "appId": app_id,
        "data": ''
    }
    b_data = {
        "nonce": '12345',
        "orderId": '90310630',
        "merOrderId": '12345'
    }  # 业务参数
    b_data_str = json.dumps(b_data)
    en_str = aes_cbc_encrypt(b_data_str)
    data['data'] = en_str
    sign_str = 'appId={1}&data={2}&timestamp={3}'.format(app_id, en_str,            data['timestamp'])
    data['sign'] = hashlib.md5(bytes(sign_str, 'UTF-8')).hexdigest().upper()
    print(data)
    home_url = 'http://xxx.xxx/xxx'
    response = requests.post(home_url, json=data).json()
    print(response)


if __name__ == '__main__':
    request_api()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值