凯撒密码不加密

Caesar Cipher Not Encrypting

不确定为什么我的代码没有正确加密密码 例如:“你好,你好吗”是“2z669 29g vbz i9e”,我的代码可以很好地解密它。如果我尝试加密它,我会得到: “e]z } g vbz I e”。我假设它与 +21 模数 len(ALPHABET) 有关 我只是想如果解密是减法我需要添加以反转密码,但我想我错了任何帮助将不胜感激!

ALPHABET = "abcdefghijklmnopqrstuvwxyz0123456789."

def encrpytstr(message):
    newString = ''
    #loop to assign new alphabet value
    for index in range(0, len(message)):
        oneLetter = message[index]
        
        #assinging new alphabet value
        newAlphabetValue = (ALPHABET.find(oneLetter.lower()) + 21) % len(ALPHABET) + (ord('A') if oneLetter.isupper() else ord('a'))
        
        #checking for white spaces
        if oneLetter == ' ':
            newAlphabetValue = 32
        
        #creating the new string
        newString = newString + chr(newAlphabetValue)
    
    return newString


def decrpytstr(message):
    newString = ''
    #loop to assign new alphabet value
    for index in range(0, len(message)):
        oneLetter = message[index]
        
        #assinging new alphabet value
        newAlphabetValue = (ALPHABET.find(oneLetter.lower()) - 21) % len(ALPHABET) + (ord('A') if oneLetter.isupper() else ord('a'))
        
        #checking for white spaces
        if oneLetter == ' ':
            newAlphabetValue = 32
        
        #creating the new string
        newString = newString + chr(newAlphabetValue)
    
    return newString


def main():
    message = input("Enter text: ")
    choice = input("(E)ncrypt or (D)ecrypt?: ").upper()
    
    if choice == "E":
        encrpytstr(message)
        newString = encrpytstr(message)
        print(newString)
        input("Press ENTER to exit")
    
    elif choice == "D":
        decrpytstr(message)
        newString = decrpytstr(message)
        print(newString)
        input("Press ENTER to exit")
    
    else:
        print("Invalid Input Try Again!")
        input("Press ENTER to exit")
main()

添加几个 print 语句揭示了您的逻辑存在相当根本的问题。

        #assinging new alphabet value
        newAlphabetValue = (ALPHABET.find(oneLetter.lower()) + 21) % len(ALPHABET) + (ord('A') if oneLetter.isupper() else ord('a'))
        print("input: %s new: %s" % (oneLetter, chr(newAlphabetValue)))
        print("(found %i +21 %i)" % (ALPHABET.find(oneLetter.lower()), ALPHABET.find(oneLetter.lower())+21))

生产

input: H new: ]
(found 7 +21 28)
input: e new: z
(found 4 +21 25)
input: l new: 
(found 11 +21 32)
input: l new: 
(found 11 +21 32)
input: o new: „
(found 14 +21 35)
input: , new: u
(found -1 +21 20)
input:   new: u
(found -1 +21 20)
input: h new: }
(found 7 +21 28)
input: o new: „
(found 14 +21 35)
input: w new: g
(found 22 +21 43)
input:   new: u
(found -1 +21 20)
input: a new: v
(found 0 +21 21)
input: r new: b
(found 17 +21 38)
input: e new: z
(found 4 +21 25)
input:   new: u
(found -1 +21 20)
input: y new: i
(found 24 +21 45)
input: o new: „
(found 14 +21 35)
input: u new: e
(found 20 +21 41)
input: ? new: u
(found -1 +21 20)

请注意 find 的结果通常是 -1 以及它是如何抛出一切的?

我不确定到底出了什么问题,因为不清楚您认为这应该如何工作。或许可以简化代码,以便更清楚地了解正在发生的事情以及原因。

绕道index也大可不必; Python 完全能够直接遍历列表中的项目。顺便说一句,如果可以的话,可能会避免重复将内容附加到字符串的末尾。

def encrpytstr(message):
    # Use a list for speed
    newletters = []
    for letter in message:
        upcase = letter.isupper()
        letter = letter.lower()
        index = ALPHABET.find(letter)
        if index > -1:
            newindex = (index + 21) % len(ALPHABET)
            # print("input: %s (%i, %i) new: %s (%i, %i)" % (letter, index, ord(letter), ALPHABET[newindex], newindex, ord(ALPHABET[newindex])))
            letter = ALPHABET[newindex]
            if upcase:
                letter = letter.upper()
        newletters.append(letter)
    # now finally do a slow string join
    return ''.join(newletters)

我留下了注释掉的 print 语句,以防您想用新代码重复调试步骤。

新代码简单地逐字保留不在ALPHABET中的任何字符;如果您不想更改,应该很容易看到要更改的内容。

最后,你是不是故意拼错了“encrpyt”和“decrpyt”?当您第一次看到它时,它有点幽默,但是一旦您的代码增长,它就会在后面咬住您。避免函数名称中奇怪的拼写错误。