这是用于像使用公平密码一样对邮件进行加密的代码。
def encrypt(input_message):
r1=r2=c1=c2=0
play = playfair()
listed_ip = list(input_message)
j=1
listed_op=list('#####################################################################################################################################################')
for i in range(len(listed_ip)-1):
first_letter = listed_ip[i]
second_letter = listed_ip[j]
if first_letter =='j':
listed_ip[i] = 'i'
if second_letter == 'j':
listed_ip[j] = 'i'
for row in range(5):
for col in range(5):
if play[row][col] ==first_letter:
r1=row
c1=col
if play[row][col] == second_letter:
r2=row
c2=col
if r1 == r2:
listed_op[i] = play[r1][(c1+1)%5]
listed_op[j] = play[r2][(c2 + 1) % 5]
elif c1 ==c2:
listed_op[i] = play[(r1+1)%5][c1]
listed_op[j] = play[(r2+1)%5][c2]
else:
listed_op[i]=play[r1][c2]
listed_op[j]=play[r2][c1]
i+=2
j+=2
output_text = ''.join(listed_op)
print(output_text)
encrypt('Hithisisvishal')
我应该得到一个输出,该输出会更改此输入消息,其中根据每个字母在矩阵中的位置,将每个字母替换为另一个字母。
用来检查每个字母的位置的矩阵的另一个函数是:
import numpy as np
def playfair():
key = input('Enter the key :')
key += 'abcdefghiklmnopqrstuvwxyz '
size = len(key)
listed_key=list(key)
for i in range(size):
if listed_key[i] == 'j':
listed_key[i] = 'i'
for j in range(i+1, size):
if listed_key[j] == listed_key[i]:
for k in range(j, size-1):
listed_key[k] = listed_key[k+1]
size -= 1
else : j += 1
final_key = ''.join(listed_key)
play = np.zeros((5, 5), 'U1')
play.flat[:32] = list(final_key)
return play
我得到的错误是:
Enter the keysecc
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-6-dd6ef9446893> in <module>
33 output_text = ''.join(listed_op)
34 print(output_text)
---> 35 encrypt('Hithisisvishal')
<ipython-input-6-dd6ef9446893> in encrypt(input_message)
7 for i in range(len(listed_ip)-1):
8 first_letter = listed_ip[i]
----> 9 second_letter = listed_ip[j]
10 if first_letter =='j':
11 listed_ip[i] = 'i'
IndexError: list index out of range
答案 0 :(得分:1)
您的i和j没有正确分配。
for i in range(len(listed_ip)-1):
变量i将为0、1、2,... ...设置i + = 2后,将在循环返回时将其重置。变量j在第二个for循环内更新。因此,您有索引错误。
def encrypt(input_message):
r1 = r2 = c1 = c2 = 0
play = playfair()
listed_ip = list(input_message)
j = 1
listed_op = list('#'*150)
for i in range(0, len(listed_ip)-1, 2):
j = i + 1
first_letter = listed_ip[i]
second_letter = listed_ip[j]
if first_letter == 'j':
listed_ip[i] = 'i'
if second_letter == 'j':
listed_ip[j] = 'i'
for row in range(5):
for col in range(5):
if play[row][col] == first_letter:
r1 = row
c1 = col
if play[row][col] == second_letter:
r2 = row
c2 = col
if r1 == r2:
listed_op[i] = play[r1][(c1+1) % 5]
listed_op[j] = play[r2][(c2 + 1) % 5]
elif c1 == c2:
listed_op[i] = play[(r1+1) % 5][c1]
listed_op[j] = play[(r2+1) % 5][c2]
else:
listed_op[i] = play[r1][c2]
listed_op[j] = play[r2][c1]
# i += 2
# j += 2
output_text = ''.join(listed_op)
print(output_text)
答案 1 :(得分:0)
您的问题似乎是您的j值对于数组而言太大了。当前,对于listing_ip中的每个元素,您的j值将增加十。以这种速率,它只需要两次迭代就可以超出list_ip的长度并抛出IndexError。对此的可能解决方法是在for循环内而不是之前将j值设置为1。这样,对于listed_ip中的每个值,您都从j = 1开始。
这里是我的解决方法:
def encrypt(input_message):
r1=r2=c1=c2=0
play = playfair()
listed_ip = list(input_message)
print(listed_ip)
listed_op=list('#####################################################################################################################################################')
for i in range(len(listed_ip)-1):
j=1
first_letter = listed_ip[i]
second_letter = listed_ip[j]
if first_letter =='j':
listed_ip[i] = 'i'
if second_letter == 'j':
listed_ip[j] = 'i'
for row in range(5):
for col in range(5):
if play[row][col] ==first_letter:
r1=row
c1=col
if play[row][col] == second_letter:
r2=row
c2=col
if r1 == r2:
listed_op[i] = play[r1][(c1+1)%5]
listed_op[j] = play[r2][(c2 + 1) % 5]
elif c1 ==c2:
listed_op[i] = play[(r1+1)%5][c1]
listed_op[j] = play[(r2+1)%5][c2]
else:
listed_op[i]=play[r1][c2]
listed_op[j]=play[r2][c1]
i+=2
j+=2
output_text = ''.join(listed_op)
print(output_text)
encrypt('Hithisisvishal')
这只是对那一行的更改,看看它是否对您有用