encrypt encodes a given string with the caesar cipher and returns the encoded message parameters inputstring the plaintext that needs to be encoded key the number of letters to shift the message by optional alphabet none the alphabet used to encode the cipher if not specified the standard english alphabet with upper and lowercase letters is used returns a string containing the encoded ciphertext more on the caesar cipher the caesar cipher is named after julius caesar who used it when sending secret military messages to his troops this is a simple substitution cipher where every character in the plaintext is shifted by a certain number known as the key or shift example say we have the following message hello captain and our alphabet is made up of lower and uppercase letters abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz and our shift is 2 we can then encode the message one letter at a time h would become j since j is two letters away and so on if the shift is ever two large or our letter is at the end of the alphabet we just start at the beginning z would shift to a then b and so on our final message would be jgnnq ecrvckp further reading https en m wikipedia orgwikicaesarcipher doctests encrypt the quick brown fox jumps over the lazy dog 8 bpm ycqks jzwev nwf rcuxa wdmz bpm tihg lwo encrypt a very large key 8000 s nwjq dsjyw cwq encrypt a lowercase alphabet 5 abcdefghijklmnopqrstuvwxyz f qtbjwhfxj fqumfgjy set default alphabet to lower and upper case english chars the final result string append without encryption if character is not in the alphabet get the index of the new key and make sure it isn t too large append the encoded character to the alphabet decrypt decodes a given string of ciphertext and returns the decoded plaintext parameters inputstring the ciphertext that needs to be decoded key the number of letters to shift the message backwards by to decode optional alphabet none the alphabet used to decode the cipher if not specified the standard english alphabet with upper and lowercase letters is used returns a string containing the decoded plaintext more on the caesar cipher the caesar cipher is named after julius caesar who used it when sending secret military messages to his troops this is a simple substitution cipher where very character in the plaintext is shifted by a certain number known as the key or shift please keep in mind here we will be focused on decryption example say we have the following ciphertext jgnnq ecrvckp and our alphabet is made up of lower and uppercase letters abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz and our shift is 2 to decode the message we would do the same thing as encoding but in reverse the first letter j would become h remember we are decoding because h is two letters in reverse to the left of j we would continue doing this a letter like a would shift back to the end of the alphabet and would become z or y and so on our final message would be hello captain further reading https en m wikipedia orgwikicaesarcipher doctests decrypt bpm ycqks jzwev nwf rcuxa wdmz bpm tihg lwo 8 the quick brown fox jumps over the lazy dog decrypt s nwjq dsjyw cwq 8000 a very large key decrypt f qtbjwhfxj fqumfgjy 5 abcdefghijklmnopqrstuvwxyz a lowercase alphabet turn on decode mode by making the key negative bruteforce returns all the possible combinations of keys and the decoded strings in the form of a dictionary parameters inputstring the ciphertext that needs to be used during bruteforce optional alphabet none the alphabet used to decode the cipher if not specified the standard english alphabet with upper and lowercase letters is used more about brute force brute force is when a person intercepts a message or password not knowing the key and tries every single combination this is easy with the caesar cipher since there are only all the letters in the alphabet the more complex the cipher the larger amount of time it will take to do brute force ex say we have a 5 letter alphabet abcde for simplicity and we intercepted the following message dbc we could then just write out every combination ecd and so on until we reach a combination that makes sense cab further reading https en wikipedia orgwikibruteforce doctests bruteforcejfyumy xih n vlony zilwy gy 20 please don t brute force me bruteforce1 traceback most recent call last typeerror int object is not iterable set default alphabet to lower and upper case english chars to store data on all the combinations cycle through each combination decrypt the message and store the result in the data get user input run functions based on what the user chose encrypt encodes a given string with the caesar cipher and returns the encoded message parameters input_string the plain text that needs to be encoded key the number of letters to shift the message by optional alphabet none the alphabet used to encode the cipher if not specified the standard english alphabet with upper and lowercase letters is used returns a string containing the encoded cipher text more on the caesar cipher the caesar cipher is named after julius caesar who used it when sending secret military messages to his troops this is a simple substitution cipher where every character in the plain text is shifted by a certain number known as the key or shift example say we have the following message hello captain and our alphabet is made up of lower and uppercase letters abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz and our shift is 2 we can then encode the message one letter at a time h would become j since j is two letters away and so on if the shift is ever two large or our letter is at the end of the alphabet we just start at the beginning z would shift to a then b and so on our final message would be jgnnq ecrvckp further reading https en m wikipedia org wiki caesar_cipher doctests encrypt the quick brown fox jumps over the lazy dog 8 bpm ycqks jzwev nwf rcuxa wdmz bpm tihg lwo encrypt a very large key 8000 s nwjq dsjyw cwq encrypt a lowercase alphabet 5 abcdefghijklmnopqrstuvwxyz f qtbjwhfxj fqumfgjy set default alphabet to lower and upper case english chars the final result string append without encryption if character is not in the alphabet get the index of the new key and make sure it isn t too large append the encoded character to the alphabet decrypt decodes a given string of cipher text and returns the decoded plain text parameters input_string the cipher text that needs to be decoded key the number of letters to shift the message backwards by to decode optional alphabet none the alphabet used to decode the cipher if not specified the standard english alphabet with upper and lowercase letters is used returns a string containing the decoded plain text more on the caesar cipher the caesar cipher is named after julius caesar who used it when sending secret military messages to his troops this is a simple substitution cipher where very character in the plain text is shifted by a certain number known as the key or shift please keep in mind here we will be focused on decryption example say we have the following cipher text jgnnq ecrvckp and our alphabet is made up of lower and uppercase letters abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz and our shift is 2 to decode the message we would do the same thing as encoding but in reverse the first letter j would become h remember we are decoding because h is two letters in reverse to the left of j we would continue doing this a letter like a would shift back to the end of the alphabet and would become z or y and so on our final message would be hello captain further reading https en m wikipedia org wiki caesar_cipher doctests decrypt bpm ycqks jzwev nwf rcuxa wdmz bpm tihg lwo 8 the quick brown fox jumps over the lazy dog decrypt s nwjq dsjyw cwq 8000 a very large key decrypt f qtbjwhfxj fqumfgjy 5 abcdefghijklmnopqrstuvwxyz a lowercase alphabet turn on decode mode by making the key negative brute_force returns all the possible combinations of keys and the decoded strings in the form of a dictionary parameters input_string the cipher text that needs to be used during brute force optional alphabet none the alphabet used to decode the cipher if not specified the standard english alphabet with upper and lowercase letters is used more about brute force brute force is when a person intercepts a message or password not knowing the key and tries every single combination this is easy with the caesar cipher since there are only all the letters in the alphabet the more complex the cipher the larger amount of time it will take to do brute force ex say we have a 5 letter alphabet abcde for simplicity and we intercepted the following message dbc we could then just write out every combination ecd and so on until we reach a combination that makes sense cab further reading https en wikipedia org wiki brute_force doctests brute_force jfyumy xih n vlony zilwy gy 20 please don t brute force me brute_force 1 traceback most recent call last typeerror int object is not iterable set default alphabet to lower and upper case english chars to store data on all the combinations cycle through each combination decrypt the message and store the result in the data get user input run functions based on what the user chose
from __future__ import annotations

from string import ascii_letters


def encrypt(input_string: str, key: int, alphabet: str | None = None) -> str:
    
    
    alpha = alphabet or ascii_letters

    
    result = ""

    for character in input_string:
        if character not in alpha:
            
            result += character
        else:
            
            new_key = (alpha.index(character) + key) % len(alpha)

            
            result += alpha[new_key]

    return result


def decrypt(input_string: str, key: int, alphabet: str | None = None) -> str:
    
    
    key *= -1

    return encrypt(input_string, key, alphabet)


def brute_force(input_string: str, alphabet: str | None = None) -> dict[int, str]:
    
    
    alpha = alphabet or ascii_letters

    
    brute_force_data = {}

    
    for key in range(1, len(alpha) + 1):
        
        brute_force_data[key] = decrypt(input_string, key, alpha)

    return brute_force_data


if __name__ == "__main__":
    while True:
        print(f'\n{"-" * 10}\n Menu\n{"-" * 10}')
        print(*["1.Encrypt", "2.Decrypt", "3.BruteForce", "4.Quit"], sep="\n")

        
        choice = input("\nWhat would you like to do?: ").strip() or "4"

        
        if choice not in ("1", "2", "3", "4"):
            print("Invalid choice, please enter a valid choice")
        elif choice == "1":
            input_string = input("Please enter the string to be encrypted: ")
            key = int(input("Please enter off-set: ").strip())

            print(encrypt(input_string, key))
        elif choice == "2":
            input_string = input("Please enter the string to be decrypted: ")
            key = int(input("Please enter off-set: ").strip())

            print(decrypt(input_string, key))
        elif choice == "3":
            input_string = input("Please enter the string to be decrypted: ")
            brute_force_data = brute_force(input_string)

            for key, value in brute_force_data.items():
                print(f"Key: {key} | Message: {value}")

        elif choice == "4":
            print("Goodbye.")
            break