each character on a computer is assigned a unique code and the preferred standard is ascii american standard code for information interchange for example uppercase a 65 asterisk 42 and lowercase k 107 a modern encryption method is to take a text file convert the bytes to ascii then xor each byte with a given value taken from a secret key the advantage with the xor function is that using the same encryption key on the cipher text restores the plain text for example 65 xor 42 107 then 107 xor 42 65 for unbreakable encryption the key is the same length as the plain text message and the key is made up of random bytes the user would keep the encrypted message and the encryption key in different locations and without both halves it is impossible to decrypt the message unfortunately this method is impractical for most users so the modified method is to use a password as a key if the password is shorter than the message which is likely the key is repeated cyclically throughout the message the balance for this method is using a sufficiently long password key for security but short enough to be memorable your task has been made easy as the encryption key consists of three lower case characters using p059cipher txt right click and save linktarget as a file containing the encrypted ascii codes and the knowledge that the plain text must contain common english words decrypt the message and find the sum of the ascii values in the original text given an encrypted message and a possible 3character key decrypt the message if the decrypted message contains a invalid character i e not an ascii letter a digit punctuation or whitespace then we know the key is incorrect so return none trykey0 17 20 4 27 104 116 120 hello trykey68 10 300 4 27 104 116 120 is none true given an encrypted message test all 3character strings to try and find the key return a list of the possible decrypted messages from itertools import cycle text the enemy s gate is down key end encoded ordk ordc for k c in zipcyclekey text text in filtervalidcharsencoded true given a list of possible decoded messages narrow down the possibilities for checking for the presence of a specified common word only decoded messages containing commonword will be returned filtercommonword asfla adf i am here a am i am here filtercommonword athla amf i am here a am athla amf i am here test the ciphertext against all possible 3character keys then narrow down the possibilities by filtering using common words until there s only one possible decoded message solutiontestcipher txt 3000 given an encrypted message and a possible 3 character key decrypt the message if the decrypted message contains a invalid character i e not an ascii letter a digit punctuation or whitespace then we know the key is incorrect so return none try_key 0 17 20 4 27 104 116 120 hello try_key 68 10 300 4 27 104 116 120 is none true given an encrypted message test all 3 character strings to try and find the key return a list of the possible decrypted messages from itertools import cycle text the enemy s gate is down key end encoded ord k ord c for k c in zip cycle key text text in filter_valid_chars encoded true given a list of possible decoded messages narrow down the possibilities for checking for the presence of a specified common word only decoded messages containing common_word will be returned filter_common_word asfla adf i am here a am i am here filter_common_word athla amf i am here a am athla amf i am here test the ciphertext against all possible 3 character keys then narrow down the possibilities by filtering using common words until there s only one possible decoded message solution test_cipher txt 3000
from __future__ import annotations

import string
from itertools import cycle, product
from pathlib import Path

VALID_CHARS: str = (
    string.ascii_letters + string.digits + string.punctuation + string.whitespace
)
LOWERCASE_INTS: list[int] = [ord(letter) for letter in string.ascii_lowercase]
VALID_INTS: set[int] = {ord(char) for char in VALID_CHARS}

COMMON_WORDS: list[str] = ["the", "be", "to", "of", "and", "in", "that", "have"]


def try_key(ciphertext: list[int], key: tuple[int, ...]) -> str | None:
    
    decoded: str = ""
    keychar: int
    cipherchar: int
    decodedchar: int

    for keychar, cipherchar in zip(cycle(key), ciphertext):
        decodedchar = cipherchar ^ keychar
        if decodedchar not in VALID_INTS:
            return None
        decoded += chr(decodedchar)

    return decoded


def filter_valid_chars(ciphertext: list[int]) -> list[str]:
    
    possibles: list[str] = []
    for key in product(LOWERCASE_INTS, repeat=3):
        encoded = try_key(ciphertext, key)
        if encoded is not None:
            possibles.append(encoded)
    return possibles


def filter_common_word(possibles: list[str], common_word: str) -> list[str]:
    
    return [possible for possible in possibles if common_word in possible.lower()]


def solution(filename: str = "p059_cipher.txt") -> int:
    
    ciphertext: list[int]
    possibles: list[str]
    common_word: str
    decoded_text: str
    data: str = Path(__file__).parent.joinpath(filename).read_text(encoding="utf-8")

    ciphertext = [int(number) for number in data.strip().split(",")]

    possibles = filter_valid_chars(ciphertext)
    for common_word in COMMON_WORDS:
        possibles = filter_common_word(possibles, common_word)
        if len(possibles) == 1:
            break

    decoded_text = possibles[0]
    return sum(ord(char) for char in decoded_text)


if __name__ == "__main__":
    print(f"{solution() = }")