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

前端 未结 2 2025
庸人自扰
庸人自扰 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: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 "", line 1, in 
      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', )
    >>> 
    

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

提交回复
热议问题