栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python实现AES加密解密

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python实现AES加密解密

  pip install pycrypto

import base64
from Crypto.Cipher import AES

BLOCK_SIZE = 16
key = "a4f5a4d8885d48d7"
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * chr(BLOCK_SIZE - len(s) % BLOCK_SIZE)
unpad = lambda s: s[0:-ord(s[-1:])]


def encrypt_aes(sourceStr):
    generator = AES.new(key, AES.MODE_ECB, segment_size=128)
    padded = pad(sourceStr)
    crypt = generator.encrypt(padded)
    cryptedStr = base64.b64encode(crypt)
    return cryptedStr


def decrypt_aes(encrypt_aes):
    unpad = lambda s: s[0:-ord(s[-1:])]
    encrypt_aes = base64.b64decode(encrypt_aes)
    generator = AES.new(key, AES.MODE_ECB, segment_size=128)
    crypt = generator.decrypt(encrypt_aes)
    decryptedStr = unpad(crypt)
    return decryptedStr


source = "1qaz@wsx"
print("密钥:{}".format(key))
print("明文:{}".format(source))
a = encrypt_aes(source)
print("密文:{}".format(a))
b = decrypt_aes(a)
print("解密后:{}".format(b))

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/674592.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号