I have code that uses the brute force technique to find the message to a Caesar cipher. The program decrypts the message using different keys and then checks whether the resulting words are contained in a file called 'wordlist.txt'. if they are, it returns only that result.
from collections import namedtuple
Keycode = namedtuple('Keycode', 'key code')
def picks(filename):
with open(filename) as f:
lines = [line.strip() for line in f]
return lines
message = 'vlr alkq exsb ql qefkh xylrq qexq exex.'
LETTERS = 'abcdefghijklmnopqrstuvwxyz'
result = []
for key in range(len(LETTERS)):
translated = ''
for symbol in message.lower():
if symbol in LETTERS:
num = LETTERS.find(symbol)
num = num - key
if num < 0:
num = num + len(LETTERS)
translated += LETTERS[num]
else:
translated += symbol
result.append(Keycode(key, translated))
for item in result:
d = item.code.split()
for word in d:
if word in picks('wordlist.txt'):
print('Code : {0:s}\nKey : {1:}'.format(item.code, item.key))
break
The code that I have does the job but it takes time and the code is not clean. Is there a way to make the code a bit cleaner? I am new to Python and haven't started on classes, so I cant use those (although it would have made my job easier).