Reputation: 492
This is a code for encrypting the message like you do in playfair cipher.
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')
I am supposed to get an output that changes this input message where evry letter is replaced with another letter according to its position in a matrix.
The other function where the matrix in which the position of each letter is checked with is:
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
The error i am getting is:
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
Upvotes: 0
Views: 800
Reputation: 1541
Your i and j are not correctly assigned.
for i in range(len(listed_ip)-1):
The variable i will be 0, 1, 2, ... After you set i += 2, it will be reset when it loops back. The variable j is updated inside the second for loop. Therefore, you have index error.
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)
Upvotes: 1
Reputation: 1
it looks like your problem is that your j value is getting too big for the array. Currently your j value is increasing by ten for every element in listed_ip. At that rate, it only needs two iterations to exceed the length of listed_ip and throw an IndexError. A possible fix for this would be setting your j value to 1 inside the for loop rather than before it. That way for every value in listed_ip, you start with j=1.
Heres my fix:
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')
Its just a change to that one line, see if it works for you
Upvotes: 0