基本原理在于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中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

  • 相关标签:python破解压缩包密码
  • 程序员必备接口测试调试工具:点击使用

    Apipost = Postman + Swagger + Mock + Jmeter

    Api设计、调试、文档、自动化测试工具

    网页生成APP,用做网站的技术去做APP:立即创建

    手机网站开发APP、自助封装APP、200+原生模块、2000+映射JS接口按需打包

    • 上一篇:二进制数1001001转换成十进制数等于多少
    • 下一篇:python怎么查看安装的模块有哪些

    相关文章

    相关视频


    • python普通人能学吗
    • python语言难学吗
    • 高中要上python吗
    • python基础教程适合初学者吗
    • python怎么读取图片大小
    • python怎么破解压缩包密码
    • 如何使用Selenium以及Python轻松抓取Agoda的旅馆信息
    • 探索Facebook隐藏的秘密使用Python存取Facebook信息
    • Python 中的 Web 服务器
    • MySQL Python 客户端的使用

    视频教程分类

    • php视频教程
    • html视频教程
    • css视频教程
    • JS视频教程
    • jQuery视频教程
    • mysql视频教程
    • Linux视频教程
    • Python视频教程
    • Laravel视频教程
    • Vue视频教程

    专题

    python怎么破解压缩包密码