在一个Caeser密码工作,但是文本没有被返回,因为它应该?

2024-09-30 16:35:13 发布

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

每当我输入编码“hello world”的移位量为1时,就会得到“ifmmp xpsme1”,这是正确的。然而,每当我试图解码它,它给我“hello9worldJ”这显然是不正确的。你知道吗

def encryption(string, shift):
    cipher = ''
    for char in string:
        if char == ' ':
            cipher = cipher + char
        elif char.isupper():
            cipher = cipher + chr((ord(char) + shift - 65) % 26 + 65)
        else:
            cipher = cipher + chr((ord(char) + shift - 97) % 26 + 97)
    shift = str(shift)

    return cipher + shift


def cipherDecrypt(word, key):
    decryptString = ""
    for i in word:
        charvalue = ord(i) - key
        if charvalue < 97:
            charvalue = ord("z") - (96-charvalue)
            decryptString += chr(charvalue)
        elif charvalue == 32:
            decryptString += " "
        else:
            decryptString += chr(charvalue)
    return decryptString

def main():
    decision = ""
    while decision != "stop":
        decision = input("Would you like to 'encode' or 'decode' or 'stop': ")
        if decision == "encode":
            text = input("Enter a string to encode: ")
            num = int(input("Enter your number to shift: "))
            print("Original input: ", text)
            print("After encryption: ", encryption(text, num))
        elif decision == "decode":
            text = input("Enter a string to decode: ")
            num = int(input("Enter your number to shift: "))
            print("Original input: ", text)
            print("After encryption: ", cipherDecrypt(text, num))


main()

Tags: totextinputstringshiftnumencryptioncipher
2条回答

出现这个问题的原因是,当您到达“(ord(" ") == 32)”时,if条件得到满足,因此您得到的是9而不是空格。你知道吗

一个非常简单的解决方案: 将if改为:
if charvalue < 97 and charvalue != ord(" ") - key:

和elif to:
elif charvalue == ord(" ") - key

之所以使用- key部分,是因为在循环中将charvalue设置为ord(i) - key。在减去键之后,您需要检查每个大小写,这意味着在您的情况下,如果charvalue == 31(32-1),您应该检查空格

在你做的时候

        elif charvalue == 32:
            decryptString += " "

您已经将charvalue设置为

        charvalue = ord(i) - key

但是看看你原来的编码字符串:空间在编码时变成了空间,但此时你已经“解码”了一个空间,然后才检查它是什么。你知道吗

你必须这样做:

def cipherDecrypt(word, key):
    decryptString = ""
    for i in word:
        charvalue = ord(i) - key
        if i == 32:  # Check if the character is a space BEFORE decoding it
            decryptString += " "
        elif charvalue < 97:
            charvalue = ord("z") - (96-charvalue)
            decryptString += chr(charvalue)
        else:
            decryptString += chr(charvalue)
    return decryptString

但是,仍然存在一些细微的错误;更好的检查是验证每个字符是否可以在继续之前解码:

def cipherEncrypt(word, key):
    cipher = ''
    for char in string:
        if char.isupper():
            cipher += chr((ord(char) + shift - 65) % 26 + 65)
        elif char.islower():
            cipher += chr((ord(char) + shift - 97) % 26 + 97)
        else:
            cipher += char
    return cipher

这样,你就可以对字母进行加密并传递其他信息。你知道吗

另外,考虑到凯撒密码的性质,cipherDecrypt的定义很简单:

def cipherDecrypt(word, key):
    return cipherEncrypt(word, -key)

相关问题 更多 >