0% found this document useful (0 votes)
443 views

Write A Program For Encryption and Decryption Using Playfair Matrix in Any Language. Encryption Code in C Language

The document provides C code to encrypt and decrypt text using a Playfair cipher. The encryption code takes plaintext as input, finds the positions of each character in the key matrix, and outputs the ciphertext by applying the Playfair cipher rules. The decryption code takes ciphertext as input, finds the positions of each character in the key matrix (which is the same as the encryption key matrix), and recovers the plaintext by applying the inverse Playfair cipher rules. Both the encryption and decryption codes demonstrate a full Playfair cipher implementation in C to encrypt and decrypt messages securely.

Uploaded by

sameer khan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
443 views

Write A Program For Encryption and Decryption Using Playfair Matrix in Any Language. Encryption Code in C Language

The document provides C code to encrypt and decrypt text using a Playfair cipher. The encryption code takes plaintext as input, finds the positions of each character in the key matrix, and outputs the ciphertext by applying the Playfair cipher rules. The decryption code takes ciphertext as input, finds the positions of each character in the key matrix (which is the same as the encryption key matrix), and recovers the plaintext by applying the inverse Playfair cipher rules. Both the encryption and decryption codes demonstrate a full Playfair cipher implementation in C to encrypt and decrypt messages securely.

Uploaded by

sameer khan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Question No.

Write a program for encryption and decryption using playfair matrix in any
language.

Encryption code in C Language

#include<stdio.h>
int main(){

  char arr[5][5]={"MONAR","CHYBD","EFGIK","LPQST","UVWXZ"};
  char pt[10];

  int i, j, r1=0, r2=0, c1=0, c2=0;


  printf("Playfair Keymatrix\n");
  for(i=0; i<5; i++)
 {
  for(j=0; j<5; j++)
printf("%c ", arr[i][j]);
  printf("\n");
 }

  printf("Enter your plain text:");


  scanf("%s",pt);
  printf("Your plain text = %s", pt);

  for(i=0; i<5; i++)


 {
  for(j=0; j<5; j++)
  {
    if(arr[i][j] == pt[0])
{
r1=i; c1=j;
}
if(arr[i][j] == pt[1])
{
r2=i; c2=j;
}
  }
 }
  if(r1==r2)
 {
  if(c2==4)
printf("Ciphertext = %c%c \n", arr[r1][c1+1], arr[r2][0]); 
  else
printf("Ciphertext = %c%c \n", arr[r1][c1+1], arr[r2][c2+1]);
 }
  if(c1==c2)
 {
  if(r2==4)

printf("Ciphertext = %c%c \n", arr[r1+1][c1], arr[0][c2]); 


  else
printf("Ciphertext = %c%c \n", arr[r1+1][c1], arr[r2+1][c2]); 
  }  if(r1 != r2 && c1 != c2) 
 {
  printf("\nCiphertext = %c%c \n", arr[r1][c2], arr[r2][c1]); 
 }
  return 0;
}

Decryption code in C Language

#include<stdio.h>
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");
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)
{
if(c2==0)
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)
{
if(r2==0)
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]); 
}

if(r1 != r2 && c1 != c2) 


{
printf("Plaintext = %c%c \n", arr[r1][c2], arr[r2][c1]); 
}
return 0;
}

You might also like