Python Vigenere Cipher Encrypt 方法未正确加密

Python Vigenere Cipher Encrypt method not encrypting properly

我程序中的加密方法没有正确加密。我想我明白了为什么要使用调试模式;这是因为它将单词之间的空格读取为必须加密的内容。所以我尝试输入不带空格的消息,但仍然无法正确显示。

我认为问题出在带有密钥的 if 语句上。我试过注释行、更改语句、将 if 语句更改为 for 循环,但仍然不正确。

def main():                                            
    vig_square = create_vig_square()                   
    message = input("Enter a multi-word message with punctuation: ")
    input_key = input("Enter a single word key with no punctuation: ") 
    msg = message.lower()                              
    key = input_key.lower()                            
    coded_msg = encrypt(msg, key, vig_square)          
    print("The encoded message is: ",coded_msg)        
    print("The decoded message is: ", msg)  

def encrypt(msg,key,vig_square):                                       
    coded_msg = ""                                                     
    key_inc = 0                                                        
    for i in range(len(msg)):                                          
        msg_char = msg[i]                                              
        if key_inc == len(key)-1:                                      
            key_inc = 0                                                
        key_char = key[key_inc]                                        
        if msg_char.isalpha() and key_char.isalpha():                  
           row_index = get_row_index(key_char,vig_square)              
           col_index = get_col_index(msg_char,vig_square)              
           coded_msg = coded_msg+vig_square[row_index][col_index]      
        else:                                                          
            coded_msg = coded_msg + " "                                
        key_inc = key_inc+1                                            
    return coded_msg                                                   

def get_col_index(msg_char, vig_square):       
    column_index = ord(msg_char) - 97          
    return column_index                        

def get_row_index(key_char, vig_square):       
    row_index = ord(key_char) - 97             
    return row_index                           

def create_vig_square():                       
    vig_square = list()                        
    for row in range(26):                      
        next_row = list()                      
        chr_code = ord('a') + row              
        for col in range(26):                  
            letter = chr(chr_code)             
            next_row.append(letter)            
            chr_code = chr_code + 1            
            if chr_code > 122:                 
                chr_code = ord('a')            
        vig_square.append(next_row)            
    return vig_square  
main()

这个例子是给我们的:

Enter a multi-word message with punctuation: The eagle has landed.

Enter a single word key with no punctuation: LINKED

The encoded message is: epr oejwm ukw olvqoh.

The decoded message is: the eagle has landed.

但我的编码消息显示为:

epr iloyo sif plvqoh

你有两个错误:

首先,您没有使用密钥中的所有字符。更改以下行:

if key_inc == len(key)-1:
    key_inc = 0

if key_inc == len(key):
    key_inc = 0

其次,即使您处理消息中的非字母字符(例如空格),您也会移动键指针。仅当您对字符进行编码时才执行此操作,即进行以下更改:

if msg_char.isalpha() and key_char.isalpha():
    ...
    key_inc = key_inc+1   # Move this line here
else:
    ...