11

I've been trying to implement a very simple script, extracting zip files that are password protected. I have created a simple zip file (test.zip) with the password "1234" containing 2 text files (1.txt, 2.txt) and i wrote this script:

import zipfile

PASSWORD = "1234"

zip = zipfile.ZipFile("test.zip", "r")
zip.setpassword(PASSWORD)
zip.extractall()
zip.close()

and i'm getting the following Runtime error:

Traceback (most recent call last):
  File "test.py", line 7, in <module>
    zip.extractall()
  File "/usr/lib/python2.7/zipfile.py", line 962, in extractall
    self.extract(zipinfo, path, pwd)
  File "/usr/lib/python2.7/zipfile.py", line 950, in extract
    return self._extract_member(member, path, pwd)
  File "/usr/lib/python2.7/zipfile.py", line 993, in _extract_member
    source = self.open(member, pwd=pwd)
  File "/usr/lib/python2.7/zipfile.py", line 934, in open
    raise RuntimeError("Bad password for file", name)
RuntimeError: ('Bad password for file', <zipfile.ZipInfo object at 0x1f3f2a8>)

I've tried iterating using "zip.namelist()" and the "extract()" method + specifying the exact parameters as follows:

zip.extract(<file_name>, path=<path>, pwd=<password>)

with no luck :( I know about the security issue with "extractall()" and in my complete code i will have verification prior to the extracting process, i'm just trying to figure out what am i doing wrong?

Thanks for the help in advance!

Blaland
  • 111
  • 1
  • 1
  • 3
  • I just copied your code and tried it myself. It worked fine for me as is. Since the traceback says you have a bad password, you might want to check if you really zipped the files with 1234 – joel goldstick Sep 20 '11 at 10:07
  • at least i can confirm Blaland's problem also occurred in my environment... – Dyno Fu Sep 20 '11 at 10:25

2 Answers2

12

As indicated in a comment it could be a problem with your encryption mode. Using 7-zip to create the zip file using AES-256 I get the same error as yours. With ZypCrypto encryption it works OK.

PyCrust 0.9.8 - The Flakiest Python Shell
Python 2.6.7 (r267:88850, Jun 27 2011, 13:20:48) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>> import zipfile
>>> psw = "abcd"

#with zipcrypto encryption

>>> path= "C:/Users/joaquin/Desktop/zipcrypto.zip"
>>> zip = zipfile.ZipFile(path, "r")
>>> zip.setpassword(psw)
>>> zip.extractall("C:/Python26")
>>> zip.close()

#with AES-256 encryption

>>> path= "C:/Users/joaquin/Desktop/aes256.zip"
>>> zip = zipfile.ZipFile(path, "r")
>>> zip.setpassword(psw)
>>> zip.extractall("C:/Python26")
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Python26\lib\zipfile.py", line 938, in extractall
    self.extract(zipinfo, path, pwd)
  File "C:\Python26\lib\zipfile.py", line 926, in extract
    return self._extract_member(member, path, pwd)
  File "C:\Python26\lib\zipfile.py", line 969, in _extract_member
    source = self.open(member, pwd=pwd)
  File "C:\Python26\lib\zipfile.py", line 901, in open
    raise RuntimeError("Bad password for file", name)
RuntimeError: ('Bad password for file', <zipfile.ZipInfo object at 0x00000000042B3948>)
>>> 

This problem (zipfile only supporting traditional PKWARE encryption method) has been reported as a feature request for python 3.2

joaquin
  • 82,968
  • 29
  • 138
  • 152
  • The feature request is now WONTFIXed. It is unlikely Python will support this in the future. – Kevin Nov 06 '15 at 23:34
6

agree with eryksun & joaquin

7z l -slt test.zip | grep Method

will show you the compress method used.

7z a -p1234 -mem=ZipCrypto test.zip 1.txt 2.txt

will create a python zipfile compatible zip.

7z a -p1234 -mem=AES128 test.zip 1.txt 2.txt

will create a AES encrypted zip.

reference http://docs.bugaco.com/7zip/MANUAL/switches/method.htm

Dyno Fu
  • 8,753
  • 4
  • 39
  • 64