总结
豆包 AI 助手文章总结

python怎么破解压缩包密码

发布: 2019-07-10 16:57:41
原创
8297人浏览过

python怎么破解压缩包密码

基本原理在于Python标准库zipfile和扩展库unrar提供的解压缩方法extractall()可以指定密码,这样的话首先(手动或用程序)生成一个字典,然后依次尝试其中的密码,如果能够正常解压缩则表示密码正确。

import os
import sys
#zipfile是Python标准库
import zipfile
#尝试导入扩展库unrar,如果没有就临时安装
try:
    from unrar import rarfile
except:
    path = '"'+os.path.dirname(sys.executable)+'\scripts\pip" install --upgrade pip'
    os.system(path)
    path = '"'+os.path.dirname(sys.executable)+'\scripts\pip" install unrar'
    os.system(path)
    from unrar import rarfile

def decryptRarZipFile(filename):
    #根据文件扩展名,使用不同的库
    if filename.endswith('.zip'):
        fp = zipfile.ZipFile(filename)
    elif filename.endswith('.rar'):
        fp = rarfile.RarFile(filename)
    #解压缩的目标文件夹
    desPath = filename[:-4]
    if not os.path.exists(desPath):
        os.mkdir(desPath)
    #先尝试不用密码解压缩,如果成功则表示压缩文件没有密码
    try:
        fp.extractall(desPath)
        fp.close()
        print('No password')
        return
    #使用密码字典进行暴力破解
    except:
        try:
            fpPwd = open('pwddict.txt')
        except:
            print('No dict file pwddict.txt in current directory.')
            return
        for pwd in fpPwd:
            pwd = pwd.rstrip()
            try:
                if filename.endswith('.zip'):
                    for file in fp.namelist():
                        #对zip文件需要重新编码再解码,避免中文乱码
                        fp.extract(file, path=desPath, pwd=pwd.encode())
                        os.rename(desPath+'\'+file, desPath+'\'+file.encode('cp437').decode('gbk'))
                    print('Success! ====>'+pwd)
                    fp.close()
                    break
                elif filename.endswith('.rar'):
                    fp.extractall(path=desPath, pwd=pwd)
                    print('Success! ====>'+pwd)
                    fp.close()
                    break
            except:
                pass
        fpPwd.close()

if __name__ == '__main__':
    filename = sys.argv[1]
    if os.path.isfile(filename) and filename.endswith(('.zip', '.rar')):
        decryptRarZipFile(filename)
    else:
        print('Must be Rar or Zip file')
登录后复制

更多Python相关技术文章,请访问Python教程栏目进行学习!

以上就是python怎么破解压缩包密码的详细内容,更多请关注php中文网其它相关文章!

python速学教程(入门到精通)
python速学教程(入门到精通)

python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
豆包 AI 助手文章总结
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号