求助 python des 和zeropadding (已经解决)

DES加密模式ECB,填充:zeropadding ,密码:11111111,偏移量:11111111,输出:base64
加密文本:12345678b46878af-bdc6-4b4a-b7b7-5bcd1a1348f0
加密结果:hYsXbaixJQPyZCgkBoXVPlZDzcULnBvxjLhkvfTP9kNVjnZ0YwfgKCc/V0C5pIL3

加密网站:tool.chacuo.net/cryptdes

我自己写出来的位数最后几位总是不对。
自写结果: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)
pardon110
最佳答案

加密位数不足填充时,要用转义字符 \0 来示意空字符(NULL)。 你直接 chr(add), 只是将数字转为unicode串,而非达成位数不足补空,所以后续都不对了。

def pad(self,text): 
   .
   entext = text + '\0' * add
   ....
3年前 评论
guishensxy (楼主) 3年前
讨论数量: 3
Jason990420

您确定问题本身包含足够的信息?

您的问题不包含代码,您确定不应该包含代码吗?


``` python
# 把你的代码放在这里
```
3年前 评论
guishensxy (楼主) 3年前
Jason990420 (作者) 3年前
pardon110

加密位数不足填充时,要用转义字符 \0 来示意空字符(NULL)。 你直接 chr(add), 只是将数字转为unicode串,而非达成位数不足补空,所以后续都不对了。

def pad(self,text): 
   .
   entext = text + '\0' * add
   ....
3年前 评论
guishensxy (楼主) 3年前
Jason990420

问题是你的填充函数不是zeropadding, 而是pkcs5padding/pkcs7padding

PKCS#5 and PKCS#7

Padding is in whole bytes. The value of each added byte is the number of bytes that are added, i.e. N bytes, each of value N are added. The number of bytes added will depend on the block boundary to which the message needs to be extended.

    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

Zero padding

All the bytes that are required to be padded are padded with zero. The zero padding scheme has not been standardized for encryption,[citation needed] although it is specified for hashes and MACs as Padding Method 1 in ISO/IEC 10118-1[9] and ISO/IEC 9797-1.

3年前 评论
guishensxy (楼主) 3年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!