无法使用Gzip解压缩DES3加密的数据

问题描述 投票:0回答:2

[将python2代码移植到python3时,我们在压缩DES3加密流的代码的gzip部分中遇到了一个问题。

以下是用于在DES3中加密数据然后写入gzip文件的代码:

def createEncryptedFile(key, enc_iv, path):
    checksum_generation = 'ciphertext'
    tmp_fd = open(path, 'w')
    encrypted_stream = utils.DES3Cipher(tmp_fd, key, enc_iv, checksum_generation)
    with gzip.GzipFile(fileobj=encrypted_stream, mode='w') as fo:
        fo.write(bytes('Testing Data For Gzip', 'latin-1'))
    encrypted_stream.close()
    tmp_fd.close()

下面是解密和解压缩内容的代码:

def decryptFile(key, enc_iv, path):
    update_size = os.path.getsize(path)
    with open(path, 'r') as update_file:
        decrypted_data = ''.join(utils.decrypt_des3_cbc_stream(update_file, update_size, key, enc_iv))
        inner_data = io.BytesIO(decrypted_data.encode('latin-1'))
        with gzip.GzipFile(fileobj=inner_data, mode='rb') as fo:
            print("The unzipped data: ", fo.read())

我遇到以下错误:

  print("The unzipped data: ", fo.read())
  File "/usr/lib64/python3.7/gzip.py", line 276, in read
    return self._buffer.read(size)
  File "/usr/lib64/python3.7/gzip.py", line 463, in read
    if not self._read_gzip_header():
  File "/usr/lib64/python3.7/gzip.py", line 411, in _read_gzip_header
    raise OSError('Not a gzipped file (%r)' % magic)
OSError: Not a gzipped file (b'\x08\x08')

我已经分别测试了DES3加密/解密方法,并排除了在那里出错的可能性。

错误似乎在gzip部分中,有什么主意吗?

python python-3.x python-3.7
2个回答
0
投票

您的open调用应处于二进制模式,即open(path, "rb")open(path, "wb"),有一种可能是Python在文本模式下错误地解码了文件。

        decrypted_data = ''.join(utils.decrypt_des3_cbc_stream(update_file, update_size, key, enc_iv))
        inner_data = io.BytesIO(decrypted_data.encode('latin-1'))

这看起来也不正确,我不确定您如何解密utils.decrypt_des3_cbc_stream中的流,但是它应该返回bytes而不是str


0
投票

此解决方案对我们有用:

import Crypto.Cipher.DES3
import random
import hexdump
import gzip
import updater.utils
import io as StringIO

input_string = b"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."

key = ''.join([chr(random.randrange(255)) for x in range(0, 24)]).encode('latin-1')
iv = ''.join([chr(random.randrange(255)) for x in range(0, 8)]).encode('latin-1')

bytes_obj = StringIO.BytesIO()
encrypted_stream = updater.utils.DES3Cipher(bytes_obj, key, iv)
encrypted_stream.write(input_string)
encrypted_stream.close()

with gzip.open('./des3__new_file', 'wb') as f:
    bytes_obj.seek(0)
    f.write(bytes_obj.read())

new_bytes_obj = StringIO.BytesIO()

with gzip.open('./des3__new_file', 'rb') as f:
    new_bytes_obj.write(f.read())
    new_bytes_obj.seek(0)
    decrypted_data = ''.join(updater.utils.decrypt_des3_cbc_stream(new_bytes_obj, new_bytes_obj.getbuffer().nbytes, key, iv))
    new_bytes_obj.close()
© www.soinside.com 2019 - 2024. All rights reserved.