You are on page 1of 5

 Playfair cipher…

…Experiment-4...
Aim: - write a c program to implement of ‘play-fair cipher’ encryption order.
Theory: -
Play-fair cipher: Playfair ciphers are a type of block cipher: the ciphertext character
that replaces a particular plaintext character in the encryption will depend in part on an
adjacent character in the plaintext. They are named for an English lord, Lyon Playfair,
who advocated their use, but they were invented by Charles Wheatstone (1854).

Encryption: -
 Encryption is accomplished using a square array of characters, constructed

from the encryption key. Because our set of plaintext characters is the 26-

letter English alphabet, for us this array will be 5 × 5, with 2 of the 26

characters occupying a single position in the array. Typically, these two

characters are i and j, since usually it is easy to distinguish from the

context which of these two letters was intended in the plaintext.

For example: Suppose the encryption key is the word ‘Larkspur’ and we want to
encrypt the message “Rocky Mountain Meadow” using a Playfair cipher with this
keyword. First, we construct the 5 × 5 array below:

L A R K S
P U B C D
E F G H I/J
M N O Q T
V W X Y Z 5*5

 Now that we have the encryption array, we proceed with encryption of the
plaintext message as follows. We separate the characters in the plaintext into
pairs, to get
RO CK YM OU NT AI NM EA DO WX

Important rules of encryption of Playfair cipher:


1st. If the two letters in the diagram both appear in the same column of the encryption
array, each letter of the diagram is replaced with the letter immediately below it in
the encryption array. (For example, the diagram “ro” encrypts to BX.)
2nd. If the two letters in the diagram both appear in the same row of the encryption
array, each letter of the diagram is replaced with the letter immediately to its right
in the encryption array. If there is no letter to the right, the corresponding
ciphertext character is taken from the far left of the row (for example, the diagram
“nt” encrypts to OM).
3rd. If the two plaintext letters in the diagram are neither in the same row nor in the
same column of the encryption array, each is replaced by the letter in its own row
that shares a column with the other letter of the plaintext diagram. (For example,
the plaintext diagram “ym” is replaced with the ciphertext VQ, and the plaintext
diagram “ou” is replaced with the ciphertext NB.)

L A R K S
P U B C D
E F G H I/J
M Q
M N O Q T
V W X Y Z

Continuing through all the plaintext diagrams in our example, we obtain the
ciphertext: BXHCVQNBOMSFONFLBTXY.

 C code of ‘Play fair cipher’ for encryption...


INPUT:
#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==================\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) //when both characters in same row
 {
  if(c2==4) //for char in last column
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)//when both characters in same column
 {
  if(r2==4) //for char in last row
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]); 
 }
  //when characters are not in a same row and column
  if(r1 != r2 && c1 != c2) 
 {
  printf("\nCiphertext = %c%c \n", arr[r1][c2], arr[r2][c1]); 
 }
  return 0;
}

OUTPUT:
APPLY THE RULES OF OUTPUT

1st. Playfair Key matrix


==================
MONAR
CHYBD
EFGIK
LPQST
UVWXZ
Enter your plain text: YQ
Your plain text = YQ Ciphertext = GW
ACCORDING TO FIRST RULE.

2nd. Playfair Key matrix

==================

MONAR

CHYBD

EFGIK

LPQST

UVWXZ

Enter your plain text: IK

Your plain text = IK Ciphertext = KE

ACCORDING TO SECOND RULE.

3rd. Playfair Key matrix

==================

MONAR
CHYBD

EFGIK

LPQST

UVWXZ

Enter your plain text: OK

Your plain text = OK

Ciphertext = RF

ACCORDING TO THIRD RULE.

You might also like