凯撒密码与关键字 - Python

时间:2016-03-22 09:07:04

标签: python encryption caesar-cipher

我是学校的学生,目前正在使用Python上的Caesar Cipher作为编程任务。第一项任务是创建一个程序,询问用户消息,偏移量,代码将根据用户的决定加密或解密。我在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)

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

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

2 个答案:

答案 0 :(得分:0)

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"))

答案 1 :(得分:0)

这应该有效:

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开始。