I’ve made a brute force password cracker which works, but I also made a string combining generator which would be better as the brute force string generator; however I can’t figure out how to combine the two.
Brute force code:
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
Character generator code:
import itertools
res = itertools.permutations(test ,length)
for i in res: 
    print(''.join(i))
I would like to replace the “Generating” section from the brute force code with the improved generator code but I can’t do this. Please help.
Advertisement
Answer
I changed your code to use itertools and to iterate through the possible passwords using a generator so you aren’t computing more than needed.    
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
A generator computes the guesses one by one as opposed to computing in advance.
