我正在研究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
@ user2357112是的,因爲我的消息將是大寫的,我需要我的子字符串也爲大寫所以我使用'.upper()' –
此外,消息是否不包含任何其他通配符,如#$〜? – skrubber
請不要更改您的標題或您的問題的內容 –