请大家帮忙提速下面这段代码

代码的目的是暴力破解RAR文件的密码

目前破解4位密码,字典范围是数字和小写英文时,最多需要40几分钟才能破解完

如果是更多位的密码的话,耗时就更长了。

请各位看看,有没有提速的方法,谢谢!

from unrar import rarfile
import os
import itertools as its
import time
import threading
import line_profiler
from numba import jit,cuda
import logging
import warnings

# warnings.filterwarnings('ignore')
# @jit()    # (debug=False,nopython=True,cache=True)
def get_pwd(file, output_path, pwd):
    '''
    判断密码是否正确
    :param file_path: 需要破解的文件路径,这里仅对单个文件进行破解
    :param output_path: 解压输出文件路径
    :param pwd: 传入的密码
    :return:
    '''
    try:
        file.extractall(path=output_path, pwd=pwd)
        # 说明当前密码有效,并告知
        print('-->Find password is "{}"'.format(pwd))
        # global right_pwd
        right_pwd.append(pwd)
        return pwd
    except :   # Exception as e    
        # 密码不正确
        # print('"{}" is not correct password!'.format(pwd))
        # print(e)
        return False

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

def find():
    file_path = 'D:\字典.rar'    # input("请输入要破解的压缩包位置:")    
    file = rarfile.RarFile(file_path)
    output_path = 'D:'

    # 密码范围
    # words = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'  # 涉及到生成密码的参数
    words = 'jfhgurmvytnb0546827913iekdcowlsxpqaz' 
    min_digit = 4
    max_digit = 4
    all = 0
    for digit in range(min_digit,max_digit+1):
        all = all + pow(len(words),digit) 
    print('共有{:.0f}个密码待尝试'.format(all))
    pwds = get_password(min_digit, max_digit, words)
    # 开始查找密码
    start = time.time()
    thr = 0
    for i in range(1,all+1):
        dur = time.time() - start
        print("\r已尝试{:.0f}%,耗时{:.0f}s".format(i/all*100,dur),end = '')    # 进度条
        # 单线程
        # try :
        #     pwd = next(pwds)
        #     if get_pwd(file, output_path, pwd=pwd):
        #         break
        # except StopIteration:
        #     # print('\n')
        #     print('\n已遍历所有可能,破解失败!')
        #     thr = 0
        #     break

        # 多线程
        try :
            pwd = next(pwds)
            try:
                th = threading.Thread(target=get_pwd,args=(file, output_path, pwd))
                th.start()
            except :    # Exception as e
                pass
        except StopIteration:
            print('已遍历所有可能,程序结束')
            thr = 1
            break

# 等待所有线程跑完
    if thr == 1:
        for th in threading.enumerate():
            if th is threading.current_thread():
                continue
            th.join()

    end = time.time()
    print('-->总耗时{:.0f}s'.format(end - start))

right_pwd = []
profile = line_profiler.LineProfiler(find)  # 把函数传递到性能分析器
profile.enable()  # 开始分析
find()
profile.disable()  # 停止分析
profile.print_stats()  # 打印出性能分析结果

# 密码输出
with open('password.txt','w') as f:
    f.write(right_pwd[0])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值