Project Euler 59 - XOR decryption
Official link: https://projecteuler.net/problem=59
Thought Process
My solution is a brute force solution but it works and I really enjoyed coding this problem. I am making use of the python built in functions chr() and ord().
chr(x) takes an integer x and converts it to it's ASCII counter string, ord(x) does the opposite, takes a string x and converts it to its ASCII value.
The most annoying part of this problem was the vague specification of what characters are allowed for your sanity I will tell you that all letters (uppercase and lowercase) are allowed along with the following symbols: " ' ( ) + , . / : ; [ ]
I made a function AcceptableChar(char) which takes a character and checks if it is a "valid" character that is, one of the above mentioned characters, it returns True or False
Now we can get into the finding the password part.
Lower case letters are in the range 97 - 122, so I make a triple loop going though a, b, c where 97 <= a,b,c <= 122 and create a temporary password chr(a) + chr(b) + chr(c).
The cipher is obviously much longer than 3 so the password will repeat cyclically, so I go through the cipher with variable i and I perform the XOR function with chr(a), (this is written (i^chr(a)), and I make sure to cyclically repeat chr(a), chr(b), chr(c) (Hint: using mod 3)
I check if AcceptableChar(i^chr(a or b or c)) == False, I break the loop because I know this cannot be the correct key.
Once I have found the correct combination of chr(a) + chr(b) + chr(c) which decodes every letter in the cipher, I can begin to reveal the message and start summing the characters
Interactive Code
No interactive code for this problem, my code is shown below