求助 Python DES zeropadding,不会加密
DES加密模式ECB,填充:zeropadding ,密码:11111111,偏移量:11111111,输出:base64加密文本:12345678b46878af-bdc6-4b4a-b7b7-5bcd1a1348f0
加密结果:hYsXbaixJQPyZCgkBoXVPlZDzcULnBvxjLhkvfTP9kNVjnZ0YwfgKCc/V0C5pIL3
加密网站:http://tool.
我自己写出来的位数最后几位总是不对。
自写结果:hYsXbaixJQPyZCgkBoXVPlZDzcULnBvxjLhkvfTP9kNVjnZ0YwfgKIU2f1htDxCF
from Crypto.Cipher import DES
import base64
class EncryptDate:
def __init__(self, key):
self.key = key # 初始化密钥
self.length = DES.block_size # 初始化数据块大小
self.aes = DES.new(self.key, DES.MODE_ECB) # 初始化AES,ECB模式的实例
# 截断函数,去除填充的字符
self.unpad = lambda date: date[0:-ord(date[-1])]
def pad(self, text):
"""
#填充函数,使被加密数据的字节码长度是block_size的整数倍
"""
count = len(text.encode('utf-8'))
print(count)
add = self.length - (count % self.length)
print(add)
entext = text + (chr(add) * add)
print(entext)
return entext
def encrypt(self, encrData): # 加密函数
res = self.aes.encrypt(self.pad(encrData).encode("utf8"))
msg = str(base64.b64encode(res), encoding="utf8")
# msg = res.hex()
return msg
def decrypt(self, decrData): # 解密函数
res = base64.decodebytes(decrData.encode("utf8"))
# res = bytes.fromhex(decrData)
msg = self.aes.decrypt(res).decode("utf8")
return self.unpad(msg)
if __name__ == '__main__':
cr = EncryptDate(b'11111111')
cr_res = cr.encrypt('12345678b46878af-bdc6-4b4a-b7b7-5bcd1a1348f0')
de_res = cr.decrypt(cr_res)
print(cr_res)
print(de_res)
[此贴子已经被作者于2020-12-4 09:51编辑过]