Python - the zipfile module doesn't seem to work with passwords

前端 未结 2 2018
庸人自扰
庸人自扰 2020-12-11 16:20

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\"

相关标签:
2条回答
  • 2020-12-11 16:38

    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

    0 讨论(0)
  • 2020-12-11 16:53

    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

    0 讨论(0)
提交回复
热议问题