Python 正在使用Caeser密码,但是文本没有按应有的方式返回?
每当我输入编码“hello world”的移位值为1时,就会得到正确的“ifmmp xpsme1”。然而,每当我试图解码它,它给我“hello9worldJ”,这显然是不正确的Python 正在使用Caeser密码,但是文本没有按应有的方式返回?,python,Python,每当我输入编码“hello world”的移位值为1时,就会得到正确的“ifmmp xpsme1”。然而,每当我试图解码它,它给我“hello9worldJ”,这显然是不正确的 def encryption(string, shift): cipher = '' for char in string: if char == ' ': cipher = cipher + char elif char.isupper():
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()
def加密(字符串、移位):
密码=“”
对于字符串中的字符:
如果char='':
密码=密码+字符
elif char.isupper():
密码=密码+chr((ord(char)+shift-65)%26+65)
其他:
密码=密码+chr((ord(char)+shift-97)%26+97)
shift=str(shift)
返回密码+移位
def密码解密(字、密钥):
decryptString=“”
对于我来说,用词来说:
charvalue=ord(i)-键
如果charvalue<97:
charvalue=ord(“z”)-(96 charvalue)
解密字符串+=chr(charvalue)
elif charvalue==32:
解密字符串+=“”
其他:
解密字符串+=chr(charvalue)
返回解密字符串
def main():
decision=“”
当决定“停止”:
决策=输入(“您想‘编码’还是‘解码’或‘停止’:”)
如果决策==“编码”:
text=输入(“输入要编码的字符串:”)
num=int(输入(“输入要移位的数字:”)
打印(“原始输入:”,文本)
打印(“加密后:”,加密(文本,数字))
elif决策==“解码”:
text=输入(“输入要解码的字符串:”)
num=int(输入(“输入要移位的数字:”)
打印(“原始输入:”,文本)
打印(“加密后:”,密码解密(文本,num))
main()
出现此问题的原因是,当您使用“(ord(“”==32)
”时,if条件得到满足,因此您得到的是9而不是空格
一个非常简单的解决方案:
将if更改为:如果charvalue<97且charvalue!=ord(“”)键:
和elif到: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
这样,您就可以对字母进行加密,并传递任何其他内容
另外,考虑到凯撒密码的性质,cipherDecrypt
的定义并不重要:
def cipherDecrypt(word, key):
return cipherEncrypt(word, -key)
elif charvalue==32
将永远不会执行,因为如果charvalue<97,则该情况也匹配。解密例程应按照与加密例程相同的顺序执行相同的测试,只需减去移位而不是相加。事实上,写一个函数…使用移位(正数或负数)进行编码,使用相反的符号进行解码。
def cipherDecrypt(word, key):
return cipherEncrypt(word, -key)