Implement Play fair cipher encryption-decryption

Practical - 3
Implement Play fair cipher encryption-decryption.



Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
char key[5][5]={'p','l','a','y','f','i','r','b','c','d','e','g','h','k','m','n','o','q','s','t','u','v','w','x','z'};
void take_input(char *text)
{
int i=0;
char c,pre;
while(1)
{
c=getch();
if(c==13)
{
text[i]='\0';
break;
}
else if(c==8)
{
putch('\b');
putch(' ');
putch('\b');
i--;
}
else if(c!=32)
{
if(isalpha(c) || isdigit(c))
{
putch(c);
//printf("\nalpha\t");
if(isdigit(c))
{

}
else if(isupper(c))
{
c=c+32;
}
if(i==0)
{
text[i]=c;
pre=c;
i++;
}
else
{
if(c==106)//ascii code of j
{
//printf("pre=%c\tcur=%c\t",pre,c);
if(pre==c)
{
text[i]='x';
i++;
text[i]=c;
pre=c;
i++;
}
else
{
text[i]='i';
pre='i';
i++;
}
}
else
{
//printf("pre=%c\tcur=%c\t",pre,c);
if(pre==c)
{
//printf("match\t");
if(pre==120)//ascii code of x
{
text[i]='z';
i++;
text[i]=c;
pre=c;
i++;
}
else
{
//printf("\nbefore change msg=%s\ti=%d\n",text,i);
text[i]='x';
//printf("\ttext=%c\t%s\t",text[i],text);
i++;
text[i]=c;
pre=c;
i++;
//printf("\nchanged msg=%s",text);
}

}
else
{
//printf("not match\t");
text[i]=c;
pre=c;
i++;
}
}
}
}
}
}
}
void encrypt()
{
char pt[200]={'\0'};
char ct[200]={'\0'};
int len,i,j,r1,c1,r2,c2,index=0,k=0;
printf("Enter message to encrypt:\n");
take_input(&pt);
len=strlen(pt);
if((len%2)!=0)
{
if(pt[len-1]=='x')
{
pt[len]='z';
pt[len+1]='\0';
}
else
{
pt[len]='x';
pt[len+1]='\0';
}
}
//printf("\nmessage :\t%s\t%d\n",pt,strlen(pt));
while(k<strlen(pt))
{
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(pt[k]==key[i][j])
{
r1=i;
c1=j;
}
else if(pt[k+1]==key[i][j])
{
r2=i;
c2=j;
}
}
}
if(r1==r2)
{
ct[index]=key[r1][(c1+1)%5];
ct[++index]=key[r2][(c2+1)%5];
}
else if(c1==c2)
{
ct[index]=key[(r1+1)%5][c1];
ct[++index]=key[(r2+1)%5][c2];
}
else
{
ct[index]=key[r1][c2];
ct[++index]=key[r2][c1];
}
index++;
k=k+2;
}
//ct[k-2]='\0';
printf("\nmodified text=\t%s\n",pt);
printf("\nEncrypted message:\t%s\n",ct);
}
void decrypt()
{
int i,j,k=0,r1,c1,r2,c2,len,index=0;
 char pt[200]={'\0'};
 char ct[200]={'\0'};
 char temp[200]={'\0'};
 printf("Enter encrypted message:\n");
 scanf("%s",ct);

 while(k<strlen(ct))
 {
  for(i=0;i<5;i++)
  {
   for(j=0;j<5;j++)
   {
    if(ct[k]==key[i][j])
    {
     r1=i;
     c1=j;
    }
    else if(ct[k+1]==key[i][j])
    {
     r2=i;
     c2=j;
    }
   }
  }
  if(r1==r2)
  {
   temp[index]=key[r1][(c1-1+5)%5];
   temp[++index]=key[r2][(c2-1+5)%5];
  }
  else if(c1==c2)
  {
   temp[index]=key[(r1-1+5)%5][c1];
   temp[++index]=key[(r2-1+5)%5][c2];
  }
  else
  {
   temp[index]=key[r1][c2];
   temp[++index]=key[r2][c1];
  }
  index++;
  k=k+2;
 }

printf("\ndecrypted message:\t%s\n",temp);

}

void main()
{
int choice;
clrscr();
while(1)
{
printf("Enter your choice: 1.Encrypt\t2.Decrypt\t3.Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
encrypt();
break;
}
case 2:
{
decrypt();
break;
}
case 3:
break;
default:
{
printf("Invalid choice\n");
}
}
if(choice==3)
{
break;
}
}
}
Output:




Comments

Popular posts from this blog

Study of DB Miner Tool

Study of WEKA tool

Create calculated member using arithmetic operators and member property of dimension member