【发布时间】:2021-11-02 17:59:22
【问题描述】:
我正在编写自己的解压缩代码,并且(通过反复试验,无法理解)看起来解密所需的一个字节上的 CRC-32 算法与 zlib 的不太匹配。从一种转换为另一种:
def crc32(ch, crc):
crc = zlib.crc32(bytes([~ch & 0xFF]), crc)
return (~crc & 0xFF000000) + (crc & 0x00FFFFFF)
这是为什么? (/我错了吗?)
编辑:我认为至少有可能我是对的的原因,在https://github.com/uktrade/stream-unzip/blob/d23400028abbe3b0d7e1951cb562cd0541bfc960/stream_unzip.py#L89我使用上面成功解密了加密的ZIP文件
def decrypt(chunks):
key_0 = 305419896
key_1 = 591751049
key_2 = 878082192
def crc32(ch, crc):
crc = zlib.crc32(bytes([~ch & 0xFF]), crc)
return (~crc & 0xFF000000) + (crc & 0x00FFFFFF)
def update_keys(byte):
nonlocal key_0, key_1, key_2
key_0 = crc32(byte, key_0)
key_1 = (key_1 + (key_0 & 0xFF)) & 0xFFFFFFFF
key_1 = ((key_1 * 134775813) + 1) & 0xFFFFFFFF
key_2 = crc32(key_1 >> 24, key_2)
def decrypt(chunk):
chunk = bytearray(chunk)
for i, byte in enumerate(chunk):
temp = key_2 | 2
byte ^= ((temp * (temp ^ 1)) >> 8) & 0xFF
update_keys(byte)
chunk[i] = byte
return chunk
yield_all, _, get_num, _ = get_byte_readers(chunks)
for byte in password:
update_keys(byte)
if decrypt(get_num(12))[11] != mod_time >> 8:
raise ValueError('Incorrect password')
for chunk in yield_all():
yield decrypt(chunk)
但是,如果我将上面的 crc32 函数替换为仅调用 zlib 的函数,则不会(例如,它会抱怨密码错误)
【问题讨论】:
-
@MarkAdler 够公平的!但是,我添加了更多细节来说明我为什么这么认为。
标签: python zip zlib deflate crc32