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 两个文件夹考到目标机器里。

相关推荐
陈天伟教授4 小时前
人工智能训练师认证教程(2)Python os入门教程
前端·数据库·python
陈文锦丫4 小时前
MQ的学习
java·开发语言
2301_764441334 小时前
Aella Science Dataset Explorer 部署教程笔记
笔记·python·全文检索
爱笑的眼睛114 小时前
GraphQL:从数据查询到应用架构的范式演进
java·人工智能·python·ai
BoBoZz194 小时前
ExtractSelection 选择和提取数据集中的特定点,以及如何反转该选择
python·vtk·图形渲染·图形处理
liwulin05064 小时前
【PYTHON-YOLOV8N】如何自定义数据集
开发语言·python·yolo
青蛙大侠公主5 小时前
Thread及其相关类
java·开发语言
爱吃大芒果5 小时前
Flutter 主题与深色模式:全局样式统一与动态切换
开发语言·javascript·flutter·ecmascript·gitcode
云栖梦泽5 小时前
易语言数据库操作:结构化数据管理的核心
开发语言
木头左5 小时前
LSTM量化交易策略中时间序列预测的关键输入参数分析与Python实现
人工智能·python·lstm