【问题标题】:Python Vigenere Cipher Encrypt method not encrypting properlyPython Vigenere Cipher Encrypt 方法未正确加密
【发布时间】:2020-01-07 21:09:37
【问题描述】:

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

我认为问题在于带有键的 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

【问题讨论】:

    标签: python encryption vigenere


    【解决方案1】:

    你有两个错误:

    首先,您不要使用键中的所有字符。更改以下行:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-02
      • 2016-06-13
      • 2021-09-02
      • 1970-01-01
      • 1970-01-01
      • 2016-02-10
      相关资源
      最近更新 更多