2017-10-22 227 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