带关键字的凯撒密码 - Python

Caesar Cipher with a Keyword - Python

我是一名在校学生,目前正在 Python 上进行凯撒密码编程作业。第一项任务是创建一个程序,要求用户提供消息、偏移量,代码将根据用户的决定进行加密或解密。我在 Python 方面一点经验都没有,因此我不是最擅长的,所以这是我的代码,相当简单:

message = input("What would you like your message to be? ")

key = int(input("What would you like your offset to be? ")) 

count = 0
while count == 0:
    mode = str(input("Would you like to encrypt or decrypt? "))
    if mode == "encrypt" or mode == "Encrypt" or mode == "E" or mode == "e":
        count = count + 1
    elif mode == "decrypt" or mode == "Decrypt" or mode == "D" or mode == "d":
        count = count + 1
    else:
        print("You need to enter in a valid answer. Please try again. ")  

letters = "abcdefghijklmnopqrstuvwxyz" 

translated_message = ""

for character in message:
    if character in letters:
       number = letters.find(character)
       if mode == "encrypt" or mode == "Encrypt" or mode == "E" or mode == "e":
           number = number + key
       elif mode == "decrypt" or mode == "Decrypt" or mode == "D" or mode == "d":
           number = number - key



       if number >= len(letters):
            number = number - len(letters)
       elif number < 0:
            number = number + len(letters)


       translated_message = translated_message + letters[number]

    else:
         translated_message = translated_message + character

print(translated_message)

现在下一个任务是创建一个带有关键字的凯撒密码,该关键字根据需要重复多次以适合用户输入的消息。因此,如果我有一个用户输入 "the password" 作为他们的消息,并输入 "key" 作为他们的关键字,那么 key 将重复多次以适合文本,并且程序应该将数值加在一起以创建新的加密或解密的消息。例如,翻译后的消息的第一个字母将是 Y,即 10(K) + 15(P) = 25(Y)。

我试图通过添加此功能来扩展我的原始代码,但我无法这样做。如果有人能够帮助我使用此代码并对其进行扩展以添加此功能,将不胜感激。谢谢你的帮助。

def encrypt(text, key):
    cryptotext = ""
    keylen = len(key)
    for i, char in enumerate(text):
        if char == " ":
            cryptotext += " "
        else:
            cryptotext += chr(((ord(char)-97+ord(key[i%keylen])-96)%26)+97)
    return cryptotext

def decrypt(ciphretext, key):
    text = ""
    keylen = len(key)
    for i, cichar in enumerate(ciphretext):
        if cichar == " ":
            text += " "
        else:
            text += chr(((ord(cichar)-ord(key[i%keylen])-2)%26)+97)
    return text

print(encrypt("abcdefghijklmnopqrstuvwxyz", "secret"))
print(decrypt("tgfvjzzmlbpffsrhvllyxnbrre", "secret"))

这应该有效:

message = input("What would you like your message to be? ")

key = input("What would you like your offset to be? ")

count = 0
while count == 0:
    mode = str(input("Would you like to encrypt or decrypt? "))
    if mode == "encrypt" or mode == "Encrypt" or mode == "E" or mode == "e":
        count = count + 1
    elif mode == "decrypt" or mode == "Decrypt" or mode == "D" or mode == "d":
        count = count + 1
    else:
        print("You need to enter in a valid answer. Please try again. ")  

letters = "abcdefghijklmnopqrstuvwxyz" 

translated_message = ""

index = 0
for character in message:
    if character in letters:
       number = letters.find(character)
       if mode == "encrypt" or mode == "Encrypt" or mode == "E" or mode == "e":
           number = number + (ord(key[index]) - ord('a'))
       elif mode == "decrypt" or mode == "Decrypt" or mode == "D" or mode == "d":
           number = number - (ord(key[index]) - ord('a'))
       index = index+1
       index = index % len(key)


       if number >= len(letters):
            number = number - len(letters)
       elif number < 0:
            number = number + len(letters)


       translated_message = translated_message + letters[number]

    else:
         translated_message = translated_message + character

print(translated_message)

还要注意:10(K) + 15(P) = 25(Z),因为索引从 0 开始。