-4

我正在为受控评估的一部分编写凯撒密码。我建立了一个功能齐全的程序,我以为我搞砸了,但是在改变了一些事情之后,我回去检查了一下,一切都出错了!

代码很不整洁,但我现在有点厌倦了编码,并且已经上网了解其他人的观点。

代码:

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)
4

2 回答 2

0

在 decypryptText 中,newPosition 可以为负数。在这种情况下,您应该环绕整个字母表。最好的方法是使用 % 运算符:

newPosition = (characterPosition - key) % len(lettersList)

顺便说一句,在 encryptText 中使用它也可以让您只制作 lettersList 字母表的一个副本,而不是您使用的两个副本。

这段代码中还有其他一些可以完善的地方,但它们没有这个重要。

编辑:如果你想为decryptText大写,你可以让decryptText只返回newMessage.upper()。同理,可以让 encryptText 返回 newMessage.lower()。

于 2015-03-08T20:45:01.553 回答
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)

或按照建议使用模运算符。这应该让您了解如何修改解密函数以“环绕”。

于 2015-03-08T21:05:46.783 回答