I'm trying to do a caesar cipher and here is my code
def getinputfile(message):
list_ = []
with open(message, 'r') as lines:
content = lines.readlines()
for i in content:
list_.append(i.strip())
return list_
def decrpyt(list_):
decrypt_num = []
decrypt_list = []
sec_message = list_[1]
sec_key = int(list_[0])
for letter in sec_message :
uni_num = (ord(letter) - sec_key)
decrypt_num.append(uni_num)
for unicode in decrypt_num:
uni_letter = chr(unicode)
decrypt_list.append(uni_letter)
return decrypt_list
def main():
message = getinputfile('secretMessage1.txt')
decrpyted_message = decrpyt(message)
print(''.join(decrpyted_message))
main()
The output that I got is CU4GXA:;2a:/54Y and I want congratulations. Now I sort of realize the problem and that is the ASCII code for all the lower case letters in the alphabet is only from 97 - 122 and whenever I subtract the key from the ord of the letters, it goes beyond 97 and 122.
I tried solving this using % (I'm not 100% sure as to how to use the modulo operator as well) but I wasn't successful in doing so.
The contents of the text file are:
18
UgFYjSLMDsLAGFk