Python编程

Lesson I 解rar压缩包的密码

1 下载Python并安装

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

Python Releases for Windows | Python.orgThe official home of the Python Programming Languageicon-default.png?t=N7T8https://www.python.org/downloads/windows/ 2 安装unrar

pip install unrar

3 下载unrar的Library

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

4 写解密的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

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

你可能感兴趣的:(python,开发语言)