如何定义caeser密码的解密

时间:2016-04-19 01:29:37

标签: python-3.x caesar-cipher

好吧所以我错过了课程,并且正在努力完成他们在那里所做的工作。其中一个问题是修复一个caeser密码。我相信我已经修好了除了最后一部分,这当然是我坚持的部分。这就是我所拥有的。

#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,还是必须重做该行并将其更改为其他内容?

这是我在尝试更改cipherText时遇到的错误,因为LalolDublin建议

 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

2 个答案:

答案 0 :(得分:1)

你不能使用cipherText它只是在该函数中的局部变量......

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

答案 1 :(得分:0)

好的,因为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)