-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path059_XOR_decryption.py
25 lines (20 loc) · 964 Bytes
/
059_XOR_decryption.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
'''Using 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 encryption
key consists of three lower case characters'''
from itertools import cycle
path = 'Project_Euler/puzzle_input/059.txt'
with open(path) as file:
nums = [int(n) for n in file.read().split(',')]
alphabet_lowercase = range(97, 123)
def decrypt():
for a in alphabet_lowercase:
for b in alphabet_lowercase:
for c in alphabet_lowercase:
key = iter(cycle((a, b, c)))
decrypted = ''.join(chr(n ^ next(key)) for n in nums)
if 'the' in decrypted and 'and' in decrypted and 'for' in decrypted:
key = iter(cycle((a, b, c)))
return decrypted + '\n', sum(n ^ next(key) for n in nums)
decypted_message, decrypted_ascii_sum = decrypt()
print(decypted_message)
print(decrypted_ascii_sum)