Oban
Oban

Reputation: 11

python caesar cipher brute force decrypting

I am doing my final for my grade 11 computer science and I have to make a caesar cipher with encryption, decryption and brute force and I am completely stuck on the brute force. Can anyone help me?

Upvotes: 1

Views: 275

Answers (1)

The Pilot Dude
The Pilot Dude

Reputation: 2227

You can use the itertools module to loop for every combination . This is how I would do it:

import itertools
import string
import sys

chars = string.ascii_lowercase + string.ascii_uppercase + string.digits

user_passw = input("Enter Your Password: ")
attempts = 0

for password_length in range(1, 9):
    for guess in itertools.product(chars, repeat=password_length):
        guess = ''.join(guess)
        attempts += 1
        print(guess, attempts)
        if guess == user_passw:                
            print('Password Is {}'.format(guess))
            sys.exit()

Basically, we first define all of the characters that can be used in the brute force. To do this, we use the string module an create one large string with every character we will use called chars

After, we ask the user to input a password that will be brute forced.

Next, we start a for loop that will run in range 1 and 9. Please note that 1 represents the minimum length of the password and 9 represents the maximum possible length of the password. You can change them to len(user_passw) if you wanted to.

Next, we create a nested for loop that loops through every letter in the chars string.

We then print the guess and number of attempts purely for a visual reference.

Next, we check to see if the guess is equal to the password. If it is, then the computer prints it to the user and exits the loop

Upvotes: 0

Related Questions