2017-10-22 92 views
0

我正在研究ceaser密码的变体。当我输入加密的消息时,我的decrypt()函数应该保持解密不同的旋转值,直到一个常用的单词,例如。弹出“the”或“time”或“attack”。出于测试目的,我使用诸如“继续攻击基础”之类的消息,因此当any(word.upper() in plainText for word in subStrings)部件运行时,它应该返回true,但它不会。我当前的代码如下:any()不返回true?

def decrypt(encryptedMessage): 
    alphanumericAlphabet = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] # List of every letter in the alphabet 

    message = (str(encryptedMessage).strip("\n")).upper() 

    subStrings = ["The", "Base", "Proceed", "North", "West", "South", "East", "Hours", "Dawn", "Attack", "Defend", "Shoot", "Bearing", "Enemy", "Position", "Move", "That", "Fast", "Time", "Rise", "Loss", "Win", "Victory"] 
    plainText = "" 
    messageFound = False 
    rotationValue = 0 

    while messageFound != True: 
     if any(word.upper() in plainText for word in subStrings): 
      messageFound = True 
     else: 
      plainText = "" 
      for character in message: 
       cipherIndex = alphanumericAlphabet.index(character.upper()) 
       plainIndex = cipherIndex - rotationValue 
       if plainIndex < 0: 
        plainIndex += 36 
       plainText += alphanumericAlphabet[plainIndex] 
     rotationValue += 1 

    print "Decrypted Message:", plainText, "\n", "Rotation:", rotationValue 

    return plainText, rotationValue 
+0

@ user2357112是的,因为我的消息将是大写的,我需要我的子字符串也为大写所以我使用'.upper()' –

+0

此外,消息是否不包含任何其他通配符,如#$〜? – skrubber

+0

请不要更改您的标题或您的问题的内容 –

回答

0

添加空间alphanumericdigit列表这个角色也被处理或剥离空白:

def decrypt(encryptedMessage): 
    alphanumericAlphabet = [" ","A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] # List of every letter in the alphabet 

    message = (str(encryptedMessage).strip("\n")).upper() 

    subStrings = ["The", "Base", "Proceed", "North", "West", "South", "East", "Hours", "Dawn", "Attack", "Defend", "Shoot", "Bearing", "Enemy", "Position", "Move", "That", "Fast", "Time", "Rise", "Loss", "Win", "Victory"] 
    plainText = "" 
    messageFound = False 
    rotationValue = 0 

    while messageFound != True: 
     if any(word.upper() in plainText for word in subStrings):   
      messageFound = True 
     else: 
      plainText = "" 
      for character in message: 
       cipherIndex = alphanumericAlphabet.index(character) 
       plainIndex = cipherIndex - rotationValue 
       if plainIndex < 0: 
        plainIndex += 36 
       plainText += alphanumericAlphabet[plainIndex] 
     rotationValue += 1 

    print("Decrypted Message:", plainText, "\n", "Rotation:", rotationValue) 
    return plainText, rotationValue 
decrypt("Proceed to attack the base") 
Decrypted Message: PROCEED TO ATTACK THE BASE 
Rotation: 2 
Out[1852]: 
('PROCEED TO ATTACK THE BASE', 2) 
+0

为你的加密消息下面,应该是什么解密。我得到(“R3SJ1WTYFYNTSHNUMJWITJXSTYMF0JYTHMJHPKTWSZRGJWXGJHFZXJYMJ3MF0JGJJSFIIJIYTYMJFQUMFGJYFWWF3YMJSZRGJWX5YMWTZLMYTEMF0JGJJSFUUJSIJIYTYMJFQUMFGJYFWWF3YMNXRFPJXYMJFWWF38BHMFWFHYJWXQTSLNSXYJFITK7B1MJSNJSHW3UYR3HMFWFHYJWNTSQ3MF0JYTHMJHPYTXJJNKNYNXNSR3J2YJSIJIFQUMFGJYYMJSNKNSINYXNSIJ2YMJSNFIIR3WTYFYNTS0FQZJKNSFQQ3NYFPJYMJRTIZQTTWWJRFNSIJWFKYJWIN0NXNTSG38BYMNXYFPJXHFWJTKFS31WFUUNSLFWTZSIYMFYMFXYTGJITSJST1QJYZXXJJNK3TZMNLMQ3YWFNSJIUWTLWFRRJWXHFSHWFHPNYLTTIQZHP”, 14)你还需要展开子列表。 – skrubber

0

我认为这个问题是在这条线

cipherIndex = alphanumericAlphabet.index(character) 

你必须检查的character.upper()指数作为alphanumericAlphabet只包含大写字符。

+0

但是字符已经是上层了,因为message =(str(encryptedMessage).strip(“\ n”))。upper()' –

+0

它已经是上层的 – skrubber

+0

你说得对,对不起。 但'alphanumericAlphabet'也不包含''“',既不会从邮件中删除空格。所以,当你尝试像解密(“继续攻击基地”),它实际上会失败,不是吗? – SaturnFromTitan