assert 65 ^ 42 == 107
assert 107 ^ 42 == 65
assert ord('a') == 97
assert chr(97) == 'a'

"""
Explanation: Project Euler: Problem 59
https://projecteuler.net/problem=59
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 cipher.txt (in this directory), 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.
The following cell shows examples of how to perform XOR in Python and how to go back and forth between characters and integers:
End of explanation
"""


from itertools

"""
Explanation: Certain functions in the itertools module may be useful for computing permutations:
End of explanation
"""


lst = []

decrypted_message = []

encrypted_message = []

common = ['the', 'and', 'or']

"""
Explanation: Here's what I have so far, however it is not working and causes slowdown, so I wouldn't run it.
First, I started by creating lists that I would need.
End of explanation
"""


# Causes computer slowdown, don't run
file = open("cipher.txt", "r+")

decrypt_key = { 'A' : range(ord('a'), ord('z')), 'B' : range(ord('a'), ord('z')), 'C' : range(ord('a'), ord('z'))}

for n in file:
    lst.append(n)

mess = lst[0]
message = mess.split(",")

for n in message:
    encrypted_message.append(int(n))

for n in encrypted_message:
    for x in decrypt_key['A']:
        d = n ^ x
        for y in decrypt_key['B']:
            e = d ^ y
            for z in decrypt_key['C']:
                f = e ^ z
                decrypted_message.append(chr(f))
                    
                    
if common[0] and common[1] and common[2] in ''.join(decrypted_message):
    print(''.join(decrypted_message))  
    


    
    





    
    
file.close()

# This cell will be used for grading, leave it at the end of the notebook.

"""
Explanation: Then, I set a variable file to the open cipher.txt.  I set it in r+ mode just to be safe in case I needed to edit the file.
I knew my key had to be three lower case letters, so I created a dictionary that defined all the possibilies of all three letters.
I then added the contents of cipher.txt to lst.
However, lst just contained one string of all the numbers including the commas, so accessing the index where it was stored in lst and setting that to mess, I used the .split method to split the string into multiple strings and exluded the commas.  I set that result to message.
Finally, I looped through message and turned all the elements into integers and appended them to encrypted_message.
Next is where I think I went wrong.  My plan was to loop through encrypted_message and xor the values with all the possible values of the three characters in the key individually using nested for loops, and then append the final, decrypted characters to a list.  However this would then obviously create a giant list with a ton of values in it.  Using the common list of words defined above and seeing if those words were in my decrypted_message didn't work, since my list contained all possbile messages decoded with all possible keys, it would print out the entire list hence the problem.
I think I should've somehow found a way to only append the chr(f) to a list if the f's made sense together, however I am not sure how to do that.
End of explanation
"""


