[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[备忘]python的aes加密,pkcs7padding

上一篇:[备忘]数据库连接串大全connection string
下一篇:[备忘]git恢复分支

添加日期:2022/7/10 11:08:56 快速返回   返回列表 阅读405次


import base64
from Crypto.Cipher import AES

class AesEcbCrypt():
    def __init__(self, key):
        self.key = key

    def pkcs5padding(self, data):
        return self.pkcs7padding(data, 8)

    def pkcs7padding(self, data, block_size=16):
        if type(data) != bytearray and type(data) != bytes:
            raise TypeError("仅支持 bytearray/bytes 类型!")
        pl = block_size - (len(data) % block_size)
        return data + bytearray([pl for i in range(pl)])
    
    def encrypt(self, data):
        data = self.pkcs7padding(data, 16)
        return AES.new(self.key, AES.MODE_ECB).encrypt(data)

    def decrypt(self, data):
        aes = AES.new(self.key, AES.MODE_ECB)
        decrypt_text = aes.decrypt(data)
        return decrypt_text[:-int(decrypt_text[-1])]

key = "abcdefghij123456".encode('utf-8')
aes = AesEcbCrypt(key)
text = "aaaaaaaa"
en_byte = aes.encrypt(text.encode())
print("密文(HEX):", base64.standard_b64encode(en_byte).decode('utf-8'))
de_byte = aes.decrypt(en_byte)
print("明文:", de_byte.decode())

 

评论 COMMENTS
没有评论 No Comments.

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved