python3.6执行AES加密及解密方法

python版本:3.6.2 

首先安装pycryptodome

cmd执行命令:pip install pycryptodome

特别简单,代码如下:

#!/usr/bin/python
# -*- coding: utf-8 -*-



import base64

from Crypto.Cipher import AES


# str不是16的倍数那就补足为16的倍数
def add_to_16(text):
    while len(text) % 16 != 0:
        text += ''
    return str.encode(text)  # 返回bytes


key = '123456'  # 密码

text = 'abc123def456'  # 待加密文本

aes = AES.new(add_to_16(key), AES.MODE_ECB)  # 初始化加密器

encrypted_text = str(base64.encodebytes(aes.encrypt(add_to_16(text))), encoding='utf8').replace('
', '')  # 加密

text_decrypted = str(aes.decrypt(base64.decodebytes(bytes(encrypted_text, encoding='utf8'))).rstrip(b'').decode("utf8"))  # 解密

print('加密值:', encrypted_text)

print('解密值:', text_decrypted)



运行结果为:
加密值: qR/TQk4INsWeXdMSbCDDdA==

解密值: abc123def456

上面网址是 http://tool.chacuo.net/cryptaes

--------------------- 本文来自 华贺 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/hh775313602/article/details/78991340?utm_source=copy 

原文地址:https://www.cnblogs.com/brady-wang/p/9753981.html