我在python中使用unpack-in-structure模块,现在得到了一些错误

2024-04-20 10:14:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我现在正在使用python模块struct为我的学校项目制作一个类似ransem的程序,这里有struct.error:unpack需要8字节的缓冲区。我不知道为什么会这样;因为我在Github中修复代码,所以我的计算机无法理解它是如何工作的。但它不起作用。一开始,glob函数有个问题,我解决了,我从来没有碰过这个部分

我不习惯使用struct模块,也不知道如何使用这个函数;所以我尝试只放入integer并删除'infle.read(struct.calcsize('Q'))或插入'<;'在calcsize中的Q前面,或者只放struct.calcsize('Q')。我也读过一些文档,试图理解如何使用它来解决问题,但遗憾的是,我失败了

def decrypt_file(key, in_filename, out_filename = None, chnksize = 64*1024):
    if not out_filename:
        out_filename = os.path.splitext(in_filename)[0]

    with open(in_filename, 'rb') as infile:
        origsize = struct.unpack('<Q', infile.read(struct.calcsize('Q')))[0]
        iv = infile.read(16)
        decryptor = AES.new(key, AES.MODE_CBC, iv)
        #chunksize = 16
        with open(out_filename, 'wb') as outfile:
            while True:
                chunk = infile.read(chunksize)
                if len(chunk) == 0:
                    break
                outfile.write(decryptor.decrypt(chunk))

            outfile.truncate(origsize)

它需要解码编码的文件,首先,它必须计算它的原始大小,但它只吐出错误消息-struct.error:unpack需要一个8字节的缓冲区

它是原始错误消息:

C:\Users\Yeeun\Desktop\for Test\jmtree_44520.jpg .sasya
Decrypting> C:\Users\Yeeun\Desktop\for Test\jmtree_44520.jpg.sasya
Traceback (most recent call last):
  File "C:\Users\Yeeun\Desktop\test\RansomTest.py", line 62, in <module>
    decrypt_file(key, filename)
  File "C:\Users\Yeeun\Desktop\test\RansomTest.py", line 32, in decrypt_file
    origsize = struct.unpack('<Q', infile.read(struct.calcsize('Q')))[0]
struct.error: unpack requires a buffer of 8 bytes

Tags: keyinreaderrorfilenameoutusersstruct