Python base64 + AES EBC模式加密

“”“
先进行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}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值