Python-Vigenere密码加密方法加密不正确

2024-05-19 12:52:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我的程序中的加密方法加密不正确。我想我知道为什么使用调试模式了;这是因为它读取单词之间的空格作为它必须加密的内容。所以我试着在没有空格的情况下输入一条消息,但仍然没有正确输出

我想问题是带键的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

Tags: thekeymessageindexifdefcodemsg
1条回答
网友
1楼 · 发布于 2024-05-19 12:52:42

您有两个错误:

首先,你不能使用所有的字符。更改以下行:

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:
    ...

相关问题 更多 >