我一直很难弄清楚如何在我的程序中编写和实现几个函数。调用get\u col\u index和get\u row\u index方法以及get\u col\u index和get\u row\u index方法的encrypt方法。主方法调用encrypt方法,并传递消息、密钥和vignereu平方。Vignere矩阵密码已经在一个单独的函数中生成。然后,encrypt方法调用get\u col\u index和get\u row\u index方法对消息进行加密,跳过所有不是字母的内容。你知道吗
我的问题很大一部分是弄清楚如何使用get\u row\u index和get\u col\u index方法对消息进行编码。对于get\u col\u index,我们通过消息和vigenere square,它返回消息中每个字母的列索引。对于get\u row\u索引,我们通过key和vigenere square,它返回key的每个字母的行索引。。我一直在尝试为这些函数使用for循环,通常会返回消息(未加密)、带有随机字母的列表或错误。这是我的最新尝试,我尝试在encrypt方法中使用append,但它只是将消息和密钥添加到一个列表中。你知道吗
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()
print("The encoded message is: ",encrypt(msg, key, vig_square))
print("The decoded message is: ")
def encrypt(msg, key, vig_square):
msg_char = msg
key_char = key
coded_msg = list()
coded_msg.append(get_col_index(msg_char, vig_square))
coded_msg.append(get_row_index(key_char, vig_square))
return coded_msg
def get_col_index(msg_char, vig_square):
column_index = ""
for i in range(len(vig_square)):
column_index = msg_char
i += 1
return column_index
def get_row_index(key_char, vig_square):
row_index = ""
for i in range(len(vig_square)):
row_index = key_char
i += 1
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()
我希望输出如下:
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.
vignere密码有一些很好的特性,因为vignere平方在水平和垂直方向上都是循环的,这意味着查找可以这样做:
这会将字母a-z赋给数字0-25,将列和行字母相加,如果值大于“z”(25),则使用模运算进行循环
这意味着可以通过以下方式进行加密:
类似地,解密查找可以使用减法来完成,利用了在python中使用负数循环的模这一事实,正如我们希望的那样。这将解密为:
把所有这些放在一起,你就能做到:
相关问题 更多 >
编程相关推荐