El deCifrado

 So, recently I have been trying to study my academics as I will be having my EndSems from the 14th of this month. It's an achilles heel for me, but I gotta do it. I had a thought about the cifrado function that I had made and posted, and then I thought of making a function that would take the level of encryption of any string and that would show us the original string, which would be very interesting and exciting to use. As it can help us to send secret messages to anyone and the level of encryption would be the key for deEncrypting the message. Although I am thinking if this can have some vulnerabilities, but I am not very sure if it does.

Well, this is the whole program, with a bit of user friendly behavior at the end, although it would have been much better if I could have used the clrscr() function, but I couldn't use it so, it becomes a bit of a mess in the screen. 

I have my Duo lessons left for the day, along with my daily words and lessons. The academics is really a poison and it's just disgusting as to how we gotta learn these balderdash claptrap things.

#include <iostream>

#include<vector>

using namespace std;

void deCifrado(vector<string>& s,int k){
    int val;
    int s_size=s.size();
    for(int i=0;i<s_size;i++){
      int s_elem_size=s[i].size();
      for(int j=0;j<s_elem_size;j++){
        int current_elem=s[i].at(j);
        if(current_elem<97){
            val=current_elem-'A';
            s[i].at(j)=((val+(26-k))%26)+'A';
        }
        if(current_elem>=97){
            val=current_elem-'a';
            s[i].at(j)=((val+(26-k))%26)+'a';
        }
      }
    }
}

void deCifrado(){
     cout<<"Enter the number of words that you have in 
your encrypted string. "<<endl;

   int n;
   cin>>n;

   cout<<"Enter the encrypted string which you want to 
deEncrypt."<<endl;

   vector<string> s(n);

   for(int i=0;i<n;i++){

       cin>>s[i];

   }

   cout<<"What was the level of encryption of your encrypted 
string from 0 to 30. "<<endl;

   int k;
   cin>>k;
   
   int val;
   int s_size=s.size();
    
   for(int i=0;i<s_size;i++){
      int s_elem_size=s[i].size();
      
      for(int j=0;j<s_elem_size;j++){
        int current_elem=s[i].at(j);
          if(current_elem<97){
             val=current_elem-'A';
             s[i].at(j)=((val+(26-k))%26)+'A';
            }
          if(current_elem>=97){
             val=current_elem-'a';
             s[i].at(j)=((val+(26-k))%26)+'a';
            }
        }  
    }
    cout<<"The deEncrypted version of your string is:- \n";
    for(string str:s){
        cout<<str<<" ";
    }cout<<endl;
}

void cifrado(vector<string>& s,int k){

  int val;

  int s_size=s.size();

  for(int i=0;i<s_size;i++){

     int s_0=s[i].size();

     for(int j=0;j<s_0;j++){

      if(s[i].at(j)>=97){

          val=s[i].at(j)-'a';

          val=(val+k)%26;

          

          s[i].at(j)='a'+val;

      }

      if(s[i].at(j)<97){

          val=s[i].at(j)-'A';

          val=(val+k)%26;

          s[i].at(j)='A'+val;

      }

  }    

  }

}

int main() {

  /* This is a program to take an input

   * string from the user and encrypt it by 

   * My personal method.

   */

   cout<<"Enter the number of words that you want to 
input and encrypt."<<endl;

   int n;
   cin>>n;

   cout<<"Enter the string which you want to encrypt."<<endl;

   vector<string> str(n);

   for(int i=0;i<n;i++){

       cin>>str[i];

   }

   cout<<"Now enter the level of encryption, from 0 to 30, and 
remember this level if you wish to deEncrypt this string 
in future. "<<endl;

   int k;
   cin>>k;

   cout<<"The encrypted version of your string is:- ";

   cifrado(str,k);

  for(string s:str){

      cout<<s<<" ";

  }cout<<endl;
  
   cout<<"Do you want to deEncrypt your current 
string?\n1:\tYes\n0:\tNo\n";
   int choice;
   cin>>choice;
   cout<<endl;
   if(choice==1){
       deCifrado(str,k);
       cout<<"The deEncrypted string is:- ";
   
       for(string s:str){
           cout<<s<<" ";
       }cout<<endl;
   
   }
   cout<<"Do you want to use the deEncrypt function for 
any other string?\n1:\tYes\n0:\tNo\n ";
   cin>>choice;
   
   if(choice==1){
       int choice1;
       
       while(true){
       cout<<"1:\tTo enter an encrypted string which 
you want to deEncrypt\n";
       cout<<"0:\tTo exit\n";
           cin>>choice1;cout<<"\n";
           switch(choice1){
               case 1:{
                    deCifrado();
                    break;
               }
                case 0:{
                    cout<<"Thanks for using this program :)";
                    return 0;     
                }
                default:{
                    cout<<"Enter a valid choice please!\n";
                    break;
                }
                
           }
       }
   }
    return 0;

}

Comments

Popular Posts