我正在为受控评估的一部分编写一个Caesar Cipher代码。我建立了一个功能齐全的程序,我认为我已经怀疑了但是在改变了一些事情之后我去检查了一切都出了问题!
这段代码非常凌乱,但我现在对此编码感到有点厌恶,并且已经上网以获得别人的观点。
代码:
answer ="C"
while answer == "C":
lettersList=['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','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']
def menu():
userChoice=input("Would you like to encrypt or decrypt a message? E or D.\n").lower()
while userChoice != "e" and userChoice != "d":
print("Invalid.")
userChoice=input("Would you like to encrypt or decrypt a message? E or D.\n").lower()
print("\n")
return userChoice
def getPlaintext():
plaintext= input("Please enter the message you would like encrypted/decrypted\n").lower()
while plaintext.isalpha() == False:
print("Invalid")
plaintext=input("Please enter the message you would like encrypted/decrypted\n").lower()
print("\n")
return plaintext
def getKey():
key=int(input("Please enter a key. 1-26\n"))
while key > 26 or key < 1:
print("Invalid.")
key=int(input("Please enter a key. 1-26\n"))
print("\n")
return key
def encryptText(plaintext,key):
characterNumber = 0
newMessage = ""
for characters in plaintext:
character = plaintext[characterNumber]
characterPosition = lettersList.index(character)
newPosition=character+key
newLetter = lettersList[newPosition]
newMessage = (newMessage+newLetter)
characterNumber= characterNumber+1
print(newMessage)
def decryptText(plaintext,key):
characterNumber = 0
newMessage = ""
for characters in plaintext:
character = plaintext[characterNumber]
characterPosition = lettersList.index(character)
print(characterPosition)
newPosition=characterPosition-key
newLetter = lettersList[newPosition]
newMessage = (newMessage+newLetter)
characterNumber= characterNumber+1
newMessage = (newMessage.lower())
print(newMessage)
userChoice=menu()
plaintext=getPlaintext()
key=getKey()
if userChoice == "e":
encryptText(plaintext,key)
elif userChoice == "d":
decryptText(plaintext,key)
print(newMessage)
答案 0 :(得分:0)
在decypryptText中,newPosition可以为负数。在这种情况下,您应该环绕整个字母表。最好的方法是使用%运算符:
newPosition = (characterPosition - key) % len(lettersList)
顺便说一句,在encryptText中使用它也可以让lettersList只有一个字母副本,而不是你使用的两个副本。
在这段代码中还有其他一些东西可以打磨,但它们并不像这个那么重要。
编辑:如果你想将它大写为decryptText,你可以使decryptText返回newMessage.upper()。以同样的方式,您可以使encryptText返回newMessage.lower()。
答案 1 :(得分:0)
您的代码中有一些简单的拼写错误,导致无法正常运行。 for characters in plaintext:
应为for character in plaintext:
- 在您的代码中创建一个新变量characters
,不会在任何地方使用。
带注释的正确加密函数如下所示:
def encryptText(plaintext, key):
newMessage = ""
for character in plaintext: # 'character' holds each letter from 'plaintext'
characterPosition = lettersList.index(character)
newPosition = characterPosition + key # shift the index, not the character!
newLetter = lettersList[newPosition]
newMessage = newMessage + newLetter # append new letter to new message
print(newMessage)
为了使lettersList
上的每个字符只出现一次(即长度为26),你需要检查移位的索引是否超出范围;也就是说,该字符不大于26.如果是,则减去26,就像这样
newPosition = characterPosition + key
if newPosition >= len(lettersList) then:
newPosition -= len(lettersList)
或按建议使用模运算符。 这应该让你了解如何修改解密函数到&#34;环绕&#34;同样。