美文网首页
python实现AES加解密

python实现AES加解密

作者: 明明就_c565 | 来源:发表于2022-12-14 18:17 被阅读0次

代码

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加解密

      本文链接:https://www.haomeiwen.com/subject/mtzkqdtx.html