“”“
先进行Base64加密,再进行AES ecb模式加密;EBC 模式需要补全
“”“
‘’’
ECB模式补足16位
‘’’
def add_to_16(byte_content):
block_size = 16
if block_size > len(byte_content):
padding = block_size - len(byte_content)
byte_content = byte_content + chr(padding).encode(‘utf-8’)*padding
elif block_size < len(byte_content):
padding = block_size - (len(byte_content) % block_size)
byte_content = byte_content + chr(padding).encode(‘utf-8’) * padding
else:
byte_content = byte_content + chr(16).encode(‘utf-8’) * 16
return byte_content
print('生成AES加密key--------------------------------------')
appendkey = first + xx +second +appId + third
appsecret = appSecret.lower()
deskey =appsecret[int(xx): int(xx)+16]
data = { "appId": "10005",
"gameId": 14,
"unionId": "0",
"siteId": "0",
"sign": signvar,
"terminalSource": 0,
"timestamp": timestamp,
"mobiles": "15088",
"content": "longzhu error",
"smsType": "2"}
datas = json.dumps(data,indent=2)
#待加密业务的base64
print('待加密业务base64格式-------------------------------------')
encodestr1 = base64.b64encode(datas.encode(encoding='utf-8'))
#进行AES加密
print('AES加密------------------------------------------------------')
aes_cipher = AES.new(deskey.encode(encoding='utf-8'), AES.MODE_ECB) # ECB模式无需向量iv
b_cipher = aes_cipher.encrypt(add_to_16(encodestr1))
# 把AES加密后的内容转成字符串,拼接appendkey
print('AES加密拼接appendkey-------------------------------------------')
encodestrs = base64.b64encode(b_cipher)
b_cipher_str = encodestrs.decode(encoding='utf-8') + appendkey
data = {"data": b_cipher_str}