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:
JavaScript
18
1
import random
2
3
characters = "ABCDE"
4
length = 5
5
while True:
6
pw = ""
7
8
#Generating Section
9
for i in range(length):
10
next_index = random.randrange(len(characters))
11
pw = pw + characters[next_index]
12
13
if pw == "ABCDE":
14
print()
15
print()
16
print("Password acquired.")
17
break
18
Character generator code:
JavaScript
6
1
import itertools
2
3
res = itertools.permutations(test ,length)
4
for i in res:
5
print(''.join(i))
6
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.
JavaScript
24
1
import itertools
2
3
characters = "ABCDE"
4
length = 5
5
done = False
6
7
while True:
8
def pw_guess():
9
res = itertools.permutations('ABCDE' ,5)
10
for guess in res:
11
yield guess
12
13
#Make generator object
14
guess_generator = pw_guess()
15
16
for guess in guess_generator:
17
if guess == ('A', 'B', 'C', 'D', 'E'):
18
print()
19
print("Password acquired: " + str(guess))
20
done = True
21
break
22
if done:
23
break
24
A generator computes the guesses one by one as opposed to computing in advance.