如何在应用引擎的Blobstore上加密Zip文件

1 投票
1 回答
683 浏览
提问于 2025-04-17 02:22

我遇到了一个问题,想要对压缩文件(zip格式)进行加密。由于一些原因,我不能使用chilkat模块,而zipfile模块只提供解密功能,所以我不知道该用什么来用密钥加密zip文件。

你能给我一些建议,告诉我这个问题应该怎么解决吗?

理想的解决方案大概是这样的:

blob_info = blobstore.BlobInfo.all()[0] #lets say we want to read the first blob we find
blob_reader = blobstore.BlobReader(blob_info.key())

file = zipfile.ZipFile(blob_reader, 'r')
data = file.read(file.namelist()[0])

output = StringIO.StringIO()
outfile = zipfile.ZipFile(output, "w")

outfile.writestr(file.namelist()[0], data)
outfile.setpassword('testpass') #it would be nice if there was a module that could set pass like this, .setpassword() only works with decryption

outfile.close()

outputStream = files.blobstore.create(mime_type='application/zip', _blobinfo_uploaded_filename = file.namelist()[0].split('.')[0] + '.zip')
with files.open(outputStream, 'a') as f:
    f.write(output.getvalue())
files.finalize(outputStream)

1 个回答

1

首先,我想说的是,zip加密技术很弱,已经过时了。如果你需要强大的安全性,别指望它。这在很多论文中都有证明(谷歌上最受欢迎的论文是Eli Biham和Paul C. Kocher写的《对PKZIP流密码的已知明文攻击》)。

其次,GAE只支持纯Python的库。可能你不能使用chilkat,因为它是一个C语言写的库。

第三,使用纯Python进行zip文件的加密和解密会非常慢,而且在GAE上可能会遇到CPU性能问题……

也许你应该考虑其他的方法来解决这个问题?

祝好

撰写回答