Playfair Cipher C++ Program - The Coding Shala

Home >> Computer Network >> Playfair Cipher

Playfair Cipher C++ Program

Here is the C++ program to implement the Playfair Cipher.

C++ Code: 

#include<bits/stdc++.h>

using namespace std;

void encrypt(string key){
 pair<char,pair<int,int> > pa[25]; //store i j value of char
 int visited[26] = {0};
 char a[5][5];
 char b[5][5];
 char ch1,ch2,c1,c2;
 int i,j,i1,j1,i2,j2;
 int count=0;
 for(i=0; i<5; i++){
  if(count==key.size()){
   break;
  }
  for(j=0; j<5; j++){
   if(count==key.size()){
    break;
   }else if(key[count]!='j'){ //skip j
    //cout << key[count] << endl;
    while(visited[key[count]-'a']==1){  //skip same char
     count++;
    }
    if(visited[key[count]-'a']==0){  //first time store it
     a[i][j]=key[count];
     visited[key[count]-'a']=1;
    }
   }
   count++;
  }
 }
 
 int p=i-1; 
 int q=j;
 i=0;  
 //making 5*5 matrix
 while(i<26){
  if(i!='j'-'a'){  //for j char
   if(visited[i]==0){
    if(q==5){
     q=0;
     p++;
    }
    a[p][q]='a'+i;
    q++;
   }
  }
  i++;
 }
 
 //copy a to b
 for(int i=0; i<5; i++){
  for(int j=0; j<5; j++){
   b[i][j] = a[i][j];
  }
 }
 
 
 //making pair
 for(int i=0; i<5; i++){
  for(int j=0; j<5; j++){
   pa[b[i][j]-'a'].first=a[i][j];
   pa[b[i][j]-'a'].second.first = i;
   pa[b[i][j]-'a'].second.second = j;
  }
 }
  
 FILE *fp1, *fp2;
 fp1 = fopen("encrypt.txt","r");
 fp2 = fopen("decrypt.txt","w");
 int flag=0;
 char c;
 do{
         if(flag==0){
          aa:
  ch1=fgetc(fp1);
  if(!isalpha(ch1) && ch1!=EOF) goto aa;
  }else{
  ch1=c;
  flag=0;
  }
  ab:
  ch2=fgetc(fp1);
  if(!isalpha(ch2) && ch2!=EOF) goto ch2;
  
  if(ch1==ch2){
  flag=1;
  c=ch2;
  ch2='x';
  }
  
  if(ch1!=EOF && ch2==EOF) {
  ch2='x';
  }
  if(ch1==EOF || ch2==EOF){
   break;
  }
  i1 = pa[ch1-'a'].second.first;  //take  first char row
  j1 = pa[ch1-'a'].second.second; // first char column
  i2 = pa[ch2-'a'].second.first;  // second char row
  j2 = pa[ch2-'a'].second.second;  // second char column
  if(pa[ch1-'a'].second.first==pa[ch2-'a'].second.first){   
                       //check same row
   c1 = a[i1][(j1+1)%5];      
   c2 = a[i2][(j2+1)%5];
  }else if(pa[ch1-'a'].second.second==pa[ch2-'a'].second.second){ 
                  // check same column
   c1 = a[(i1+1)%5][j1];
   c2 = a[(i2+1)%5][j2];
  }else{    // digonal interchange
   c1 = a[i1][j2];
   c2 = a[i2][j1];
  }
  //cout << ch1 << " " << ch2;
  fputc(c1,fp2);
  fputc(c2,fp2);
 }while(ch1 != EOF || ch2!=EOF);
 
 fclose(fp1);
 fclose(fp2);
 
 //fp2 = fopen("p2.txt","r");
 
 //do{
 // ch1=fgetc(fp2);
//cout << ch1;
 //}while(ch1!=EOF);


}

void decrypt(string key){
 pair<char,pair<int,int> > pa[25];
 int visited[26] = {0};
 char a[5][5];
 char b[5][5];
 char ch1,ch2,c1,c2;
 int i,j,i1,j1,i2,j2;
 int count=0;
 for(i=0; i<5; i++){
  if(count==key.size()){
   break;
  }
  for(j=0; j<5; j++){
   if(count==key.size()){
    break;
   }else if(key[count]!='j'){
    //cout << key[count] << endl;
    while(visited[key[count]-'a']==1){
     count++;
    }
    if(visited[key[count]-'a']==0){
     a[i][j]=key[count];
     visited[key[count]-'a']=1;
    }
   }
   count++;
  }
 }
 
 int p=i-1;
 int q=j;
 i=0;
 while(i<26){
  if(i!='j'-'a'){
   if(visited[i]==0){
    if(q==5){
     q=0;
     p++;
    }
    a[p][q]='a'+i;
    q++;
   }
  }
  i++;
 }
 
 for(int i=0; i<5; i++){
  for(int j=0; j<5; j++){
   b[i][j] = a[i][j];
  }
 }
 
 
 for(int i=0; i<5; i++){
  for(int j=0; j<5; j++){
   pa[b[i][j]-'a'].first=a[i][j];
   pa[b[i][j]-'a'].second.first = i;
   pa[b[i][j]-'a'].second.second = j;
  }
 }
  
 FILE *fp1, *fp2;
 fp1 = fopen("decrypt.txt","r");
 fp2 = fopen("encrypt.txt","w");
 
 do{
  ch1=fgetc(fp1);
  ch2=fgetc(fp1);
  if(ch1==EOF || ch2==EOF){
   break;
  }
  i1 = pa[ch1-'a'].second.first;
  j1 = pa[ch1-'a'].second.second;
  i2 = pa[ch2-'a'].second.first;
  j2 = pa[ch2-'a'].second.second;
  if(pa[ch1-'a'].second.first==pa[ch2-'a'].second.first){
   c1 = a[i1][(j1+4)%5];
   c2 = a[i2][(j2+4)%5];
  }else if(pa[ch1-'a'].second.second==pa[ch2-'a'].second.second){
   c1 = a[(i1+4)%5][j1];
   c2 = a[(i2+4)%5][j2];
  }else{
   c1 = a[i1][j2];
   c2 = a[i2][j1];
  }
  //cout << ch1 << " " << ch2;
  fputc(c1,fp2);
  fputc(c2,fp2);
 }while(ch1 != EOF || ch2!=EOF);
 
 fclose(fp1);
 fclose(fp2);
 
 //fp2 = fopen("p1.txt","r");
 
 //do{
 // ch1=fgetc(fp2);
 // cout << ch1;
//while(ch1!=EOF);


}

int main(){
 string key;
 int ch;
 start:
  cout<<"Playfair cipher\n";
 cout << "1.encrypt\n 2.decrypt\n 3.exit\n";
 cin >> ch;
 switch(ch){
  case 1:
   cout << "enter key  :\n";
   cin >> key;
   encrypt(key);
   goto start;
   break;
  case 2:
   cout << "enter key  :\n";
   cin >> key;
   decrypt(key);
   goto start;
   break;
  case 3:
   exit(0);
 }
 return 0;
}



Other Posts You May Like

Vigenere Cipher C++ Program
Caesar Cipher C++ Program
Please leave a comment below if you like this post or found some error, it will help me to improve my content.

Comments

Popular Posts from this Blog

Shell Script to find sum, product and average of given numbers - The Coding Shala

LeetCode - Bulb Switcher Solution - The Coding Shala

Anti Diagonals - The Coding Shala

Java Method Overloading - The Coding Shala

Sorting the Sentence LeetCode Solution - The Coding Shala