Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Program to accept secret message and encrypt and decrypt it
- */
- #include<iostream>
- #include<string>
- using namespace std;
- // function to find the position of each letter in the key
- size_t find_pos(const char ch, const string str)
- {
- size_t pos{}, rpos{str.length()-1}; // rpos is reverse position to search the number from the end of the string simultaneously
- while(pos <= rpos)
- {
- if(str[pos]==ch)
- return pos;
- else if(str[rpos]==ch)
- return rpos;
- pos++, rpos--;
- }
- return string::npos;
- }
- // Function to encrypt the plain-text
- string encrypt(const string &txt, const string &alpha, const string &key)
- {
- string ciphered;
- size_t key_pos;
- for(size_t i{}; i<txt.length(); i++)
- {
- key_pos = find_pos(txt[i], alpha);
- if(key_pos!=string::npos)
- ciphered += key[key_pos];
- else
- ciphered += txt[i];
- }
- return ciphered;
- }
- // Function to decrypt the cipher
- string decrypt(const string &c_txt, const string &alpha, const string &key)
- {
- string deciphered;
- size_t alpha_pos;
- for(size_t i{}; i<c_txt.length(); i++)
- {
- alpha_pos = find_pos(c_txt[i], key);
- if(alpha_pos!=string::npos)
- deciphered += alpha[alpha_pos];
- else
- deciphered += c_txt[i];
- }
- return deciphered;
- }
- // Function to print menu and prompt user to choose
- int menu()
- {
- int choice;
- cout << "========================" << endl;
- cout << " ENCYPTION/DECRYPTION" << endl;
- cout << "========================" << endl;
- cout << " Choose an option " << endl;
- cout << "1. Enter a Message." << endl;
- cout << "2. Encrypt a Message." << endl;
- cout << "3. Decrypt a Message." << endl;
- cout << "4. Exit." << endl;
- cout << "========================" << endl;
- cout << "Enter : ";
- while(!(cin>>choice))
- {
- cout << "\nWrong choice!\nPlease Enter Again : ";
- cin.clear();
- cin.ignore(' ', '\n');
- }
- return choice;
- }
- int main()
- {
- const string alphabets {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
- const string key {"QWERTYUIOPasdfghjklZXCVBNMqwertyuiopASDFGHJKLzxcvbnm"};
- string message, ciphered_txt, deciphered_txt;
- int choice;
- do
- {
- choice = menu();
- switch(choice)
- {
- case 1:
- char ch;
- if(message.length()) // Condition to check if there is already a message
- {
- cout << "\nYou've already entered a Message.";
- cout << "\nIf you re-enter, previous Message will be over-written";
- cout << "\nContinue Overwriting? (Y/N): ";
- cin >> ch;
- while(ch!='n'&&ch!='N'&&ch!='y'&&ch!='Y') // condition to check wrong choice
- {
- cout <<"\nWrong choice entered\n";
- cout <<"Enter again : ";
- cin >> ch;
- }
- if(ch=='n' || ch=='N')
- {
- cout << endl;
- break;
- }
- }
- cin.clear();
- cin.ignore(' ', '\n');
- cout << "\nEnter your Mssage\n";
- getline(cin, message);
- cout << "Your Message has been saved!\n\n";
- break;
- case 2:
- if(message.length()) // if there is a message
- {
- ciphered_txt = encrypt(message, alphabets, key);
- cout << "\n-------------------------------\n";
- cout << "Your Message has been Encrypted!\n\n";
- cout << " Original Message:\n" << "\"" << message << "\"\n\n";
- cout << " Encrypted Message:\n" << "\"" << ciphered_txt << "\"";
- cout << "\n-------------------------------\n\n";
- }
- else
- cout << "\nThere is no message to Encrypt!\n\n";
- break;
- case 3:
- if(ciphered_txt.length()) // if there is a ciphered text
- {
- deciphered_txt = decrypt(ciphered_txt, alphabets, key);
- cout << "\n-------------------------------\n";
- cout << "Your Message has been Decrypted!\n\n";
- cout << " Encrypted Message:\n" << "\"" << ciphered_txt << "\"\n\n";
- cout << " Decrypted Message:\n" << "\"" << deciphered_txt << "\"";
- cout << "\n-------------------------------\n";
- }
- else
- cout << "\nThere is no Encrypted message to Decrypt!\n\n";
- break;
- case 4:
- cout << "\nThank You!\n";
- break;
- default:
- cout << "\nWrong choice! Please try again!\n";
- }
- }
- while(choice!=4);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement