Playfair Decryption implementation
Assumptions:
- Assume key matrix is given to us.
- Read cipher text (2 characters) from user.
- This program demonstrate four rules of the Playfair decryption algorithm.
- This program will process only 2 characters input. You may extend to process n characters by repeating given logic.
int main()
{
char arr[5][5]={"MONAR","CHYBD","EFGIK","LPQST","UVWXZ"};
char ct[10];
int i, j, r1=0, r2=0, c1=0, c2=0;
printf("Plaifair Keymatrix\n=================\n");
for(i=0; i<5; i++)
{
for(j=0; j<5; j++)
printf("%c ", arr[i][j]);
printf("\n");
}
printf("Enter your cipher text:");
scanf("%s",ct);
printf("Your cipher text is %s\n", ct);
for(i=0; i<5; i++)
{
for(j=0; j<5; j++)
{
if(arr[i][j] == ct[0])
{
r1=i; c1=j;
}
if(arr[i][j] == ct[1])
{
r2=i; c2=j;
}
}
}
if(r1==r2) //Rule2-when both characters in same row
{
if(c2==0) //for char in last column
printf("Plaintext = %c%c \n", arr[r1][c1-1], arr[r2][4]);
else
printf("Plaintext = %c%c \n", arr[r1][c1-1], arr[r2][c2-1]);
}
if(c1==c2)//Rule3- when both characters in same column
{
if(r2==0) //for char in last row
printf("Plaintext = %c%c \n", arr[r1-1][c1], arr[4][c2]);
else
printf("Plaintext = %c%c \n", arr[r1-1][c1], arr[r2-1][c2]);
}
//Rule4 when characters are not in a same row and column
if(r1 != r2 && c1 != c2)
{
printf("Plaintext = %c%c \n", arr[r1][c2], arr[r2][c1]);
}
return 0;
}
Output of the program
Plaifair Keymatrix
=================
M O N A R
C H Y B D
E F G I K
L P Q S T
U V W X Z
Enter your cipher text:NA
Your cipher text is NA
Plaintext = ON
No comments:
Post a Comment