破解凯撒密码的程序 [英] Program to crack Caesar cipher
问题描述
凯撒密码基本上将每个明文字母移动一个固定数字.例如,如果使用键2,则单词Sourpuss将被编码为Uqwrtrwuu
The Caesar cipher basically shifts each letter of plaintext by a fixed number. For example, if the key 2 is used, the word Sourpuss would be encoded Uqwrtrwuu
文本只能包含可打印的ASCII字符(出于我们的目的,为32-126).实现用于破解此代码的算法.
The text can contain only the printable ASCII characters (32-126, for our purposes). Implement an algorithm for cracking this code.
我需要解密:"T!x $ r&'} r& z!%21j!'1〜zxy& 1"r %% 1TZedBEAB?"
I need to decrypt this: "T! x$r&'}r&z! %21j!'1~zxy&1"r%%1TZedBEAB?"
这是我的代码:
def decoded(s):
for i in range(1,95):
string = ""
for char in s:
if(ord(char) + i > 126):
charc = (ord(char) + i) - 94
string = string + chr(charc)
else:
charc = ord(char) + i
string = string + chr(charc)
print(string)
decoded("T! x$r&'}r&z! %21j!'1~zxy&1\"r%%1TZedBEAB?")
您可以看到我添加了 \
,但是我认为这不会改变我的答案吗?
As you can see I added a \
but I don't think that would change my answer?
问题是它没有打印出正确的答案.有人可以告诉我我的代码有什么问题吗,或者只是指出正确的方向.
The problem is that it is not printing out a correct answer. Could someone please tell me what is wrong with my code or just point me in the right direction.
推荐答案
关键#78:恭喜!您可能会通过CITS1401.
Key #78: Congratulations! You might pass CITS1401.
您要做的就是在第6行中将 -94
更改为 -95
.
All you have to do is change -94
to -95
in line 6.
如果您得到127减去94,那么您得到33,而您希望它是32.(正如Antti Haapala指出的那样).
If you get 127 and subtract 94, you get 33, and you want it to be 32. (As Antti Haapala pointed out).
如果您后退并使用 ord(char)-i<32
,如果为true,则添加95,您将得到Key#17.
If you go backwards and take ord(char) - i < 32
, and add 95 if its true, you'll get Key #17 instead.
这篇关于破解凯撒密码的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!