读取Zipped和DES加密文件

时间:2014-10-24 16:44:16

标签: python encryption

我已经发送了一个已经过gzip压缩和DES加密的数据文件。我想解密它,然后解压缩它。

我解密文件的代码如下所示

from Crypto.Cipher import DES

key = 'XXXXXXXX'
mode = DES.MODE_ECB
des = DES.new(key, mode)

input_file = r'C:\Users\UserName\Documents\Somefile.gz.des.20141001'

with open(input_file, 'rb') as file:
    ciphertext = file.read().replace('\n', ' ')

text = des.decrypt(ciphertext)

output_file = r'C:\Users\UserName\Documents\Somefile.gz.20141001'

with open(file_name, 'wb') as file:
    file.write(text)

问题是输出文件似乎没有被正确解密,因为我随后无法解压缩文件;它不被识别为gzip压缩文件。

我已经使用正确的密钥进行了双倍和三重检查。还有什么可能出错?

1 个答案:

答案 0 :(得分:1)

根据您提供的信息,无法说出您的问题或解决方案是什么......我说我会抓住这是您的问题

with open(input_file, 'rb') as file:
    ciphertext = file.read().replace('\n', ' ')
    #I doubt that the original encryptor replaced spaces with newlines after they encrypted it

text = des.decrypt(ciphertext)

您可能想尝试

with open(input_file, 'rb') as file:
    ciphertext = file.read()

text = des.decrypt(ciphertext)

with open(input_file, 'rb') as file:
    ciphertext = file.read()

text = des.decrypt(ciphertext).replace("\n"," ")