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 += '\0'
    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='utf-8').replace('\n', '')  # 加密

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

print('加密值:', encrypted_text)

print('解密值:', text_decrypted)


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

解密值: abc123def456

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

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

相关文章:

  • 2021-10-17
  • 2021-09-23
猜你喜欢
  • 2022-12-23
  • 2021-08-11
  • 2022-12-23
  • 2022-12-23
  • 2021-08-18
  • 2022-12-23
  • 2021-04-15
相关资源
相似解决方案