Python - 蛮力发生器

时间:2015-05-23 20:19:48

标签: python-3.x brute-force

我已经制作了一个强力密码破解器,但是我也制作了一个组合发生器的字符串,它可以作为强力弦发生器更好;但是我无法弄清楚如何将两者结合起来。

暴力代码:

import random

characters = "ABCDE"
length = 5
while True:
    pw = ""

   #Generating Section
    for i in range(length):
        next_index = random.randrange(len(characters))
        pw = pw + characters[next_index]

    if pw == "ABCDE":
        print()
        print()
        print("Password acquired.")
        break

字符生成器代码:

import itertools

res = itertools.permutations(test ,length)
for i in res: 
    print(''.join(i))

我想替换" Generating"蛮力代码部分与改进的生成器代码,但我无法做到这一点。请帮忙。

3 个答案:

答案 0 :(得分:1)

我更改了您的代码以使用itertools并使用generator迭代可能的密码,因此您的计算量不会超过需要。

import itertools

characters = "ABCDE"
length = 5
done = False

while True:
    def pw_guess():
        res = itertools.permutations('ABCDE' ,5)
        for guess in res:
            yield guess

    #Make generator object
    guess_generator = pw_guess()

    for guess in guess_generator:
         if guess == ('A', 'B', 'C', 'D', 'E'):
            print()
            print("Password acquired: " + str(guess))
            done = True
            break
    if done:
        break

生成器逐个计算猜测,而不是事先计算。

答案 1 :(得分:0)

Tbh,我做了一个蛮力计划,不需要那么多代码。我的代码中的一些行是可选的(比如计算猜测密码所花费的次数)。试试我的代码:

import random
import string
guessAttempts = 0
myPassword = input("Enter a password for the computer to try and guess: ")
passwordLength = len(myPassword)
while True:
    guessAttempts = guessAttempts + 1
    passwordGuess = ''.join([random.choice(string.ascii_letters + string.digits)for n in range(passwordLength)])
    print(passwordGuess)
    if passwordGuess == myPassword:
        print("Password guessed successfully!")
        print("It took the computer %s guesses to guess your password." % (guessAttempts))
        break

答案 2 :(得分:0)

我使用 itertools.product 从 [a-z, 0-9] 列表中生成任意长度的密码。最后一个循环中的 1800 用于我的测试。

import itertools
char_set = [chr(_) for _ in list(range(ord('a'), ord('z')+1))] + [chr(_) for _ in list(range(ord('0'), ord('9')+1))]

def my_fun(alphanum):
    i = 0
    while True:
        result = itertools.product(alphanum, repeat=i)
        for guess in result:
            yield guess
        i += 1

g = my_fun(char_set)
for c in range(1800):
        print((''.join(next(g))))