使用感叹号加密Caesar密码函数

时间:2018-05-24 03:25:39

标签: python encryption caesar-cipher

有人可以帮我制作一个加密消息的功能,其中包含消息中的感叹号。例如:HOWDY!你好。 截至目前,我的函数(HOWDY!Hello。)的输出是

Your translated text is:
MTBIDEEMJQQTE

真的应该是:

Your translated text is:
MTBID! Mello.

我的完整代码:

alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
message = ""
encryptedmessage = " "
keynumber = 0

def encrypt():
 global message
 global encryptedmessage
 global keynumber
 print()
 print()
 message = str(input("Enter your message:"))
 print()
 print()
 keynumber = int(input("Enter the key number (1-26)"))
 print()
 print()
 for i in message:
  position = alphabet.find(i)
  newposition = (position+5)%26
  encryptedmessage +=alphabet[newposition]
 print("Your translated text is:")
 print(encryptedmessage)


def decrypt():
 global message
 global encryptedmessage
 global keynumber
 message = str(input("Enter your message:"))
 keynumber = int(input("Enter the key number (1-26)"))


action = input("Do you wish to encrypt or decrypt a message?")

if action == "encrypt":
 encrypt()
if action == "decrypt":
 decrypt()

1 个答案:

答案 0 :(得分:1)

如果package.json不在"plain javascript",则{p> alphabet.find(i)将为-1。发生这种情况时,您应该只将i复制到加密邮件而不是对其进行编码。

alphabet