Python编程

Lesson I 解rar压缩包的密码

1 下载Python并安装

网址: 注意选对是32 bit还是64 bit

Python Releases for Windows | Python.orgThe official home of the Python Programming Languagehttps://www.python.org/downloads/windows/ 2 安装unrar

python 复制代码
pip install unrar

3 下载unrar的Library

RARLab官方下载库文件 下载地址: http://www.rarlab.com/rar/UnRARDLL.exe

4 写解密的Python代码

python 复制代码
from unrar import rarfile
import os
 
import itertools as its
import time
 
from multiprocessing import Pool
import queue
import threading
 
 
def get_pwd(file_path, output_path, pwd):
    '''
    判断密码是否正确
    :param file_path: 需要破解的文件路径,这里仅对单个文件进行破解
    :param output_path: 解压输出文件路径
    :param pwd: 传入的密码
    :return:
    '''
    try:
        # 传入被解压的文件路径,生成待解压文件对象
        file = rarfile.RarFile(file_path, pwd=pwd)
        # 输出解压后的文件路径
        out_put_file_path = output_path
        # print(file_path,output_path)
        
        file.extractall(output_path)
        # 如果发现文件被解压处理,移除该文件
        # os.remove(out_put_file_path)
        # 说明当前密码有效,并告知
        print('Find password is "{}"'.format(pwd))
 
        return True,pwd
    except Exception as e:
        # 密码不正确
       # print('"{}" is not correct password!'.format(pwd))
        # print(e)
 
        return False,pwd
 
 
def get_password(min_digits, max_digits, words):
    """
    密码生成器
    :param min_digits: 密码最小长度
    :param max_digits: 密码最大长度
    :param words: 密码可能涉及的字符
    :return: 密码生成器
    """
    while min_digits <= max_digits:
        pwds = its.product(words, repeat=min_digits)
        for pwd in pwds:
            yield ''.join(pwd)
        min_digits += 1
 
 
 
if __name__=="__main__":
 
 
    file_path = 'C:\TEMP\python\python.rar'
    output_path = 'C:\TEMP\python'
 
    # 密码范围
    # words = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'  # 涉及到生成密码的参数
    words = '0138'
    pwds = get_password(4, 4, words)
 
    # 开始查找密码
    start = time.time()
 
    # judge = []
    result=queue.Queue(maxsize=10) #队列
    pool = Pool()
    def pool_th():
        while True: ##这里需要创建执行的子进程非常多
            pwd = next(pwds)
            try:
                result.put(pool.apply_async(get_pwd, args=(file_path, output_path, pwd)))
            except:
                break
    def result_th():
        while True:
            #pwd = next(pwds)
            a=result.get().get() #获取子进程返回值
            print(a)
            if a[0]:
                #print(pwd)
                pool.terminate() #结束所有子进程
                break
    '''
    利用多线程,同时运行Pool函数创建执行子进程,以及运行获取子进程返回值函数。
    '''
    t1=threading.Thread(target=pool_th)
    t2=threading.Thread(target=result_th)
    t1.start()
    t2.start()
    t1.join()
    t2.join()
    pool.join()
 
    end = time.time()
    print('程序耗时{}'.format(end - start))

5 运行代码

会出现找不到库的错误:Couldn't find path to unrar library

修改 C:\python\Lib\site-packages\unrar\unrarlib.py

python 复制代码
if platform.system() == 'Windows':
    from ctypes.wintypes import HANDLE as WIN_HANDLE
    HANDLE = WIN_HANDLE
    UNRARCALLBACK = ctypes.WINFUNCTYPE(ctypes.c_int, ctypes.c_uint,
                                       ctypes.c_long, ctypes.c_long,
                                       ctypes.c_long)
    lib_path = lib_path or find_library("unrar.dll")
    if lib_path:
        unrarlib = ctypes.WinDLL(lib_path)
    unrarlib = ctypes.WinDLL("C:\\Program Files (x86)\\UnrarDLL\\x64\\unrar.dll")

手工修改unrarlib的路径。

6 如果要把代码迁移到其他机器上,需要准备好Python安装文件( Win7版本- python-3.8.10-amd64.exe;Win10版本- python-3.11.4-amd64.exe) 和unrarDLL.exe

然后安装Python和unrar,使用pip安装unrar模块的时候会报错,因为无法连外网。

此时需要把源机器的 C:\python\Lib\site-packages\unrar 和 C:\python\Lib\site-packages\unrar-0.4.dist-info 两个文件夹考到目标机器里。

相关推荐
databook2 小时前
Manim实现闪光轨迹特效
后端·python·动效
Juchecar3 小时前
解惑:NumPy 中 ndarray.ndim 到底是什么?
python
用户8356290780513 小时前
Python 删除 Excel 工作表中的空白行列
后端·python
Json_3 小时前
使用python-fastApi框架开发一个学校宿舍管理系统-前后端分离项目
后端·python·fastapi
数据智能老司机10 小时前
精通 Python 设计模式——分布式系统模式
python·设计模式·架构
数据智能老司机11 小时前
精通 Python 设计模式——并发与异步模式
python·设计模式·编程语言
数据智能老司机11 小时前
精通 Python 设计模式——测试模式
python·设计模式·架构
数据智能老司机11 小时前
精通 Python 设计模式——性能模式
python·设计模式·架构
c8i11 小时前
drf初步梳理
python·django
每日AI新事件11 小时前
python的异步函数
python