0% found this document useful (0 votes)
119 views5 pages

Playfair Cipher Implementation Guide

The document describes an experiment implementing the Playfair cipher encryption algorithm. It includes the theory behind the Playfair cipher, which encrypts pairs of letters using a 5x5 grid keyed from a keyword. The program code generates the key grid from the keyword, removes duplicate letters from the plaintext, finds the positions of each letter pair, and encrypts the pairs according to the Playfair rules. It takes a keyword and plaintext from the user, encrypts the text, and outputs the encrypted text. In conclusion, it notes that while better than simpler ciphers, the Playfair cipher can be broken if the key is discovered and has weaknesses with repeated and I/J letters.

Uploaded by

Avinash Vad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
119 views5 pages

Playfair Cipher Implementation Guide

The document describes an experiment implementing the Playfair cipher encryption algorithm. It includes the theory behind the Playfair cipher, which encrypts pairs of letters using a 5x5 grid keyed from a keyword. The program code generates the key grid from the keyword, removes duplicate letters from the plaintext, finds the positions of each letter pair, and encrypts the pairs according to the Playfair rules. It takes a keyword and plaintext from the user, encrypts the text, and outputs the encrypted text. In conclusion, it notes that while better than simpler ciphers, the Playfair cipher can be broken if the key is discovered and has weaknesses with repeated and I/J letters.

Uploaded by

Avinash Vad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Cloud Computing and Network Security Lab 2015

PRACTICAL 2
Aim:
To implement Mayfair Cipher code for cryptography
Instructors:
Professor Kiran Joshi & Professor Sowmiya Raksha
Theory:
The Playfair Cipher is a manual symmetric encryption cipher invented in 1854 by Charles
Wheatstone, however its name and popularity came from the endorsement of Lord Playfair.
The Playfair cipher encrypts pairs of letters (digraphs), instead of single letters as is the case with
simpler substitution ciphers such as the Caesar Cipher. Frequency analysis is still possible on the
Playfair cipher, however it would be against 600 possible pairs of letters instead of 26 different
possible letters. For this reason the Playfair cipher is much more secure than older substitution
ciphers, and its use continued up until WWII.
The playfair cipher starts with creating a key table. The key table is a 55 grid of letters that will
act as the key for encrypting your plaintext. Each of the 25 letters must be unique and one letter
of the alphabet (usually Q) is omitted from the table (as there are 25 spots and 26 letters in the
alphabet).
Now for the actual encryption process. The Playfair cipher uses a few simple rules relating to
where the letters of each digraph are in relation to each other. The rules are:

If both letters are in the same column, take the letter below each one (going back to the
top if at the bottom)

If both letters are in the same row, take the letter to the right of each one (going back to
the left if at the farthest right)

If neither of the preceding two rules are true, form a rectangle with the two letters and
take the letters on the horizontal opposite corner of the rectangle

Program:
#include<stdio.h>
#include<string.h>
#define BUFFSIZE 26
void generate_matrix(char a[][5],char key[])
{
int arr[BUFFSIZE]={0};
int k=0,i=0,j=0;
for(k=0;k<strlen(key);k++)
V.J.T.I, Mumbai - 400056

Page 1

Cloud Computing and Network Security Lab 2015

{
if(!arr[key[k]-'A'])
{
a[i][j] = key[k];
if(j==4)
{j=0;i++;}
else
j++;
arr[key[k]-'A']=1;
}
}
for(k=0;k<BUFFSIZE;k++)
{
//printf("%c\n",'A'+k);
if('A'+k == 'J')
continue;
if(!arr[k])
{
//printf("%c %d %d\n",'A'+k,i,j);
a[i][j]='A'+k;
if(j == 4)
{j=0;i++;}
else
j++;
}
}
}
void remove_duplicate(char text[])
{
int i=1;int len=strlen(text);
for(;i<len-1;i++)
{
if(text[i-1] == text[i])
text[i]='X';
}
}
void find_index(char a,char playfair[][5],int *x,int *y)
{
if(a == 'J')
a='I';
int i=0,j=0;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(a == playfair[i][j])
V.J.T.I, Mumbai - 400056

Page 2

Cloud Computing and Network Security Lab 2015

{
*x=i;*y=j;
}
}
}
}
void encrypt_text(char text[],char playfair[][5],char encrypt[])
{
if(strlen(text)%2 == 1)
{
int len=strlen(text);
text[len]='X';
text[len+1]='\0';
}
//printf("%s\n",text);
remove_duplicate(text);
//printf("%s\n",text);
int i=0;
for(i=0;i<strlen(text)-1;i+=2)
{
char a1=text[i],a2=text[i+1];
int x1=0,y1=0,x2=0,y2=0;
find_index(a1,playfair,&x1,&y1);
find_index(a2,playfair,&x2,&y2);
//printf("%d %d %d %d\n",x1,y1,x2,y2);
if(x1!=x2 && y1!=y2)
{
if(y1<y2){
encrypt[i]=playfair[x2][y1];
encrypt[i+1]=playfair[x1][y2];
}
else
{
encrypt[i]=playfair[x1][y2];
encrypt[i+1]=playfair[x2][y1];
}
}
else{
if(x1==x2)
{
encrypt[i]=playfair[x1][(y1+1)%5];
encrypt[i+1]=playfair[x2][(y2+1)%5];
}
else{
encrypt[i]=playfair[(x1+1)%5][y1];
encrypt[i+1]=playfair[(x2+1)%5][y2];
V.J.T.I, Mumbai - 400056

Page 3

Cloud Computing and Network Security Lab 2015

}
}
}
encrypt[i]='\0';
}
int main()
{
char a[5][5];
char key[100],text[200],encrypt[200];
printf("Enter Key:");
scanf("%s",&key);
//printf("%s\n",key);
generate_matrix(a,key);
printf("PlayFair Array");
int i=0,j=0;
for(i=0;i<5;i++)
{printf("\n");
for(j=0;j<5;j++)
printf("%c ",a[i][j]);
}
printf("\n");
printf("Enter Text to be Ciphered: ");
int c;i=0;
c=getchar();
while((c=getchar())!=EOF && c!='\n')
{
if(c == ' ')
continue;
text[i]=c;
i++;
}
text[i]='\0';
//scanf("%s",text);
encrypt_text(text,a,encrypt);
printf("%s\n",encrypt);
}

V.J.T.I, Mumbai - 400056

Page 4

Cloud Computing and Network Security Lab 2015

Output:

Conclusion:
Thus we studied a cryptography technique called Playfair Cipher. The Cipher is better than
Caesar cipher. However it is very simple to reconstruct the message if the key is discovered.
Letters like I or J share a single space and hence an encrypted message must be checked for such
both their possibilities. The cipher also requires reconstructing repeating letters. However for
most messages this cipher works well.

V.J.T.I, Mumbai - 400056

Page 5

You might also like