Mar 10, 2010

Playfair cipher encyption and decryption

//PLAYFAIR CIPHER ENCRYPTION

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
FILE *fp;
int i,m=0,flag=0,j,n,row1,row2,col1,col2;
char ch,d,mat[5][5],key[10],k[10],c[30],a[10],ct[10],pt[10];
clrscr();
fp=fopen("key.txt","r");
fscanf(fp,"%s",key);
fclose(fp);
printf("The key is: %s",key);
//finding new key removing duplicates...
for(i=0;i<strlen(key);)
{
if(key[i]=='j')
key[i]=105;
flag=0;
for(j=0;j<i;j++)
{
if(key[i]==key[j])
flag=1;
}
if(flag==1)
i++;
else
k[m++]=key[i++];  //new array for key
}
k[m]=NULL;
printf("\nThe new key is  :\n%s",k);//printing new key
//store key in matrix
m=0;
for(ch=97;ch<=122;)
{
flag=0;
for(i=0;i<strlen(k);i++)
{
if(k[i]==ch)
flag=1;
}
if(flag==1)
ch++;
else if(ch=='j')
ch++;
else
{
c[m++]=ch;
ch++;
}
}
c[m]=NULL;
//construction of 2-D 5*5 matrix
m=0;
n=0;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
mat[i][j]=k[m++];
if(m>strlen(k))
mat[i][j]=c[n++];
}
}
printf("\n");
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
printf("\t %c",mat[i][j]);
printf("\n");
}
//retrieve plain-text from file
i=0;
fp=fopen("plain.txt","r");
fscanf(fp,"%s",pt);
fclose(fp);
printf("\nThe plain-text is: %s",pt);
//seperation of digram
j=0;
printf("\nAfter Processing Digrams: ");
for(i=0;i<strlen(pt);)
{        if(pt[i]==pt[i+1])
{
a[j++]=pt[i];
a[j++]='x';
i+=1;
}
else
{
a[j++]=pt[i];
a[j++]=pt[i+1];
i+=2;
}
}
a[j]='\0';
printf("%s",a);
printf("\n");
for(i=0;i<strlen(a);i+=2)//Loop for conversion to cipher text
{
for(j=0;j<5;j++)// matrix row
{
for(m=0;m<5;m++)//matrix column
{
//Find position of 1st element of Digram in matrix
if(a[i]==mat[j][m])
{
row1=j;
col1=m;
}
//Find position of 2nd element of Digram in matrix
if(a[i+1]==mat[j][m])
{
row2=j;
col2=m;
}
//Condition For both elements in same row of matrix
if(row1==row2)
{
ct[i]=mat[row1][(col1+1)%5];
ct[i+1]=mat[row1][(col2+1)%5];
}
//Condition For both elements in same column of matrix
else if(col1==col2)
{
ct[i]=mat[(row1+1)%5][col1];
ct[i+1]=mat[(row2+1)%5][col2];
}
else
{
ct[i]=mat[row1][col2];
ct[i+1]=mat[row2][col1];
}
}
}
}
ct[i]='\0';
fp=fopen("cipher.txt","w");
printf("\nCipher Text: ");
//store cipher text in a file
for(i=0;i<strlen(ct);i++)
putc(ct[i],fp);
fclose(fp);
printf("%s",ct);      //print cipher text
getch();
}


//PLAYFAIR CIPHER DECRYPTION

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
FILE *fp;
int i,m=0,flag=0,j,n,row1,row2,col1,col2;
char ch,d,mat[5][5],key[10],k[10],c[30],a[10],ct[10],pt[10];
clrscr();
fp=fopen("key.txt","r");
fscanf(fp,"%s",key);
fclose(fp);
printf("The key is: %s",key);
for(i=0;i<strlen(key);)
{
if(key[i]=='j')
{
key[i]='i';
}
flag=0;
for(j=0;j<i;j++)
{
if(key[i]==key[j])
flag=1;
}

if(flag==1)
i++;
else
k[m++]=key[i++];  //new array for key
}
k[m]=NULL;
// printing new key after elimination of duplicates
printf("\nThe new key is  :\n%s",k);
//seperation of key letters from a-z & later stored in an array
m=0;
for(ch=97;ch<=122;)
{
flag=0;
for(i=0;i<strlen(k);i++)
{
if(k[i]==ch)
flag=1;
}
if(flag==1)
ch++;
else if(ch=='j')
ch++;
else
{
c[m++]=ch;
ch++;
}
}
c[m]=NULL;
//construction of 2-D 5*5 matrix
m=0;
n=0;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
mat[i][j]=k[m++];
if(m>strlen(k))
mat[i][j]=c[n++];
}
}
printf("\n");
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
printf("\t %c",mat[i][j]);
}
printf("\n");
}

//retrieve cipher-text from file

i=0;
fp=fopen("cipher.txt","r");
fscanf(fp,"%s",ct);
fclose(fp);
printf("\nThe Cipher-text is: %s",ct);



printf("\n");
for(i=0;i<strlen(ct);i+=2)//conversion to plain text
{
for(j=0;j<5;j++) //matrix row
{
for(m=0;m<5;m++)//matrix column
{
//Find position of 1st element of Digram in matrix
if(ct[i]==mat[j][m])
{
row1=j;
col1=m;
}
//Find position of 2nd element of Digram in matrix
if(ct[i+1]==mat[j][m])
{
row2=j;
col2=m;
}
//Condition For both elements in same row of matrix
if(row1==row2)
{
pt[i]=mat[row1][(col1-1+5)%5];
pt[i+1]=mat[row1][(col2-1+5)%5];
}
//Condition For both elements in same column of matrix
else if(col1==col2)
{
pt[i]=mat[(row1-1+5)%5][col1];
pt[i+1]=mat[(row2-1+5)%5][col2];
}
else
{
pt[i]=mat[row1][col2];
pt[i+1]=mat[row2][col1];
}
}
}
}
pt[i]='\0';
fp=fopen("plain.txt","w");
printf("\nPlain Text: ");
//store plain text in a file
for(i=0;i<strlen(pt);i++)
  putc(pt[i],fp);
fclose(fp);
printf("%s",pt);      //print plain text
fclose(fp);
getch();
}

No comments:

Post a Comment