aes之ecb模式的加密解密
from Crypto.Cipher import AES import base64 BLOCK_SIZE = 16 # Bytes pad = lambda s: s + (BLOCK_SIZE - len(s.encode()) % BLOCK_SIZE) * chr(BLOCK_SIZE - len(s.encode()) % BLOCK_SIZE) unpad = lambda s: s[:-ord(s[len(s) - 1:])] def aesEncrypt(key, data): ''' AES的ECB模式加密方法 :param key: 密钥 :param data:被加密字符串(明文) :return:密文 ''' key = key.encode('utf8') # 字符串补位 data = pad(str(data)) cipher = AES.new(key, AES.MODE_ECB) # 加密后得到的是bytes类型的数据,使用Base64进行编码,返回byte字符串 data_bytes = bytes(data, encoding="utf-8") result = cipher.encrypt(data_bytes) encodestrs = base64.b64encode(result) enctext = encodestrs.decode('utf8') return enctext def aesDecrypt(key, data): ''' :param key: 密钥 :param data: 加密后的数据(密文) :return:明文 ''' key = key.encode('utf8') data = base64.b64decode(data) cipher = AES.new(key, AES.MODE_ECB) # 去补位 text_decrypted = unpad(cipher.decrypt(data)) text_decrypted = text_decrypted.decode('utf8') return text_decrypted if __name__ == '__main__': key = '5c44c819appsapi0' # data = 'herish acorn' data = '吴森' ecdata = aesEncrypt(key, data) aesDecrypt(key, ecdata) res_data = aesDecrypt(key, ecdata) s = f""" key:{key} data:{data} ecdata:{ecdata} res_data:{res_data} """ print(s)