Caesar Cipher C++ Program - The Coding Shala

Home >> Computer Network >> Caesar Cipher

Caesar Cipher C++ Program

Here is the C++ Program to implement the Caesar Cipher.

C++ Code: 

#include<bits/stdc++.h>

using namespace std;

int findmax(int* a){
 int max = -1;
 int maxindex;
 for(int i=0; i<26; i++){
  if(a[i]>max){
   max = a[i];
   maxindex = i;
  }
 }
 
 a[maxindex] = -1;
 return maxindex;
}


void encript(){
              int key;
              cout<<"enter the key\n";
              cin>>key;
              // char *file;
              //cout<<"enter the file name\n";  
              //cin>>file; 
              FILE *fp1, *fp2;
              fp1 = fopen("encript.txt", "r");
              fp2 = fopen("decript.txt", "w");
              char ch;
              while((ch=fgetc(fp1))!=EOF){
              //cout<<ch;
              if(isalpha(ch)){
              if(ch>='A' && ch<='Z'){
              char ot = ((((ch-'A')+key)%26)+'A');
              fputc(ot, fp2);
              }
             else if(ch>='a' && ch<='z') {
             char ot = ((((ch-'a')+key)%26)+'a');
             fputc(ot, fp2);
             }
             }
            else continue;
            }
            //cout<<"\n";
            fclose(fp1); 
            fclose(fp2); 
}

void decript(){
            int key;
            cout<<"enter the key\n";
            cin>>key;
            key = key%26;
            // char *file;
            //cout<<"enter the file name\n";  
            //cin>>file; 
            FILE *fp1, *fp2;
            fp1 = fopen("encript.txt", "w");
            fp2 = fopen("decript.txt", "r");
            char ch;
            while((ch=fgetc(fp2))!=EOF){
            //cout<<ch;
            if(ch>='A' && ch<='Z'){
            char ot = (((((ch-'A')-key)+26)%26)+'A');
            fputc(ot, fp1);
            }
            else if(ch>='a' && ch<='z') {
           char ot = (((((ch-'a')-key)+26)%26)+'a');
           fputc(ot, fp1);
           }
           }
        cout<<"\n";
        fclose(fp1); 
        fclose(fp2);
}

void bruteforce(){ 
                FILE *fp1, *fp2;
                char next='n'; 
                int key = 1;
                char  ch;
                while(key<=26){
                         cout<<"\n";
                         fp1 = fopen("encript.txt", "w");
                         fp2 = fopen("decript.txt", "r");
                         while((ch=fgetc(fp2))!=EOF){
                        if(ch>='A' && ch<='Z'){
                        char ot = (((((ch-'A')-key)+26)%26)+'A');
                        cout<<ot;
                         }
                         else if(ch>='a' && ch<='z') {
                         char ot = (((((ch-'a')-key)+26)%26)+'a');
                       cout<<ot;
                        }
                        }
                        cout<<"\nenter y if text is readable else n \n";
                        cin>>next;
                        if(next=='y') break;
                        else key++;
                        fclose(fp1); 
                        fclose(fp2);  
                } 
                 fp1 = fopen("encript.txt", "w");
                 fp2 = fopen("decript.txt", "r");
                while((ch=fgetc(fp2))!=EOF){
                //cout<<ch;
                if(ch>='A' && ch<='Z'){
                char ot = (((((ch-'A')-key)+26)%26)+'A');
                fputc(ot, fp1);
                 }
                 else if(ch>='a' && ch<='z') {
                 char ot = (((((ch-'a')-key)+26)%26)+'a');
                 fputc(ot, fp1);
                }
                } 
                fclose(fp1); 
                fclose(fp2);    
                
}

void frequency(){
  int sample[26] = {0};
  int decripttxt[26] = {0};
  char ch;
  FILE *fp1, *fp2, *fp3;
  fp1 = fopen("sample.txt", "r");
  fp2 = fopen("decript.txt", "r");
  while((ch=fgetc(fp1))!=EOF){ 
   if(isalpha(ch)){
    if(ch>='a' && ch<='z') sample[ch-'a']++;
    else if(ch>='A' && ch<='Z') sample[ch-'A']++;
   }else continue;
  }
  fclose(fp1);
  
  while((ch=fgetc(fp2))!=EOF){ 
   if(isalpha(ch)){
    if(ch>='a' && ch<='z') decripttxt[ch-'a']++;
    else if(ch>='A' && ch<='Z') decripttxt[ch-'A']++;
   }else continue;
  }
  fclose(fp2);
  
  int a = findmax(decripttxt);
  int b;
  char next='n';
  int key;
     while(next=='n'){
                  b = findmax(sample);
                   key = abs(a-b);
                         cout<<"\n";
                         fp2 = fopen("decript.txt", "r");
                         while((ch=fgetc(fp2))!=EOF){
                        if(ch>='A' && ch<='Z'){
                        char ot = (((((ch-'A')-key)+26)%26)+'A');
                        cout<<ot;
                         }
                         else if(ch>='a' && ch<='z') {
                         char ot = (((((ch-'a')-key)+26)%26)+'a');
                       cout<<ot;
                        }
                        }
                        cout<<"\nenter y if text is readable else n \n";
                        cin>>next;
                        if(next=='y') break;
                        fclose(fp2);  
                } 
                
                 fp3 = fopen("encript.txt", "w");
                 fp2 = fopen("decript.txt", "r");
                 
                 
                while((ch=fgetc(fp2))!=EOF){
                //cout<<ch;
                if(ch>='A' && ch<='Z'){
                char ot = (((((ch-'A')-key)+26)%26)+'A');
                fputc(ot, fp3);
                 }
                 else if(ch>='a' && ch<='z') {
                 char ot = (((((ch-'a')-key)+26)%26)+'a');
                 fputc(ot, fp3);
                }
                } 
                fclose(fp3); 
                fclose(fp2);  
  
}




int main(){
int ch;
start:
cout<<"Please choose option\n";
cout<<"1. Encript\n 2.Decript\n 3.Brute force\n 4.Frequency analysis\n 5.exit\n";
cin>>ch;
switch(ch){
   case 1:
          encript();
          goto start;
          break;
   case 2:
         decript();
         goto start;
         break;
   case 3:
         bruteforce();
         goto start;
         break;
   case 4: 
         frequency();
         goto start;
         break;
    case 5: exit(0);
   default:
        cout<<"enter correct option\n";
}
return 0;
}



Other Posts You May Like
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

Single Number 3 LeetCode Solution - The Coding Shala

LeetCode - Number of Good Pairs Solution - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

Java Method Overloading - The Coding Shala