【发布时间】:2021-01-29 05:55:52
【问题描述】:
我正在尝试做一个凯撒密码,这是我的代码
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()
我得到的输出是 CU4GXA:;2a:/54Y,我要恭喜你。 现在我有点意识到这个问题,那就是字母表中所有小写字母的 ASCII 码仅从 97 到 122 并且每当我从字母顺序中减去键时,它就会超出 97 和 122。
我尝试使用 % 解决这个问题(我也不是 100% 确定如何使用模运算符),但我没有成功。
文本文件的内容是:
18
UgFYjSLMDsLAGFk
【问题讨论】:
-
我在您发布的代码中没有看到
%。你可能想要% 256或% 128。 -
第一个搜索结果是stackoverflow.com/questions/53886402/…,它是c,但您可能会理解它。
标签: python ascii caesar-cipher