Невозможно распаковать зашифрованные данные DES3 с помощью Gzip - PullRequest
0 голосов
/ 08 мая 2020

При переносе кода python2 на python3 мы сталкиваемся с проблемой в разделе gzip кода, который сжимает зашифрованный поток DES3.

Ниже приведен код для шифрования данных в 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, есть идеи?

Ответы [ 2 ]

0 голосов
/ 16 мая 2020

Это решение сработало для нас:

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()
0 голосов
/ 08 мая 2020

Ваши open вызовы должны быть в двоичном режиме ie open(path, "rb") или open(path, "wb"), есть вероятность, что Pythons неправильно декодирует файлы в текстовом режиме.

        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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...