我在加密或解密消息时试图保持标点符号不变
# encryption
message = input("Enter a message to be encrypted: ") # user inputs message to be encrypted
offset = int(input ("Enter an offset: ")) # user inputs offset
print ("\t")
encrypt = " "
for char in message:
if char == " ":
encrypt = encrypt + char
elif char.isupper():
encrypt = encrypt + chr((ord(char) + offset - 65) % 26 + 65) # for uppercase Z
else:
encrypt = encrypt + chr((ord(char) + offset - 97) % 26 + 97) # for lowercase z
print ("Your original message:",message)
print ("Your encrypted message:",encrypt)
print ("\t")
如果我尝试使用标点符号(偏移量为 8)加密消息,则输出的示例如下:
Your original message: Mr. and Mrs. Dursley, of number four Privet Drive, were proud to say that they were perfectly normal, thank you very much.
Your encrypted message: Uzj ivl Uzaj Lczatmgh wn vcujmz nwcz Xzqdmb Lzqdmh emzm xzwcl bw aig bpib bpmg emzm xmznmkbtg vwzuith bpivs gwc dmzg uckp
我怀疑由于chr(ord(char))功能的原因,该程序正在将标点符号更改为字母 。
有什么方法可以在不过多更改代码的情况下将实际标点添加到加密消息中?我真的很感激任何帮助,谢谢!
慕莱坞森
繁星点点滴滴
相关分类