凯撒密码python解密

2024-10-06 11:49:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我在尝试解码加密邮件时遇到问题。它几乎可以完美地解密所有内容,但某些小写字母无法正确解密。虽然当它们是大写的时候它就起作用了。我做错了什么

def decrypt():
        print("Heres our Caeser Cipher Decryption program.")
        encrpMsg = input("Enter the message you would like to decrypt: ").strip()
        print()
        decrpKey = int(input("Enter key to decrypt, a number 0-25: "))
    
        decryptedMessage = ""
    
        for i in range(len(encrpMsg)):
            if ord(encrpMsg[i]) == 32:
                decryptedMessage += chr(ord(encrpMsg[i]))
    
            elif ((ord(encrpMsg[i]) - decrpKey) < 97 and ((ord(encrpMsg[i]) - decrpKey) > 90)):
                temp = (ord(encrpMsg[i]))
                decryptedMessage += chr(temp)
    
            elif (ord(encrpMsg[i]) - decrpKey) <65:
                temp = (ord(encrpMsg[i]) - decrpKey) + 26
                decryptedMessage += chr(temp)
    
            else:
                decryptedMessage += chr(ord(encrpMsg[i]) - decrpKey)
    
        print(decryptedMessage)

关键字:13 输出: ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmTUVWXYZhijklm 预期产出: ABCDEFGHIJKLMNOPQRSTUVXYZ ABCDEFGHIJKLMNOPQRSTUVXYZ


Tags: toinput邮件解码tempprintenterelif