python实现AES加解密

代码

from Crypto.Cipher import AES

import base64


AES_KEY ="QWER1234asdf5678".encode()


def aes_encrypt(text):

    text = text.encode()

    amount_to_pad = AES.block_size - (len(text) % AES.block_size)

    if amount_to_pad != AES.block_size:

        text = text +'\0'.encode() * amount_to_pad

    return base64.b64encode(AES.new(AES_KEY, AES.MODE_ECB).encrypt(text)).decode()


def aes_decrypt(text):

    plain_text = AES.new(AES_KEY, AES.MODE_ECB).decrypt(base64.b64decode(text.encode()))

    return bytes.decode(plain_text).rstrip('\0')


参考

https://www.jb51.net/article/196942.htm

你可能感兴趣的:(python实现AES加解密)