[英]How to strip password encryption from zipfile with Python
I have an use case where I need to strip decryption of password protected zip file attached to an email, and replace it with the same zip file unencrypted. 我有一个用例,其中我需要剥离对电子邮件附带的受密码保护的zip文件的解密,然后将其替换为未加密的相同zip文件。 What I have so far:
到目前为止,我有:
import zipfile
import StringIO
...
if part.get_content_type() == "application/zip":
encrypted_string = part.get_payload().decode("base64")
encrypted_zip = zipfile.ZipFile(StringIO(encrypted_string))
encrypted_zip.setpassword("password")
I know that the zip file is now decrypted as I can do encrypted_zip.namelist()
and it works. 我知道该zip文件现在已解密,因为我可以进行
encrypted_zip.namelist()
并且它可以工作。 Now that I have regular zip in the var encrypted_zip, I would just like to base64 encode it and replace the payload of the current attachment and move forward to next attachment. 现在,我在var encryption_zip中有了常规的zip,现在我想对它进行base64编码,并替换当前附件的有效负载,然后移至下一个附件。 However, ZipFile does not have ".to_string()" method which i could use to re-encode it.
但是,ZipFile没有“ .to_string()”方法,我可以使用该方法对其进行重新编码。
How do I achieve this? 我该如何实现?
You can create a temporary archive to get rid of the password : 您可以创建一个临时档案来删除密码:
import zipfile
import StringIO
path = "dev.zip"
encrypted_zip = zipfile.ZipFile( path )
encrypted_zip.setpassword("pass")
print encrypted_zip.namelist()
with zipfile.ZipFile('spam.zip', 'w') as myzip:
for nested_file in encrypted_zip.namelist():
myzip.write(encrypted_zip.read(nested_file))
The script copy files for the password-protected archive 'dev.zip' into the uncrypted archive 'spam.zip'. 该脚本将受密码保护的档案“ dev.zip”的文件复制到未加密的档案“ spam.zip”中。 ( do not forget to destroy the archive after).
(不要忘了销毁之后的档案)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.