-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathproblem59.py
More file actions
28 lines (25 loc) · 879 Bytes
/
problem59.py
File metadata and controls
28 lines (25 loc) · 879 Bytes
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
26
27
28
fh = open('cipher1.txt', 'r')
values = map(lambda x: int(x), fh.read().split(','))
# key is 3 letters from a-z
key_length = 3
valid_chars = [chr(i) for i in xrange(ord('a'), ord('z') + 1)]
clue_words = [' the ', ' and ', ' or ', ' but ', ' in ']
def guess_password(password='', key_length=3):
if key_length > 0:
for char in valid_chars:
guess_password(password + char, key_length - 1)
else:
xored = []
for i in xrange(len(values)):
xored.append(chr(values[i] ^ ord(password[i % 3])))
xored_text = ''.join(xored)
matches = 0
for clue_word in clue_words:
if clue_word in xored_text:
matches += 1
if matches > 3:
print password
print xored_text[:100]
print sum(map(lambda x: ord(x), xored))
return
guess_password()