繁体   English   中英

无法在Python中提取加密的Zip文件

[英]Unable to Extract Encrypted Zip File in Python

我无法使用Python提取受密码保护的zip文件。 这是我使用的代码段:

import os
import subprocess
import zipfile
import sys

# Step 1: Encrypt the file using AES256
rc = subprocess.call(['/usr/local/Cellar/p7zip/16.02_1/bin/7z', 'a', '-mem=AES256', '-pP4$$W0rd', '-y', 'myarchive.zip'] + 
                    ['/Users/joe/Projects/Sandbox/python-projects/aaa.txt', '/Users/joe/Projects/Sandbox/python-projects/bbb.txt'])

# Step 2: Decrypt the archive
f = zipfile.ZipFile('myarchive.zip').extractall(pwd='P4$$W0rd')

这是错误消息。 可以看出,我可以使用密码成功加密和压缩文件,但是当我尝试使用相同的密码提取文件时,它失败了! 很奇怪!

Joes-MacBook-Pro:python-projects joe$ python ./test.py

7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=utf8,Utf16=on,HugeFiles=on,64 bits,8 CPUs x64)

Scanning the drive:
2 files, 245 bytes (1 KiB)

Creating archive: myarchive.zip

Items to compress: 2


Files read from disk: 2
Archive size: 534 bytes (1 KiB)
Everything is Ok
Traceback (most recent call last):
  File "./test.py", line 15, in <module>
    f = zipfile.ZipFile('myarchive.zip').extractall(pwd='P4$$W0rd')
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/zipfile.py", line 1040, in extractall
    self.extract(zipinfo, path, pwd)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/zipfile.py", line 1028, in extract
    return self._extract_member(member, path, pwd)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/zipfile.py", line 1082, in _extract_member
    with self.open(member, pwd=pwd) as source, \
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/zipfile.py", line 1007, in open
    raise RuntimeError("Bad password for file", name)
RuntimeError: ('Bad password for file', <zipfile.ZipInfo object at 0x106354438>)
Joes-MacBook-Pro:python-projects joe$

为什么是这样? 我做错了什么吗?

我想出了怎么做!

subprocess.call(["/usr/local/Cellar/p7zip/16.02_1/bin/7z", "x", '-p{}'.format(passwd), "myarchive.zip"])

似乎AES加密和Python ZipFile存在错误!

这可能有效。

f = zipfile.ZipFile('myarchive.zip').extractall(pwd=bytes(‘P4$$W0rd‘,'utf-8'))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM