利用python暴力破解rar压缩文件密码

主体内容

将要破解的文件改名为test.rar放入同意文件夹即可,中间把rarfile改成zipfile即可破解zip文件

import rarfile
import random
import time
import sys


class MyIterator():
    # 单位字符集合
    letters = 'abcdefghijklmnopqrstuvwxyz0123456789*'
    min_digits = 0
    max_digits = 0

    def __init__(self, min_digits, max_digits):
        # 实例化对象时给出密码位数范围,一般4到10位
        if min_digits < max_digits:
            self.min_digits = min_digits
            self.max_digits = max_digits
        else:
            self.min_digits = max_digits
            self.max_digits = min_digits

    # 迭代器访问定义
    def __iter__(self):
        return self

    def __next__(self):
        rst = str()
        for item in range(0, random.randrange(self.min_digits, self.max_digits + 1)):
            rst += random.choice(MyIterator.letters)
        return rst


def extract():
    start_time = time.time()
    zfile = rarfile.RarFile("test.rar")
    # 解压文件名字


    for p in MyIterator(4, 10):
        try:
            zfile.extractall(path=".", pwd=str(p).encode('utf-8'))
            # 文件地址这里用.说明当前文件夹,如果C:\Users这样形式
            print("the password is {}".format(p))
            now_time = time.time()
            print("spend time is {}".format(now_time - start_time))
            sys.exit(0)
        except Exception as e:
            print('error',p)
            pass


if __name__ == '__main__':
    extract()

random.choice()

choice() 方法返回一个列表,元组或字符串的随机项。

#!/usr/bin/python
import random

print "choice([1, 2, 3, 5, 9]) : ", random.choice([1, 2, 3, 5, 9])
print "choice('A String') : ", random.choice('A String')

结果

choice([1, 2, 3, 5, 9]) :  2
choice('A String') :  n

迭代器尝试

from random import choice
class MyIterator():
    letters='1dasjdoi'

    def __iter__(self):
        return self

    def __next__(self):
        a=choice(MyIterator.letters)
        return a
for i in MyIterator():
    print(i)

随即输出letters里字母在这里插入图片描述

你可能感兴趣的:(python插件游戏制作)