AIM:
Towrite a C++ program toimplement Play Fair Cipher.
ALGORITHM:
Step 1: Generate Key matrix.
(a)Take any random key of any length and
form a 5 X 5 matrix.
(b) Fill the rows of
the matrix with the key characters and ignore repeating character.
(c) Fill the remaining matrix with
alphabets from A to Z (except those already occurred in the key).
Step 2: Encrypt the data using encryption rule and key
matrix
(a) To Encrypt the data take
two characters at time from plain text file and encrypt it using one of the following rules.
(b) Repeating plain
text letters that would fall in the same pair are separated with filler letter.
(c)
If both the characters are in the same raw then replace each with the character
to its right, with the last character
followed by the first, in the matrix.
(d) If both the
characters are in the same column then replace each with the character below it, with the bottom character
followed by the top, in the matrix.
Otherwise each plain
text letter is replaced by the letter that lies in its own row and the column occupied by the
other plain text letter
Step 3:To decrypt, use the
inverse of encryption rules.
Step 4: Display the cipher
text after encryption and plain text after decryption.
PROGRAM:
#include<iostream>
using
namespace std;
int
main()
{
inti,j,isI=0,l,k=0,start=0,same=0,k1=0;
int
i1,j1,i2,j2,i11,jj;
char
alph[5][5],chr[25],chr2[25],character,chr1='a',chr_ex[25];
char
txt[25],txt2[25];
cout<<"Enter
the key string : ";
cin>>chr;
for(i=0;i<strlen(chr2);i++) /*TO
PREVENT STRING FROM BEING PRINTED WITH GARBAGE CHARACTER...*/
{
chr_ex[i]='-';
}
for(i=0;i<strlen(chr);i++)
{
cout<<"\n"<<i+1<<"\t"<<chr[i];
chr_ex[i]=chr[i];
}
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
alph[i][j]='-';
}
}
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
character=chr[++k];
for(l=k-1;l>=0;l--) //FOR REPEATING CHARACTERS...
{
if(character==chr[l])
{
chr[k]='-';
break;
}
}
}
}
k=0;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(start++<strlen(chr))
if(chr[k]!='-')
//FOR i AND j
{
if(chr[k]=='i'
|| chr[k]=='j')
{
if(isI==0)
{
alph[i][j]=chr[k];
isI=1;
}
else
{
if(chr[k]=='i')
chr[k]='i';
if(chr[k]=='j')
chr[k]='j';
j--;
}
}
elsealph[i][j]=chr[k];
}
else
{
chr[k]='-';
j--;
}
k++;
}
}
chr1--;
k1=0;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(alph[i][j]=='-')
{
same=0;
chr1++;
for(k1=0;k1<strlen(chr);k1++)
{
if(chr1==chr[k1])
{
same=1;
//else same=0;
j--;
break;
}
}
if(same!=1)
{
if(chr1=='i'
|| chr1=='j')
{
if(isI==0)
{
alph[i][j]=chr1;
isI=1;
}
else
j--;
}
else
alph[i][j]=chr1;
}
}
}
}
cout<<"\n"<<"\n"<<"\n";
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
cout<<alph[i][j];
}
cout<<"\n";
}
j=0;
for(i=0;i<strlen(chr);i++)
{
if(chr[i]=='-')
{
for(j=i;j<strlen(chr);j++)
{
chr[j]=chr[j+1];
}
}
}
cout<<"\n"<<"\n"<<"Enter
the plain text : ";
cin>>txt;
j=0;
for(i=0;i<strlen(txt);i++)
{
txt2[j++]=txt[i];
if(txt[i]==txt[i+1])
{
txt2[j++]='x';
}
}
jj=j;
for(i11=0;i11<strlen(txt2);i11+=2)
{
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(txt2[i11]==alph[i][j])
{
i1=i;
j1=j;
}
if(txt2[i11+1]==alph[i][j])
{
i2=i;
j2=j;
}
}
}
if(i1==i2)
{
if((j1+1)>=5) j1=-1;
if((j2+1)>=5) j2=-1;
txt2[i11]=alph[i1][j1+1];
txt2[i11+1]=alph[i2][j2+1];
}
else if(j1==j2)
{
if((i1+1)>=5) i1=-1;
if((i2+1)>=5) i2=-1;
txt2[i11]=alph[i1+1][j1];
txt2[i11+1]=alph[i2+1][j2];
}
else
{
if(i2>i1)
{
txt2[i11+1]=alph[i2][j1];
txt2[i11]=alph[i1][j2];
}
else
{
txt2[i11]=alph[i2][j1];
txt2[i11+1]=alph[i1][j2];
}
}
}
cout<<"\n"<<"Final
string : ";
(i=0;i<jj;i++)
{
cout<<txt2[i];
}
}
OUTPUT:
Comments
Post a Comment