在将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部分中,有什么主意吗?
答案 0 :(得分: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()