不多废话直接上菜......
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}×tamp={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()