使用 itertools 暴力破解应用程序

Lui*_*rti 2 python brute-force python-itertools

我正在尝试编写一个应用程序来暴力破解一个完全小写、仅字母、6 个字符长的密码,其中有 2 个字母重复两次。我尝试使用itertools.product(string.lowercase, 6),但即使我有一台非常“强大”的计算机,它也没有足够的 RAM 来完成计算,这是因为itertools在返回之前实际上构建了一个完整的列表。

所以然后我想到有一个像这样的列表[0, 0, 0, 0, 0, 0],它会从右到左计数,每次索引达到 26 时,它都会返回到 0,并且它左边的槽会增加 1,除非我还没有找到一种编码方法。

我已经尝试解决这个问题有一段时间了,转向这里是我最后的手段,任何算法技巧都非常受欢迎。我很抱歉提出这种“家庭作业”类型的问题,但我真的被这个问题惊呆了。

小智 5

使用这样的东西将不需要占用太多内存:

import itertools
import string

for guess in itertools.product(string.lowercase, repeat=6):
    if checkguess(''.join(guess)):
        print("Password is: {0}".format(''.join(guess)))
Run Code Online (Sandbox Code Playgroud)

itertools.product一一生成可能的组合。它不返回任何内容,它是一个生成器函数。请参阅python 文档。中的所有函数的itertools行为都类似于迭代器或处理迭代器,因此得名。

如果您的代码占用了大量内存,您可能需要将其发布到此处,以便我们可以找到罪魁祸首。

根据 DSM 对更快代码的建议,您可以执行以下操作:

import string
from itertools import product, combinations, permutations

for guess_chars in combinations(string.lowercase, 4):
    for doubled_chars in combinations(guess_chars, 2):
        for guess in permutations(guess_chars + doubled_chars):
            #...
Run Code Online (Sandbox Code Playgroud)