Vernam密码KeyError:0 Python

时间:2019-03-25 04:52:42

标签: python python-3.x cryptography

我试图使用python编程语言创建Vernam Cipher加密和解密技术。但是有时它可以工作,有时却不行。我不确定确切的错误是什么。仅收到Keyerror 0消息。

import string
import random

number_to_alphabet_dict = { 1:'a',2:'b',3:'c',4:'d',5:'e'
                           ,6:'f',7:'g',8:'h',9:'i',10:'j'
                           ,11:'k',12:'l',13:'m',14:'n',15:'o'
                           ,16:'p',17:'q',18:'r',19:'s',20:'t'
                           ,21:'u',22:'v',23:'w',24:'x',25:'y'
                           ,26:'z'}

alphabet_to_number_dict = {  'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5
                           , 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10
                           , 'k': 11, 'l': 12, 'm': 13, 'n': 14, 'o': 15
                           , 'p': 16, 'q': 17, 'r': 18, 's': 19, 't': 20
                           , 'u': 21, 'v': 22, 'w': 23, 'x': 24, 'y': 25
                           , 'z': 26}
def getMode():
    while True:
        print("Do you want to encrypt or decrypt the message")
        mode = str(input()).lower()
        if mode in 'encrypt e decrypt d'.split():
            return mode
        elif mode in 'exit quit'.split():
            break
        else:
            print("Enter either 'encrypt' or 'e' for Encryption or 'decrypt' or 'd' for Dycryption")
def getMessage():
    print("Enter the message:")
    message = str(input())
    message = message.lower()
    return message

def generateKey(message):
    key = ''.join([random.choice(string.ascii_letters) for n in range(len(message))])
    key = key.lower()
    return key

def getKey():
    print("Enter the key:")
    key = str(input())
    key = key.lower()
    return key

def cipherText(message,key):
    encrypted_message =''
    index = 0
    for symbol in message:
        temp = (alphabet_to_number_dict[symbol] + alphabet_to_number_dict[key[index]]) % 26
        encrypted_message = encrypted_message +number_to_alphabet_dict[temp]
        index = index + 1
        if index == len(key):
            index = 0
    return encrypted_message

def plainText(message,key):
    decrypted_message =''
    index = 0
    for symbol in message:
        temp = (alphabet_to_number_dict[symbol] - alphabet_to_number_dict[key[index]]) % 26
        decrypted_message = decrypted_message +number_to_alphabet_dict[temp]
        index = index + 1
        if index == len(key):
            index = 0
    return decrypted_message

while True:
    print("Do you want to encrypt or decrypt the message")
    mode = str(input()).lower()
    if mode in 'encrypt e decrypt d'.split():
        if mode[0]=='e':
            message = getMessage()
            key = generateKey(message)
            print("Plain Message is {0}".format(message))
            print("Encryption Key is {0}".format(key))
            cipher_text = cipherText(message,key)
            print("Encrypted Message is {0}".format(cipher_text))
        elif mode[0]=='d':
            message = getMessage()
            key = getKey()
            print("Cipher Message is {0}".format(message))
            print("Decryption Key is {0}".format(key))
            plain_text = plainText(message,key)
            print("Plain Message is {0}".format(plain_text))
        else:
            print("Enter either 'encrypt' or 'e' for Encryption or 'decrypt' or 'd' for Dycryption.")  
    elif mode in 'exit quit'.split():
            break

请帮助我。我是python的新手,无法修复此错误。在下面的scrrenshot中,您可以看到它有时起作用而有时不起作用。

enter image description here

1 个答案:

答案 0 :(得分:1)

设置临时变量时,将使用%26,它将返回0到25之间的整数。字典中的键范围是1到26。只要将温度设置为零,您的代码就会中断。您需要将字典中的键更改为0到25