Program to crack Caesar cipher

前提是你 提交于 2021-01-29 00:16:43

问题


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

The text can contain only the printable ASCII characters (32-126, for our purposes). Implement an algorithm for cracking this code.

I need to decrypt this: "T! x$r&'}r&z! %21j!'1~zxy&1"r%%1TZedBEAB?"

Here is my code:

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.


回答1:


Key #78: Congratulations! You might pass CITS1401.

All you have to do is change -94 to -95 in line 6.

If you get 127 and subtract 94, you get 33, and you want it to be 32. (As Antti Haapala pointed out).

If you go backwards and take ord(char) - i < 32, and add 95 if its true, you'll get Key #17 instead.



来源:https://stackoverflow.com/questions/29252574/program-to-crack-caesar-cipher

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!