pythonencryptionpassword-protectionzipwinzip

How to open WinZip password protected archive?


I know decryption is an expansive world which I'm a novice in, but I have a WinZip file I can open in WinZip with the known password easily through the WinZip UI.

However, supplying the same password, I cannot open it in Python. I suspect the encoding is possibly in AES, but I don't want to involve a non-native library to open the file. Is there any standard to open password protected WinZip files in Python? I've tried the different codec's of encoding in ZipFile.

from zipfile import ZipFile

with ZipFile(r'C:\Users\user\Desktop\Data.zip') as zf:
    pas = 'myPass'
    res = pas.encode('utf-32-le')
    zf.extractall(pwd=res)
zf
RuntimeError: Bad password for file ...

Solution

  • You first need to determine what type of encryption the zip file is using. There may be a reporting tool in WinZip itself that will tell you. I don't have it, so don't know.

    If you have access to any of the command line zip utilities you can find out quite easily.

    Firstly if you have the Infozip implementation of unzip available, run it with the -lv option. If you have a very new version of unzip available and it displays AES_WG in the Method column, your file is AES encrypted.

    $ unzip -lv my.zip
    Archive:  /home/paul/perl/ext/Gzip/IO-Zippo/scratch/sample-zip/7z/7z-win32-aes128.zip
     Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
    --------  ------  ------- ---- ---------- ----- --------  ----
        1933  AES_WG      884  54% 04-15-2010 22:26 00000000  0001-perl-74088.patch
    --------          -------  ---                            -------
        1933              884  54%                            1 file
    

    If your unzip is older, the presence of the string Unk:099 in the Method column means your file is AES encrypted (but the version of unzip you have doesn't support unzipping it).

    $ unzip -lv my.zip
    Archive:  IO-Zippo/scratch/sample-zip/7z/7z-win32-aes128.zip
     Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
    --------  ------  ------- ---- ---------- ----- --------  ----
        1933  Unk:099     884  54% 2010-04-15 22:26 00000000  0001-perl-74088.patch
    --------          -------  ---                            -------
        1933              884  54%                            1 file
    

    Another alternative is to use zipdetails (full disclosure, I'm the author of zipdetails). The key thing to look for is the line Compression Method 0063 'AES Encryption'

    $ zipdetails my.zip
    
    0000 LOCAL HEADER #1       04034B50
    0004 Extract Zip Spec      33 '5.1'
    0005 Extract OS            00 'MS-DOS'
    0006 General Purpose Flag  0001
         [Bit  0]              1 'Encryption'
    0008 Compression Method    0063 'AES Encryption'
    ...
    

    If it turns out you do have AES encryption and you need a python way to read the file, the standard zipfile approach will not work. As things stand zipfile only supports weak encryption.

    For other python ways to read AES-encrypted Zip files see Python unzip AES-128 encrypted file