Advertisement
ankit_saiyan

Simple Cipher/Deipher Message [C++]

Apr 17th, 2021 (edited)
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.35 KB | None | 0 0
  1. /*
  2.  *  Program to accept secret message and encrypt and decrypt it
  3.  */
  4.  
  5. #include<iostream>
  6. #include<string>
  7.  
  8. using namespace std;
  9.  
  10. // function to find the position of each letter in the key
  11. size_t find_pos(const char ch, const string str)
  12. {
  13.     size_t pos{}, rpos{str.length()-1};     // rpos is reverse position to search the number from the end of the string simultaneously
  14.     while(pos <= rpos)
  15.     {
  16.         if(str[pos]==ch)
  17.             return pos;
  18.         else if(str[rpos]==ch)
  19.             return rpos;
  20.         pos++, rpos--;
  21.     }
  22.    
  23.     return string::npos;
  24. }
  25.  
  26. // Function to encrypt the plain-text
  27. string encrypt(const string &txt, const string &alpha, const string &key)
  28. {
  29.     string ciphered;
  30.     size_t key_pos;
  31.    
  32.     for(size_t i{}; i<txt.length(); i++)
  33.     {
  34.         key_pos = find_pos(txt[i], alpha);
  35.         if(key_pos!=string::npos)
  36.             ciphered += key[key_pos];
  37.         else
  38.             ciphered += txt[i];
  39.     }
  40.    
  41.     return ciphered;
  42. }
  43.  
  44. // Function to decrypt the cipher
  45. string decrypt(const string &c_txt, const string &alpha, const string &key)
  46. {
  47.     string deciphered;
  48.     size_t alpha_pos;
  49.    
  50.     for(size_t i{}; i<c_txt.length(); i++)
  51.     {
  52.         alpha_pos = find_pos(c_txt[i], key);
  53.         if(alpha_pos!=string::npos)
  54.             deciphered += alpha[alpha_pos];
  55.         else
  56.             deciphered += c_txt[i];
  57.     }
  58.    
  59.     return deciphered;
  60. }
  61.  
  62. // Function to print menu and prompt user to choose
  63. int menu()
  64. {
  65.     int choice;
  66.    
  67.     cout << "========================" << endl;
  68.     cout << "  ENCYPTION/DECRYPTION" << endl;
  69.     cout << "========================" << endl;
  70.     cout << "    Choose an option " << endl;
  71.     cout << "1. Enter a Message." << endl;
  72.     cout << "2. Encrypt a Message." << endl;
  73.     cout << "3. Decrypt a Message." << endl;
  74.     cout << "4. Exit." << endl;
  75.     cout << "========================" << endl;
  76.     cout << "Enter : ";
  77.    
  78.     while(!(cin>>choice))
  79.     {
  80.         cout << "\nWrong choice!\nPlease Enter Again : ";
  81.         cin.clear();
  82.         cin.ignore(' ', '\n');
  83.     }
  84.  
  85.     return choice;
  86. }
  87.  
  88. int main()
  89. {
  90.     const string alphabets {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
  91.     const string key {"QWERTYUIOPasdfghjklZXCVBNMqwertyuiopASDFGHJKLzxcvbnm"};
  92.     string message, ciphered_txt, deciphered_txt;
  93.     int choice;
  94.    
  95.     do
  96.     {
  97.         choice = menu();      
  98.         switch(choice)
  99.         {
  100.             case 1:
  101.                 char ch;
  102.                 if(message.length())        // Condition to check if there is already a message
  103.                 {
  104.                     cout << "\nYou've already entered a Message.";
  105.                     cout << "\nIf you re-enter, previous Message will be over-written";
  106.                     cout << "\nContinue Overwriting? (Y/N): ";
  107.                     cin >> ch;
  108.                    
  109.                     while(ch!='n'&&ch!='N'&&ch!='y'&&ch!='Y')   // condition to check wrong choice
  110.                     {
  111.                         cout <<"\nWrong choice entered\n";
  112.                         cout <<"Enter again : ";
  113.                         cin >> ch;
  114.                     }
  115.                    
  116.                     if(ch=='n' || ch=='N')
  117.                     {
  118.                         cout << endl;
  119.                         break;
  120.                     }
  121.                    
  122.                 }
  123.                
  124.                 cin.clear();
  125.                 cin.ignore(' ', '\n');
  126.                
  127.                 cout << "\nEnter your Mssage\n";
  128.                 getline(cin, message);
  129.                 cout << "Your Message has been saved!\n\n";
  130.                
  131.                 break;
  132.                
  133.             case 2:
  134.                 if(message.length())        // if there is a message
  135.                 {
  136.                     ciphered_txt = encrypt(message, alphabets, key);
  137.                     cout << "\n-------------------------------\n";
  138.                     cout << "Your Message has been Encrypted!\n\n";
  139.                     cout << " Original Message:\n" << "\"" << message << "\"\n\n";
  140.                     cout << " Encrypted Message:\n" << "\"" << ciphered_txt << "\"";
  141.                     cout << "\n-------------------------------\n\n";
  142.                 }
  143.                 else
  144.                     cout << "\nThere is no message to Encrypt!\n\n";
  145.                 break;
  146.                
  147.             case 3:
  148.                 if(ciphered_txt.length())       // if there is a ciphered text
  149.                 {
  150.                     deciphered_txt = decrypt(ciphered_txt, alphabets, key);
  151.                     cout << "\n-------------------------------\n";
  152.                     cout << "Your Message has been Decrypted!\n\n";
  153.                     cout << " Encrypted Message:\n" << "\"" << ciphered_txt << "\"\n\n";
  154.                     cout << " Decrypted Message:\n" << "\"" << deciphered_txt << "\"";
  155.                     cout << "\n-------------------------------\n";
  156.                 }
  157.                 else
  158.                     cout << "\nThere is no Encrypted message to Decrypt!\n\n";
  159.                 break;
  160.                
  161.             case 4:
  162.                 cout << "\nThank You!\n";
  163.                 break;
  164.  
  165.             default:
  166.                 cout << "\nWrong choice! Please try again!\n";
  167.         }
  168.     }
  169.     while(choice!=4);
  170.        
  171.     return 0;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement