在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Python/ 新手遇阻:python3使用AES加密,參數(shù)can't concat str t

新手遇阻:python3使用AES加密,參數(shù)can't concat str to bytes

first_param = "{rid:\"\", offset:\"0\", total:\"true\", limit:\"20\", csrf_token:\"\"}"

def get_params():
    iv = "0102030405060708"
    first_key = forth_param
    second_key = 16 * 'F'
    h_encText = AES_encrypt(first_param, first_key, iv)
    h_encText = AES_encrypt(h_encText, second_key, iv)
    return h_encText

def AES_encrypt(text, key, iv):
    pad = 16 - len(text) % 16
    text = text + pad * chr(pad)
    encryptor = AES.new(key, AES.MODE_CBC, iv)
    encrypt_text = encryptor.encrypt(text)
    encrypt_text = base64.b64encode(encrypt_text)
    return encrypt_text

這段代碼第十一行用python就會拋出can't concat str to bytes無法拼接str,試過轉(zhuǎn)換bytes和轉(zhuǎn)換str再合并都不行。。請教下有沒有什么解決辦法

回答
編輯回答
愛是癌

def AES_encrypt(text, key, iv):

pad = 16 - len(text) % 16

text = text + pad * chr(pad)
encryptor = AES.new(key, AES.MODE_CBC, iv)
encrypt_text = encryptor.encrypt(text)
encrypt_text = base64.b64encode(encrypt_text)
***encrypt_text = str(encrypt_text, encoding="utf-8")***
return encrypt_text
2017年8月24日 11:08
編輯回答
淚染裳

我剛才就遇到這個問題了,因?yàn)锳ES的encrypt方法的參數(shù)要求是bytes str,所以我就講填充符轉(zhuǎn)化為bytes,也就是直接在字符串前面加b’,然后就可以了。

2018年7月23日 17:08