galaxies
galaxies

Reputation: 63

Caeser Cipher Brute Force Attack Won't Decrypt - Python

I've tried to make a program for brute force attack that will accept an input of a caesar ciphered message and attempt to break it without a rotation factor. The output should be a list of all 26 rotation factors with the corresponding messages (eg: KEY # 01 : (shifting the inputs characters by 1, etc)) and one of those should contain the decrypted message. The rotation number of that message will then be the key, (I hope that wasn't too confusing). Here is my code:

message = input("Enter a message: ") # user inputs message
    offset = 0

    while offset < 26:
        for char in message:
            decrypt = " "

            if not char.isalpha(): # to keep punctuation unchanged
                decrypt = decrypt + char

            elif char.isupper():
                decrypt = decrypt + chr((ord(char) - offset - 65) % 26 + 65) # -65 +65 for uppercase Z

            else:
                decrypt = decrypt + chr((ord(char) - offset - 97) % 26 + 97) # -97 +97 for lowercase z

            offset = offset + 1
            print ("KEY #:", offset, message)

The program does not decrypt the input and for some reason it prints the same input 41 times... I'm not a very skilled programmer so it would be great if someone could help me out.

Upvotes: 0

Views: 748

Answers (2)

galaxies
galaxies

Reputation: 63

message = input("Enter a message: ") # user inputs message
    offset = 0 # setting offset to 0

    for offset in range(len(message)): # for loop to run as many times as length of message

        decrypt = " "
        for char in message:

            if not char.isalpha(): # to keep punctuation unchanged
                decrypt = decrypt + char

            elif char.isupper():
                decrypt = decrypt + chr((ord(char) - offset - 65) % 26 + 65) # -65 +65 for uppercase Z

            else:
                decrypt = decrypt + chr((ord(char) - offset - 97) % 26 + 97) # -97 +97 for lowercase z

        print ("KEY #:", offset, decrypt)

Upvotes: 0

Grismar
Grismar

Reputation: 31354

There are three problems with your script: the offset = offset + 1 is inside your for loop, so offset gets increased for every character, which you don't want. And your initialisation of the decrypt variable is inside the loop as well. Finally, you're printing the message, not the decrypt result.

This does work:

message = input("Enter a message: ")  # user inputs message
offset = 0

while offset < 26:
    decrypt = ""
    for char in message:
        if not char.isalpha():  # to keep punctuation unchanged
            decrypt = decrypt + char

        elif char.isupper():
            decrypt = decrypt + chr((ord(char) - offset - 65) % 26 + 65)  # -65 +65 for uppercase Z

        else:
            decrypt = decrypt + chr((ord(char) - offset - 97) % 26 + 97)  # -97 +97 for lowercase z

    offset = offset + 1
    print("KEY #:", offset, decrypt)

Upvotes: 1

Related Questions