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

相关推荐
钢铁男儿43 分钟前
C# 方法(栈帧)
开发语言·c#
Amo Xiang2 小时前
《100天精通Python——基础篇 2025 第18天:正则表达式入门实战,解锁字符串处理的魔法力量》
python·正则表达式·re
忆源3 小时前
【Qt】之音视频编程1:QtAV的背景和安装篇
开发语言·qt·音视频
敲键盘的小夜猫3 小时前
Python核心数据类型全解析:字符串、列表、元组、字典与集合
开发语言·python
李匠20243 小时前
C++GO语言微服务之图片、短信验证码生成及存储
开发语言·c++·微服务·golang
apcipot_rain4 小时前
【应用密码学】实验五 公钥密码2——ECC
前端·数据库·python
小彭律师4 小时前
门禁人脸识别系统详细技术文档
笔记·python
鸿业远图科技5 小时前
分式注记种表达方式arcgis
python·arcgis
别让别人觉得你做不到6 小时前
Python(1) 做一个随机数的游戏
python