PLAYFAIR CIPHER
#include<stdio.h>
#include<string.h>
#define max 40
void playFair(void);
void display(void);
void inputText(void);
void encryptionText(void);
void decryptionText(void);
void outputText(void);
char playfair[5][5],rtext[max],CT[max],PT[max],dtext[max];
int jcount=0,jpos[max];
void playFair()
{
char key[max],dkey[max];
int i,j,m,n,m1;
int serial=0,index=0;
printf("\nEnter the key: ");
scanf("%s",key);
for(m1=0;m1<strlen(key);m1++)
{
if(key[m1]=='j')
key[m1]='i';
}
strcat(key,"abcdefghiklmnopqrstuvwxyz");
for(i=0;i<strlen(key);i++)
{
for(j=0;j<i;j++)
{
if(key[i]==key[j])
break;
}
if(j==i)
dkey[index++]=key[i];
}
for(m=0;m<5;m++)
{
for(n=0;n<5;n++)
{
playfair[m][n]=dkey[serial++];
}
}
}
void display()
{
printf("\n\n......PLAYFAIR-KEY-TABLE......\n\n");
int m,n;
for(m=0;m<5;m++)
{
for(n=0;n<5;n++)
{
printf(" %c",playfair[m][n]);
}
printf("\n");
}
printf("\n\n");
}
void inputText()
{
char text[max];
int k,i,j=0;
printf("\nEnter the Text: ");
scanf("%s",text);
for(k=0;k<strlen(text);k++)
{
if(text[k]=='j')
{
text[k]='i';
jpos[jcount++]=k;
}
}
for(i=0;i<strlen(text);i++)
{
rtext[j++]=text[i];
if(text[i]==text[i+1])
{
rtext[j++]='x';
}
}
if((strlen(rtext)%2)!=0)
rtext[j]='x';
//printf("\n Last X contain Text: %s",rtext);
}
void encryptionText()
{
int i,j,m,n,r1,r2,c1,c2;
for(i=0,j=1;i<strlen(rtext);i=i+2,j=j+2)
{
for(m=0;m<5;m++)
{
for(n=0;n<5;n++)
{
if(playfair[m][n]==rtext[i])
{
r1=m;
c1=n;
}
else if(playfair[m][n]==rtext[j])
{
r2=m;
c2=n;
}
}
}
if(r1==r2)
{
CT[i]=playfair[r1][(c1+1)%5];
CT[j]=playfair[r2][(c2+1)%5];
}
else if(c1==c2)
{
CT[i]=playfair[(r1+1)%5][c1];
CT[j]=playfair[(r2+1)%5][c1];
}
else
{
CT[i]=playfair[r1][c2];
CT[j]=playfair[r2][c1];
}
}
//printf("\nCipher Text: %s\n",CT);
}
void decryptionText()
{
int i,j,m,n,r1,r2,c1,c2;
for(i=0,j=1;i<strlen(CT);i=i+2,j=j+2)
{
for(m=0;m<5;m++)
{
for(n=0;n<5;n++)
{
if(playfair[m][n]==CT[i])
{
r1=m;
c1=n;
}
else if(playfair[m][n]==CT[j])
{
r2=m;
c2=n;
}
}
}
if(r1==r2)
{
PT[i]=playfair[r1][((c1-1)+5)%5];
PT[j]=playfair[r2][((c2-1)+5)%5];
}
else if(c1==c2)
{
PT[i]=playfair[((r1-1)+5)%5][c1];
PT[j]=playfair[((r2-1)+5)%5][c1];
}
else
{
PT[i]=playfair[r1][c2];
PT[j]=playfair[r2][c1];
}
}
//printf("\nPlain Text: %s\n",PT);
outputText();
}
void outputText()
{
int i,j=0,k;
for(i=0;i<strlen(PT);i++)
{
if(PT[i]=='x')
i++;
dtext[j++]=PT[i];
}
//printf("\nOutput Text: %s\n\n",dtext);
if(jcount!=0)
{
//printf("\nJ is in position:");
for(k=0;k<jcount;k++)
{
dtext[jpos[k]]='j';
}
}
//printf("\nOutput Text with j: %s\n\n",dtext);
}
main()
{
playFair();
display();
inputText();
encryptionText();
decryptionText();
printf("\nCipher Text: %s\n",CT);
printf("\nPlain Text: %s\n",dtext);
}
Output:
Enter the key: charles
......PLAYFAIR-KEY-TABLE......
c h a r l
e s b d f
g i k m n
o p q t u
v w x y z
Enter the Text: ebmyfo
Cipher Text: sdtreu
Plain Text: ebmyfo
__________________________________________________________
Enter the key: charles
......PLAYFAIR-KEY-TABLE......
c h a r l
e s b d f
g i k m n
o p q t u
v w x y z
Enter the Text: hello
Cipher Text: csazcu
Plain Text: hello
0 Comments