如何定义凯撒密码的解密

how to define decryption of caeser cipher

好的,所以我错过了 class 并且正在尝试完成他们在那里所做的工作。其中一个问题是修复凯撒密码。我相信我已经修复了除最后一部分之外的所有内容,这当然是我坚持的部分。这就是我的。

#Change to T to lowercase in plaintext
#add : to the end of if statement      
def encrypt(plaintext, alphabet, key):       
        plaintext = plaintext.lower()
        cipherText = ""
        for ch in plaintext:
            idx = alphabet.find(ch)
            if idx >= 0 and idx <= 25:
                cipherText = cipherText + key[idx]
            else:
                cipherText = cipherText + ch
                #add return ciphertext
        return cipherText
#add def to decrypt line
def decrypt(cipherText, alphabet, key):
        plainText = ""
        for ch in cipherText:
            idx = key.find(ch)
            #add and idx <= 25 to if statement
            if idx >= 0 and idx <= 25:
                plaintext = plaintext + alphabet[idx]
            else:
                plaintext = plaintext + ch
        return plaintext
#got rid of def main
#have myCipher = encrypt
# define both myCipher and my plain to the encrypt and decrypt
alphabet = "abcdefghijklmnopqrstuvwxyz"
key = "nopqrstuvwxyzabcdefghijklm"
plaintext = input("Enter the text you want to encrypt: " )
myCipher = encrypt(plaintext, alphabet, key)
myPlain = decrypt(cipherText,alphabet, key)


print(myCipher)
print("Checking if decryption works: ")
print(myPlain)

当我 运行 代码时,它说 cipherText 未在

中定义
myPlain = decrypt(cipherText,alphabet, key)

我尝试了几种不同的选择,但我似乎比现在修复它更进一步。那么我可以在该行中定义 cipherText 的方法还是必须重做该行并将其更改为其他内容?

这是我尝试按照 LalolDublin 的建议更改 cipherText 时遇到的错误

 Traceback (most recent call last):
  File "C:\Users\David\Downloads\caeser (2).py", line 32, in <module>
    myPlain = decrypt(myCipher ,alphabet, key)
  File "C:\Users\David\Downloads\caeser (2).py", line 21, in decrypt
    plaintext = plaintext + alphabet[idx]
UnboundLocalError: local variable 'plaintext' referenced before assignment

您不能使用 cipherText,它只是该函数内的局部变量...

myCipher = encrypt(plaintext, alphabet, key)
myPlain = decrypt(myCipher ,alphabet, key)

好的,正如 LalolnDublin 所说,我需要放置 myCipher 而不是 cipherText,他是对的,但我还需要更改输入代码的位置,以便它可以正常工作。如果有人遇到和我一样的问题,这就是成品了。

key = "nopqrstuvwxyzabcdefghijklm"
plainText = input("Enter the text you want to encrypt: " )
alphabet = "abcdefghijklmnopqrstuvwxyz"
def encrypt(plainText, key, alphabet):
        plainText = plainText.lower()
        cipherText = ""
        for ch in plainText:
            idx = alphabet.find(ch)
            if idx >= 0 and idx <= 25:
                cipherText = cipherText + key[idx]
            else:
                cipherText = cipherText + ch
        return cipherText
myCipher = encrypt(plainText, key, alphabet)
def decrypt(myCipher, plainText, alphabet, key):
        plainText = ""
        for ch in myCipher:
            idx = key.find(ch)
            if idx >= 0 and idx <=25:
                plainText = plainText + alphabet[idx]
            else:
                plainText = plainText + ch
        return plainText
myPlain = decrypt(myCipher, plainText, alphabet, key)



print(myCipher)

print("Checking if decryption works: ")

print(myPlain)