Skip to main content

Playfair cipher

                                                          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
 1.      Encrypt the following message using Playfair cipher.
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

Popular posts from this blog

Hill Cipher

                                            Hill cipher encryption-decryption. PSEUDOCODE Encryption To encrypt a message using the Hill Cipher we must first turn our keyword into a key matrix (a 2 x 2 matrix for working with digraphs, a 3 x 3 matrix for working with trigraphs, etc). We also turn the plaintext into digraphs (or trigraphs) and each of these into a column vector. We then perform matrix multiplication modulo the length of the alphabet (i.e. 26) on each vector. These vectors are then converted back into letters to produce the ciphertext. Step 1:  We shall encrypt the plaintext message "short example" using the keyword hill and a 2 x 2 matrix. The first step is to turn the keyword into a matrix. If the keyword was longer than the 4 letters needed, we would only take the first 4 letters, and if it was shorter, we would fill it up with the alphabet...

Mono alphabetic cipher

                                               Mono alphabetic cipher encryption-decryption A mono-alphabetic cipher is a type of simple substitution cipher. In this cipher technique, each letter of the plaintext is replaced by another letter in the cipher-text. An example of a mono-alphabetic cipher key follows: Plain Text   >>>   a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z Cipher Text >>>  z  w  x  y  o  p  r  q  a  h  c  b  e  s  u  t  v  f  g  j  l  k  m  n  d  i This key means that any ‘a’ in the plaintext will be replaced by a ...