danifus/pyzipper

GitHub: danifus/pyzipper

Python zipfile 模块的扩展替代库,提供 AES 加密 zip 文件的读写能力。

Stars: 143 | Forks: 24

# pyzipper Python 的 ``zipfile`` 替代品,能够读取和写入 AES 加密的 zip 文件。它 Fork 自 Python 3.7 的 ``zipfile`` 模块,具有当时相同的 ``zipfile`` API(最显著的是,不支持 Python 3.8 中引入的兼容 ``pathlib`` 的 封装)。 ## 安装 .. code-block:: bash pip install pyzipper ## 用法 .. code-block:: python import pyzipper secret_password = b'lost art of keeping a secret' with pyzipper.AESZipFile('new_test.zip', 'w', compression=pyzipper.ZIP_LZMA, encryption=pyzipper.WZ_AES) as zf: zf.setpassword(secret_password) zf.writestr('test.txt', "What ever you do, don't tell anyone!") with pyzipper.AESZipFile('new_test.zip') as zf: zf.setpassword(secret_password) my_secrets = zf.read('test.txt') ## CRC32 值 从 pyzipper v0.4.0 版本开始,默认情况下文件头中不包含 CRC32 值。 在 v0.4.0 (2026-05-13) 之前,一个 bug 导致小文件(未压缩时 <20 字节)中错误地包含了 CRC32 值。 以下是 `WZ AES FAQ`_ 中关于 CRC 值的说明: ``` Within the Zip format, the primary use of the CRC value is to detect accidental corruption of data that has been stored in the Zip file. With files encrypted according to the Zip 2.0 encryption specification, it also functions to some extent as a method of detecting deliberate attempts to modify the encrypted data, but not one that can be considered cryptographically strong. The CRC is not needed for these purposes with the WinZip AES encryption specification, where the HMAC-SHA1-based authentication code instead serves these roles. The CRC has a drawback in that for very small files, such as files with four or fewer bytes, the CRC can be used, independent of the encryption algorithm, to determine the unencrypted contents of the file. And, in general, it is preferable to store as little information as possible about the encrypted file in the unencrypted Zip headers. The CRC does serve one purpose that the authentication code does not. The CRC is computed based on the original uncompressed, unencrypted contents of the file, and it is checked after the file has been decrypted and decompressed. In contrast, the authentication code used with WinZip AES encryption is computed after compression/encryption and it is checked before decryption/decompression. In the very rare event of a hardware or software error that corrupts data during compression and encryption, or during decryption and decompression, the CRC will catch the error, but the authentication code will not. ``` 您可以使用以下代码,有条件地为至少包含 ``min_bytes_to_include_crc`` 未压缩字节的文件启用 CRC32 值包含功能: .. code-block:: python with pyzipper.AESZipFile( 'new_test.zip', 'w', compression=pyzipper.ZIP_LZMA, ) as zf: zf.setencryption( pyzipper.WZ_AES, conditionally_include_crc=True, min_bytes_to_include_crc=, ) ... ``min_bytes_to_include_crc`` 可设置的最小字节数为 ``20``,如 `WZ AES FAQ`_ 中所述。 ## AES 强度 AES 加密的强度可以配置为 128、192 或 256 位。 默认为 256 位。使用 ``setencryption()`` 方法指定加密 kwargs: .. code-block:: python import pyzipper secret_password = b'lost art of keeping a secret' with pyzipper.AESZipFile('new_test.zip', 'w', compression=pyzipper.ZIP_LZMA) as zf: zf.setpassword(secret_password) zf.setencryption(pyzipper.WZ_AES, nbits=256) zf.writestr('test.txt', "What ever you do, don't tell anyone!") with pyzipper.AESZipFile('new_test.zip') as zf: zf.setpassword(secret_password) my_secrets = zf.read('test.txt') ## 文档 官方 Python ZipFile 文档可在此处获取:https://docs.python.org/3/library/zipfile.html ## 致谢 文档骨架是使用 Cookiecutter_ 和 `audreyr/cookiecutter-pypackage`_ 项目模板创建的。 .. _Cookiecutter: https://github.com/audreyr/cookiecutter .. _`audreyr/cookiecutter-pypackage`: https://github.com/audreyr/cookiecutter-pypackage .. _`WZ AES FAQ`: https://www.winzip.com/en/support/aes-encryption/#crc-faq
标签:AES加密, Python, 压缩解压, 开发工具库, 文件处理, 无后门, 逆向工具