Playfair Cipher program in c
Introduction to Playfair
The Playfair cipher or Playfair square or Wheatstone-Playfair cipher is a manual symmetric encryption technique(Use same key for encryption of plaintext and decryption of ciphertext) and was the first literal digram substitution cipher. The scheme was invented in 1854 by Charles Wheatstone, but bears the name of Lord Playfair for promoting its use.
This technique encrypts pairs of letters (bigrams or digrams), instead of single letters as in the simple substitution cipher and rather more complex cipher systems then in use.
History of playfair
It was however later used for tactical purposes by British forces in the Second Boer War and in World War I and for the same purpose by the British and Australians during World War II.
This was because Playfair is reasonably fast to use and requires no special equipment - just a pencil and some paper.
A typical scenario for Playfair use was to protect important but non-critical secrets during actual combat e.g. the fact that an artillery barrage of smoke shells would commence within 30 minutes to cover soldiers' advance towards the next objective. By the time enemy cryptanalysts could decode such messages hours later, such information would be useless to them because it was no longer relevant.
Playfair Program in c
Step 1:Variables we use in program:
First of all we see deceleration of variables.Here basic deceleration which is use full in further code...
#include<stdio.h>
#include<conio.h>
#include<time.h>
void main()
{
char key[50]={'\0'};
char o[25]={'a','b','c','d','e','f','g','h','i','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char k[25]={'\0'};
char m[5][5]={'\0'};
char pt[50]={'\0'},tp[50]={'\0'};
//variable for time complexity
clock_t start,end;
double time_used;
int i=0,j=0,flag=0,a=0,len=0;
clrscr();
Explain variable:
here use time.h for calculate time complexity of code.
key : char. array it is use for enter cipher key for encryption.
o : this is temp. array for add alpha bates
k : use for Add matrix's value in 1D(one dimension) array.
m :It is our 5x5 matrix.
pt and tp:pt is use for Plain Text and tp is temporary array.
Not understand variable don't panic...
Step 2:Enter key into 1D array:
In key we use only unique character so first of all remove space from key.In matrix which is 5x5 means 25 character store but we have 26 character therefore use i=j that mean whenever j come in key it will replace by i.you will see in below code start=clock();
printf("\n\t\t|||||||||| PLAYFAIR CIPHER ||||||||||\n");
printf("\n\n");
printf("\nEnter key:");
gets(key);
for(i=0;i<strlen(key);i++)
{
if(key[i]=='j')
{
key[i]='i';
}
for(j=0;j<strlen(k);j++)
{
if(k[j]==key[i])
{
flag=1;
break;
}
}
if(flag != 1)
{
k[a]=key[i];
a++;
}
flag=0;
} //remove same char from key and add into k
In above code start clock for time.using gets() retrieve value of key.First for loop is go for length of key in this check 'j' is present in key whenever find 'j' replace with 'i'.
In second for loop check character in 1D array k[25] because unique value is require.Here use of 'flag' , when flag=1 then skip character because it is present already in array k[25] .if flag !=1 then add this character and change flag=0.
E.g :"computer" is our key here duplicate not present therefore in k enter 'computer'
Step 3:Enter extra character into 1D array:
First enter key value then we also add remaining character in array. for(i=0;i<=strlen(o);i++)
{
for(j=0;j<strlen(k);j++)
{
if(k[j]==o[i])
{
flag=1;
break;
}
}
if(flag != 1)
{
k[a]=o[i];
a++;
}
flag=0;
} //add extra character
using Two for loop, one for length of alpha bates and second for 1D array.simply check character is present in k[25] or not.Not present then add character when present then skip.
E.g : our key is already enter in k[25] which is "computer" then remaining character "a b c d f g h i k l n q s v w x y z" is enter in key here 'i/j' enter in one place.
Step 4:One Dimension array to Two Dimension:
a=0;
printf("Matrix of 5x5 is:\n");
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
m[i][j]=k[a];
if(m[i][j]=='i')
{
printf("\t i/j");
}else
{
printf("\t %c",m[i][j]);
}
a++;
}
printf("\n");
}
Here 'a' is use for index of 1D array.using this for loop we store 1D array value into 2D array and also print that value.
Below picture of output for step 1 to 4.
In above output we see that character of key enter first then sequence wise entered in non repeated form.
Step 5: Enter plain text and remove space
printf("\n");
printf("\n Enter PlainText:");
gets(pt);
a=0;
for(i=0;i<strlen(pt);i++)
{
if(pt[i]!=' ')
{
tp[a]=pt[i];
a++;
}
} //remove space
Here when space is occur then skip it.And store new string in temporary variable 'tp'.
Step 6:Divide into pair of two
Playfair is work on pair of character.Here divide into two pair and check pair characters are same when character are same then add x after first character. Understand ?? No It's Ok...
E.g: Our Plan Text is "information"
First divide into two of pair "in fo rm at io n" but we need two pair here 'n' is alone therefore we enter 'x' after 'n' so new plaintext is "in fo rm at io nx".
second example: our plain text is "hello" then
"he ll o" is divide in pair but 'll' is same character in pair therefore add 'x' after 'l'.then new plaintext pair is "he lx lo".
len=strlen(tp); a=0; for(i=0;i<len;i++) { if(tp[i]==tp[i+1]) { pt[a]=tp[i]; pt[a+1]='x';
//add x when two same character
a=a+2; }else { pt[a]=tp[i]; pt[a+1]=tp[i+1]; i=i+1; a=a+2; } } if(strlen(pt)%2 !=0) { pt[a-1]='x'; } // add x when last character is alone printf("\n New PlainText:"); puts(pt);
Step 7:Call Playfair function:
First add function above main function:
then write below code in main:
In function we send plain text which is 'pt' and 2D array 'm'.
First of all see variable that is use in this function
cy: it is character array for ciphertext.
Here Three Rule of cipher text:
1) Same Column: When pair characters are in same column then use down character for cipher text.
2) Same Row: When pair in same row then use next character for cipher text.
3) Rectangle: When pair make rectangle then use corner of character match in matrix.
code for this rule:
here we have ciphertext: "in fo rm at io nx" ==> "gs le gr be fu xm"
Here encryption is done...
But also we have convert ciphertext into plaintext at receiving side therefore use Decryption.
"Decryption is inverse process of encryption"
Step 9: Decryption
Using cipher text 'cy' and inverse of encryption we can generate plaintext.
Here Main Output of code :
Below You Can download code in .txt file:
Thank you!!
void playfair(char [50],char [5][5]);
then write below code in main:
playfair(pt,m);
end=clock();
time_used=((double)(end-start))/CLOCKS_PER_SEC;
printf("\n time taken=%f sec",time_used);
getch();
}
In function we send plain text which is 'pt' and 2D array 'm'.
Step 8:Playfair Function:
void playfair(char pt[50],char m[5][5])
{
int i=0,j=0,k=0,a=0;
char cy[50]={'\0'};
int x1=0,y1=0,x2=0,y2=0;
First of all see variable that is use in this function
cy: it is character array for ciphertext.
Here Three Rule of cipher text:
- Same Column
- Same Row
- Rectangle
1) Same Column: When pair characters are in same column then use down character for cipher text.
2) Same Row: When pair in same row then use next character for cipher text.
3) Rectangle: When pair make rectangle then use corner of character match in matrix.
code for this rule:
for(k=0;k<strlen(pt);k++)
{
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(m[i][j]==pt[k])
{
x1=i;
y1=j;
}
if(m[i][j]==pt[k+1])
{
x2=i;
y2=j;
}
}
}
if(y1==y2) //.............same column rule:1
{
x1++;
x2++;
if(x1>4)
{
x1=0;
}
if(x2>4)
{
x2=0;
}
cy[a]=m[x1][y1];
a++;
cy[a]=m[x2][y2];
a++;
} //.............rule:1 done
else if(x1==x2) //..............................rule:2 same row
{
y1++;
y2++;
if(y1>4)
{
y1=0;
}
if(y2>4)
{
y2=0;
}
cy[a]=m[x1][y1];
a++;
cy[a]=m[x2][y2];
a++;
}else if(x1!=x2 && y1!=y2) //..........rule:3
{
cy[a]=m[x1][y2];
a++;
cy[a]=m[x2][y1];
a++;
} //.........................rule:3 done
k=k+1;
}
printf("\n Cipher text is:");
puts(cy);
here we have ciphertext: "in fo rm at io nx" ==> "gs le gr be fu xm"
Here encryption is done...
But also we have convert ciphertext into plaintext at receiving side therefore use Decryption.
"Decryption is inverse process of encryption"
Step 9: Decryption
a=0;
for(k=0;k<strlen(cy);k++)
{
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(m[i][j]==cy[k])
{
x1=i;
y1=j;
}
if(m[i][j]==cy[k+1])
{
x2=i;
y2=j;
}
}
}
if(y1==y2) //.........rule:1
{
if(x1==0)
{
x1=4;
}else
{
x1--;
}
if(x2==0)
{
x2=4;
}else
{
x2--;
}
pt[a]=m[x1][y1];
a++;
pt[a]=m[x2][y2];
a++;
}else if(x1==x2) //..........rule:2
{
if(y1==0)
{
y1=4;
}else
{
y1--;
}
if(y2==0)
{
y2=4;
}else
{
y2--;
}
pt[a]=m[x1][y1];
a++;
pt[a]=m[x2][y2];
a++;
}else if(x1!=x2 && y1!=y2) //............rule:3
{
pt[a]=m[x1][y2];
a++;
pt[a]=m[x2][y1];
a++;
}
k=k+1;
}
printf("\n Plain Text:");
puts(pt);
}
Using cipher text 'cy' and inverse of encryption we can generate plaintext.
Here Main Output of code :
Below You Can download code in .txt file:
Thank you!!
Nice... Good work...
ReplyDeleteThank you
DeleteThanks pruthvi
ReplyDelete