Python如何破解压缩包密码

 更新时间:2022年05月21日 09:29:05   作者:平底锅锅锅  
破解rar和zip压缩包。Windows下使用PyCharm软件,本文给大家详细介绍Python如何破解压缩包密码,感兴趣的朋友一起看看吧

简介:

破解rar和zip压缩包。Windows下使用PyCharm软件。

1.步骤

1.环境

  • 指令pip install 安装。
  • 如果是rar文件需要把rar安装包下的Rar.exe和UnRar.exe,放在对应项目\venv\Scripts的路径下。
  • import失败时,需要在File->Settings->Project Interpreter添加对应的模块。

2.判断文件格式

 type = os.path.splitext(path)[-1][1:]
if type == "zip":
elif type == "rar":

3.判断是否有密码

 type = os.path.splitext(path)[-1][1:]
        if type == "zip":
                fileGet = zipfile.ZipFile(path)
                with fileGet as z:
                    for l in z.infolist():
                        is_encrypted = l.flag_bits & 0x1
                        if is_encrypted:
                            print("have password ")
                            break
                        else:
                            pass
 
        elif type == "rar":
            fileGet = rarfile.RarFile(path)
            with fileGet as z:
                if z.needs_password():
                    print("have password ")
                else:
                    print("no password")
                    return

4.密码字典 自己写或者下载相应的软件生成。

5.解压文件

1.zip和rar

fileGet = zipfile.ZipFile(path)
fileGet = rarfile.RarFile(path)

2.解压

 fileExtr.extractall(pwd=password)

2.代码

import sys
import zipfile
import rarfile
import threading
import datetime
import os
import subprocess
import  getopt
i = 0
fileGet = ""
class MyThread(threading.Thread):
    def __init__(self, func, args, name=''):
        threading.Thread.__init__(self)
        self.name = name
        self.func = func
        self.args = args
        self.result = self.func(*self.args)
    def get_result(self):
        try:
            return self.result
        except Exception:
            return None
def extractFile(fileExtr, password, fileType):
    try:
        encodestr = str.encode(password)
        if (fileType == "zip"):
           fileExtr.extractall(pwd=str.encode(password))
        else:
            fileExtr.extractall(pwd=password)
        global i
        i = i + 1
        print("search count : %d,real password is : %s" % (i, password))
        return password
    except:
        i = i + 1
        print("search count : %d,test password : %s, err:%s" % (i, password, sys.exc_info()[0]))
        pass
def mainStep():
    path = input("please input path:")
    try:
        if os.path.exists(path) == False:
            print("%s : path error!"%(path))
            return
        type = os.path.splitext(path)[-1][1:]
        if type == "zip":
                fileGet = zipfile.ZipFile(path)
                with fileGet as z:
                    for l in z.infolist():
                        is_encrypted = l.flag_bits & 0x1
                        if is_encrypted:
                            print("have password ")
                            break
                        else:
                            pass
                fileGet = zipfile.ZipFile(path)
        elif type == "rar":
            fileGet = rarfile.RarFile(path)
            with fileGet as z:
                if z.needs_password():
                    print("have password ")
                else:
                    print("no password")
                    return
        else:
            print("file not right")
            return
        pwdLists = open("D:\Python工程\mutou.txt")
        startTime = datetime.datetime.now()
        for line in pwdLists.readlines():
            Pwd = line.strip('\n')
            t = MyThread(extractFile, (fileGet, Pwd, type))
            t.start()
            if (t.get_result() is Pwd):
                break
        endTime = datetime.datetime.now()
        timeSpan = endTime - startTime
        print("search time:%ss" % (timeSpan.total_seconds()))
    except:
       print("err:%s" % sys.exc_info()[0])
if __name__ == '__main__':
    mainStep()

1.在线调试

2.脚本运行

  • cmd 窗口打开方式:右键开始菜单,选择‘命令提示符(管理员)’即可。或者从开始菜单->运行->输入cmd,回车。
  • 关于 cd 命令:用于改变当前目录路径。使用方式:cd[空格][路径]。例如 cd d:/Python27/Mytest 转到该路径下。
  • 注意:如果当前盘符不是 D 盘,需要先转到 D 盘,输入 d: 回车即可。然后才可以使用 cd d:/Python27/Mytest 。
  • 输入python test.py。test.py是对应的文件名。

到此这篇关于Python如何破解压缩包密码的文章就介绍到这了,更多相关Python破解压缩包密码内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 基于Python2、Python3中reload()的不同用法介绍

    基于Python2、Python3中reload()的不同用法介绍

    今天小编就为大家分享一篇基于Python2、Python3中reload()的不同用法介绍,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-08-08
  • Python中str.format()详解

    Python中str.format()详解

    本文主要给大家详细介绍的是python编程中str.format()的基本语法和高级用法,非常的详细,并附有示例,希望大家能够喜欢
    2017-03-03
  • 基于Python和openCV实现图像的全景拼接详细步骤

    基于Python和openCV实现图像的全景拼接详细步骤

    这篇文章主要介绍了基于Python和openCV实现图像的全景拼接,本文分步骤通过实例代码给大家介绍的非常详细,需要的朋友可以参考下
    2021-10-10
  • Python日志logging模块功能与用法详解

    Python日志logging模块功能与用法详解

    这篇文章主要介绍了Python日志logging模块功能与用法,结合实例形式详细分析了Python日志logging模块的基本功能、原理、用法及操作注意事项,需要的朋友可以参考下
    2020-04-04
  • python3 实现除法结果为整数

    python3 实现除法结果为整数

    这篇文章主要介绍了python3 实现除法结果为整数,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-03-03
  • Python中的 Numpy 数组形状改变及索引切片

    Python中的 Numpy 数组形状改变及索引切片

    这篇文章主要介绍了Python中的 Numpy 数组形状改变及索引切片,Numpy提供了一个reshape()方法,它可以改变数组的形状,返回一个新的数组,更多相关内容需要的小伙伴可以参考下面文章
    2022-05-05
  • python format格式化和数字格式化

    python format格式化和数字格式化

    这篇文章主要介绍了python format格式化和数字格式化,格式化字符串的函数 str.format(),它增强了字符串格式化的功能,基本语法是通过{} 和 : 来代替以前的 % ,下面内容介绍,需要的朋友可以参考一下
    2022-02-02
  • Python中six模块基础用法

    Python中six模块基础用法

    在本篇文章里小编给大家分享的是关于Python中six模块基础用法以及相关知识点,需要的朋友们学习下。
    2019-12-12
  • python 怎样进行内存管理

    python 怎样进行内存管理

    这篇文章主要介绍了python 是如何进行内存管理的,帮助大家更好的理解和学习python,感兴趣的朋友可以了解下
    2020-11-11
  • python语言变量和数据类型基础学习

    python语言变量和数据类型基础学习

    这篇文章主要为大家介绍了python语言变量和数据类型基础学习,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-10-10

最新评论