Playfair cipher encryption-decryption
Pseudocode:
- 1. First, a plaintext message is split into pairs of two letters (digraphs). If there is an odd number of letters, a Z is added to the last letter. Let us say we want to encrypt the message "Programming". It will be written as - Programming
- 2. The rules of encryption are -
- 3. If both the letters are in the same column, take the letter below each one (going back to the top if at the bottom)
- 4. If both letters are in the same row, take the letter to the right of each one (going back to the left if at the farthest right)
- 5. If neither of the preceding two rules is true, form a rectangle with the two letters and take the letters on the horizontal opposite corner of the rectangle.
- 6. Using these rules, the result of the encryption of 'Programming' with the key of 'Algorithm' would be − UlroalkkcvhG
Prgram:
#include<stdio.h>
#include<strings.h>
#include<string.h>
void choice_fill();
char encryption(char []);
void decryption(char []);
void play_fair();
char key[100],play[5][5],ct[255],msg[255],plain_text[255],c_text[255];
void main(){
int i,choice,flag=0,j,k,size;
printf("\n --- play Fair Cipher ---\n");
printf("Enter plain text :");
scanf("%[^\n]",msg);
size=strlen(msg);
int count = 0;
for (i = 0; msg[i]; i++)
if (msg[i] != ' ')
msg[count++] = msg[i];
msg[count] = '\0';
for(i=0;i<size;i=+2)
{
if(msg[i]==msg[i+1])
{
for(j=size;j>i;j--)
{
msg[j]=msg[size-1];size--;
}
msg[j+1]='x';
}
}
size=strlen(msg);
if(size%2!=0){ msg[size]='x'; msg[size+1]='\0';}
printf("\n plain text after space remove: %s",msg);
printf("\n message for encryption is : ");
for(i=0,j=0;msg[i];i++){putchar(msg[i]);
if(i%2!=0){printf(" ");}
}
choice_fill();
}
void choice_fill(){
int choice,flag=0;
do{
printf("\npress 1 for encryption\npress 2 for decryption\npress '0' for exit\n");
scanf("%d",&choice);
switch(choice){
case 1 : encryption(msg); flag=1;break;
case 2 : if(flag==1)decryption(ct);
else printf("first perform encryption process");
break;
case 0:return;
default : printf("\nplease enter valid choice\n"); break;
}
}while(choice!=0);
}
char encryption(char pt[]){
int i,j,k,l,r1,r2,c1,c2,p,q;
printf("\n Plain text : %s",pt);
play_fair();
j=1;
for(i=0;i<strlen(pt);){
r1=0;r2=0;c1=0;c2=0;p=0;q=0;
p=pt[i];q=pt[j]; printf("\t "); putchar(pt[i]); putchar(pt[j]); printf("=");
if(p=='j'){ pt[i]='i';}
if(q=='j'){ pt[i]='i';}
for(k=0;k<5;k++){ for(l=0;l<5;l++)
{if(play[k][l]==p){ r1=k;c1=l;}if(play[k][l]==q){ r2=k;c2=l;}}}
if(r1==r2){ct[i]=play[r1][(c1+1)%5];ct[j]=play[r2][(c2+1)%5];
putchar(ct[i]);putchar(ct[j]);
}else if(c1==c2){ct[i]=play[(r1+1)%5][c1];ct[j]=play[(r2+1)%5][c2];
putchar(ct[i]);putchar(ct[j]);
}else{ct[i]=play[r1][c2];ct[j]=play[r2][c1];
putchar(ct[i]);putchar(ct[j]);}i=i+2;j=j+2;
}
printf("\n\n \tEncrypted msg is : %s\n",ct);
}
void decryption(char pt[]){
int i,j,k,l,r1,r2,c1,c2,p,q;
printf("\n Plain text : %s",pt);j=1;
for(i=0;i<strlen(pt);){
r1=0;r2=0;c1=0;c2=0;p=0;q=0;
p=pt[i];q=pt[j]; printf("\t "); putchar(pt[i]); putchar(pt[j]);
printf("=");
if(p=='j'){ pt[i]='i';}
if(q=='j'){ pt[i]='i';}
for(k=0;k<5;k++){for(l=0;l<5;l++)
{if(play[k][l]==p){ r1=k;c1=l;}if(play[k][l]==q){ r2=k;c2=l;}}}
if(r1==r2){ct[i]=play[r1][(c1-1)%5];ct[j]=play[r2][(c2-1)%5];
putchar(ct[i]);putchar(ct[j]);}
else if(c1==c2){ct[i]=play[(r1-1)%5][c1];ct[j]=play[(r2-1)%5][c2];
putchar(ct[i]);putchar(ct[j]);}
else{ct[i]=play[r1][c2];ct[j]=play[r2][c1];putchar(ct[i]);putchar(ct[j]);}i=i+2;j=j+2;
}
printf("\n\n \tPlain Text msg is : %s\n",ct);
}
void play_fair(){//Creating playfair matrix
int i,k,j,size;
printf("\n enter key : "); scanf("%s",key);
strcat(key,"abcdefghiklmnopqrstuvwxyz");
size=strlen(key);
for(i=0;i<size;i++){
if(key[i]=='j'){ key[i]=='i';}
for(j=i+1;j<size;){if (key[j] == key[i]){
for (k = j; k < size; k++) { key[k] = key[k + 1]; } size--;}else j++;}
}
for(i=0,k=0;i<5;i++){ //generate play fair cipher block 5*5
for(j=0;j<5;j++){
play[i][j]=key[k];
k++;}
}
for(i=0;i<5;i++){
for(j=0;j<5;j++){ printf(" "); putchar(play[i][j]); } printf("\n");
}
}Viva Question
Message: COMSEC means communications security
Keyword: Galois
2. Let the keyword in Playfair cipher is “keyword”. Encrypt a message “come to the window” using Playfair cipher.
3. Construct a Playfair matrix with the key “occurrence”. Generate the cipher text for the plaintext “Tall trees”
4. Construct a Playfair matrix with the key “moonmission” and encrypt the message “greet”.
5. Is Playfair cipher monoalphabetic cipher?
Comments
Post a Comment