1

凯撒密码基本上将明文的每个字母移动一个固定数字。例如,如果使用密钥 2,则单词 Sourpuss 将被编码为 Uqwrtrwuu

文本只能包含可打印的 ASCII 字符(32-126,为了我们的目的)。实现破解此代码的算法。

我需要解密这个:“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?")

如您所见,我添加了一个\,但我认为这不会改变我的答案吗?

问题是它没有打印出正确的答案。有人可以告诉我我的代码有什么问题,或者只是指出我正确的方向。

4

2 回答 2

0

关键#78:恭喜!你可能会通过 CITS1401。

您所要做的就是更改-94-95第 6 行。

如果你得到 127 并减去 94,你得到 33,你希望它是 32。(正如 Antti Haapala 指出的那样)。

如果你倒退并取ord(char) - i < 32,如果它是真的加上 95,你会得到 Key #17。

于 2015-03-25T11:08:05.890 回答
0

这是一个可以加密和解密的代码。它甚至返回格式!(例如,资本,较低)

import time
def shift_dict(Caesar, Shift):
  dic_len = len(Caesar)
  Shift = Shift % dic_len
  list_dic = [(k,v) for k, v in iter(Caesar.items())]
  Shifted = {
    list_dic[x][0]: list_dic[(x - Shift) % dic_len][1]
    for x in range(dic_len)
  }
  return Shifted
def shift_dict2(Caesar, Shift):
  dic_len = len(Caesar)
  Shift = Shift % dic_len
  list_dic = [(k,v) for k, v in iter(Caesar.items())]
  Shifted = {
    list_dic[x][0]: list_dic[(x - Shift) % dic_len][-1]
    for x in range(dic_len)
  }
  return Shifted

UpperList = {
  "A":0,
  "B":1,
  "C":2,
  "D":3,
  "E":4,
  "F":5,
  "G":6,
  "H":7,
  "I":8,
  "J":9,
  "K":10,
  "L":11,
  "M":12,
  "N":13,
  "O":14,
  "P":15,
  "Q":16,
  "R":17,
  "S":18,
  "T":19,
  "U":20,
  "V":21,
  "W":22,
  "X":23,
  "Y":24,
  "Z":25
}
UpperCaesar = {
  "A":"A",
  "B":"B",
  "C":"C",
  "D":"D",
  "E":"E",
  "F":"F",
  "G":"G",
  "H":"H",
  "I":"I",
  "J":"J",
  "K":"K",
  "L":"L",
  "M":"M",
  "N":"N",
  "O":"O",
  "P":"P",
  "Q":"Q",
  "R":"R",
  "S":"S",
  "T":"T",
  "U":"U",
  "V":"V",
  "W":"W",
  "X":"X",
  "Y":"Y",
  "Z":"Z"
}
LowerList = {
  "a":0,
  "b":1,
  "c":2,
  "d":3,
  "e":4,
  "f":5,
  "g":6,
  "h":7,
  "i":8,
  "j":9,
  "k":10,
  "l":11,
  "m":12,
  "n":13,
  "o":14,
  "p":15,
  "q":16,
  "r":17,
  "s":18,
  "t":19,
  "u":20,
  "v":21,
  "w":22,
  "x":23,
  "y":24,
  "z":25

}
LowerCaesar = {
  "a":"a",
  "b":"b",
  "c":"c",
  "d":"d",
  "e":"e",
  "f":"f",
  "g":"g",
  "h":"h",
  "i":"i",
  "j":"j",
  "k":"k",
  "l":"l",
  "m":"m",
  "n":"n",
  "o":"o",
  "p":"p",
  "q":"q",
  "r":"r",
  "s":"s",
  "t":"t",
  "u":"u",
  "v":"v",
  "w":"w",
  "x":"x",
  "y":"y",
  "z":"z"
}
UpperList1 = {
  "A":0,
  "B":1,
  "C":2,
  "D":3,
  "E":4,
  "F":5,
  "G":6,
  "H":7,
  "I":8,
  "J":9,
  "K":10,
  "L":11,
  "M":12,
  "N":13,
  "O":14,
  "P":15,
  "Q":16,
  "R":17,
  "S":18,
  "T":19,
  "U":20,
  "V":21,
  "W":22,
  "X":23,
  "Y":24,
  "Z":25
}
UpperCaesar1 = {
  "A":"A",
  "B":"B",
  "C":"C",
  "D":"D",
  "E":"E",
  "F":"F",
  "G":"G",
  "H":"H",
  "I":"I",
  "J":"J",
  "K":"K",
  "L":"L",
  "M":"M",
  "N":"N",
  "O":"O",
  "P":"P",
  "Q":"Q",
  "R":"R",
  "S":"S",
  "T":"T",
  "U":"U",
  "V":"V",
  "W":"W",
  "X":"X",
  "Y":"Y",
  "Z":"Z"
}
LowerList1 = {
  "a":0,
  "b":1,
  "c":2,
  "d":3,
  "e":4,
  "f":5,
  "g":6,
  "h":7,
  "i":8,
  "j":9,
  "k":10,
  "l":11,
  "m":12,
  "n":13,
  "o":14,
  "p":15,
  "q":16,
  "r":17,
  "s":18,
  "t":19,
  "u":20,
  "v":21,
  "w":22,
  "x":23,
  "y":24,
  "z":25

}
LowerCaesar1 = {
  "a":"a",
  "b":"b",
  "c":"c",
  "d":"d",
  "e":"e",
  "f":"f",
  "g":"g",
  "h":"h",
  "i":"i",
  "j":"j",
  "k":"k",
  "l":"l",
  "m":"m",
  "n":"n",
  "o":"o",
  "p":"p",
  "q":"q",
  "r":"r",
  "s":"s",
  "t":"t",
  "u":"u",
  "v":"v",
  "w":"w",
  "x":"x",
  "y":"y",
  "z":"z"
}

Asker = int(input("Do you want to... 1. Encode, or 2. Decode? "))

if Asker == 1:
  Plaintext = str(input(""))
  OriginalShift = int(input(""))
  Shift = OriginalShift*-1
  UpperCaesar = shift_dict(UpperCaesar, Shift)
  LowerCaesar = shift_dict(LowerCaesar, Shift)
  Lister = []
  X = 0
  for i in range(len(Plaintext)):
    if Plaintext[X].isalpha():
      if Plaintext[X].isupper():
        Lister.append(UpperCaesar[Plaintext[X]])
      else:
        Lister.append(LowerCaesar[Plaintext[X]])
    else:
      Lister.append(Plaintext[X])
    X += 1
  print(*Lister, sep = "")
elif Asker == 2:
  Asker1 = int(input("Do you have the key (1), or not(2): "))
  if Asker1 == 1: 
    Plaintext = str(input(""))
    OriginalShift = int(input(""))
    Shift = OriginalShift*-1
    UpperCaesar = shift_dict(UpperCaesar, 26 - Shift)
    LowerCaesar = shift_dict(LowerCaesar, 26 - Shift)
    Lister = []
    X = 0
    for i in range(len(Plaintext)):
      if Plaintext[X].isalpha():
        if Plaintext[X].isupper():
          Lister.append(UpperCaesar[Plaintext[X]])
        else:
          Lister.append(LowerCaesar[Plaintext[X]])
      else:
        Lister.append(Plaintext[X])
      X += 1
    print(*Lister, sep = "")
  elif Asker1 == 2:
    Plaintext = str(input(""))
    OriginalShift = 0
    for i in range(26):
      UpperCaesar = shift_dict2(UpperCaesar, -1)
      LowerCaesar = shift_dict2(LowerCaesar, -1)
      Lister = []
      X = 0
      for i in range(len(Plaintext)):
        if Plaintext[X].isalpha():
          if Plaintext[X].isupper():
            Lister.append(UpperCaesar[Plaintext[X]])
          else:
            Lister.append(LowerCaesar[Plaintext[X]])
        else:
          Lister.append(Plaintext[X])
        X += 1
        time.sleep(0.01)
      print("With a shift of ", 25 - (OriginalShift*-1), ": ", *Lister, sep = "")
      OriginalShift -= 1
于 2021-03-10T17:21:18.180 回答