0

I'm not able to extract a password protected zip file using Python. Here is the snippet that I use:

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')

Here is the error message. As it can be seen that I can successfully encrypt and zip the file using a password, but when I tried to extract it using the same password, it fails! Very strange!

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$

Why is this? Is there anything that I'm doing wrong?

joesan
  • 13,963
  • 27
  • 95
  • 232

2 Answers2

0

I figured out how to do this!

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

Seems like there is a bug with AES encryption and Python ZipFile!

joesan
  • 13,963
  • 27
  • 95
  • 232
  • This is not *Python* per se, it's *shell* commands wrapped in *Python* (wonder why the trouble). A *Python* solution would be to use *PKWARE* passwords but it has some limitations as well: https://stackoverflow.com/questions/54532010/zipfile-badzipfile-bad-crc-32-when-extracting-a-password-protected-zip-zip/55063500#55063500 – CristiFati Mar 20 '19 at 23:53
  • Yes, it is indeed, but it just works for me! I need AES256 encryption and this way of doing it seems to be the only possibility, given the known bug in Python ZipFile which is not compatible with AES256 encrypted passwords! – joesan Mar 21 '19 at 05:03
-2

this may work.

f = zipfile.ZipFile('myarchive.zip').extractall(pwd=bytes(‘P4$$W0rd‘,'utf-8'))
Daj Katal
  • 1
  • 1
  • It would not. Since 'foo' and bytes('foo', 'utf-8') are not the same. And that is not even the problem, to start with – han solo Mar 20 '19 at 16:56
  • 1
    Welcome to Stack Overflow. While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. [How to Answer](https://stackoverflow.com/help/how-to-answer) – Elletlar Mar 20 '19 at 16:56
  • This is what i saw in the documentation for zipfile – Daj Katal Mar 20 '19 at 16:59