社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

python vigenere密码加密方法未正确加密

Chey • 5 年前 • 1583 次点击  

我的程序中的加密方法未正确加密。我想我已经弄明白了为什么要使用调试模式,因为它读取单词之间的空格作为必须加密的内容。所以我试着键入一条没有空格的消息,但仍然没有正确显示出来。

我想问题是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社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/39497
 
1583 次点击  
文章 [ 1 ]  |  最新文章 5 年前