document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. // program to implement ceasar cipher encryption
  2.  
  3. #include <iostream>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <fstream>
  7. #include <sstream>
  8.  
  9. using namespace std;
  10.  
  11. // function that takes original message and shift value as input and returns encrypted message string as output
  12. string encrypt(string original_msg, int shift_value){
  13.  
  14.     string allChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ !\\"#$%&\'()*+,-./:;<=>?@[\\\\]^_`{|}~";
  15.     int all_chars_length = allChars.length();
  16.  
  17.  
  18.     string encrypted_msg = "";
  19.  
  20.     for(int i=0; i< original_msg.length(); i++){
  21.         char letter = original_msg[i];
  22.         int position = allChars.find(letter);
  23.         if(position >= 0) {
  24.             position += shift_value;
  25.             position %= all_chars_length;
  26.             char e_letter = allChars[position];
  27.             encrypted_msg += e_letter;
  28.  
  29.         }else{
  30.             encrypted_msg += letter;
  31.         }
  32.     }
  33.  
  34.  
  35.     return encrypted_msg;
  36. }
  37.  
  38. // function the takes path of the file to be encrypted and then stores the encrypted file on desktop
  39. static void encrypt_file(int shift_value){
  40.     ifstream input_file;
  41.     string file_name, data;
  42.     cout<<"Enter File name : ";
  43.     cin>>file_name;
  44.     try {
  45.         input_file.open(file_name);
  46.         stringstream ss;
  47.         ss << input_file.rdbuf();
  48.         data = ss.str();
  49.         cout<<"File read successufully!!"<<endl;
  50.     } catch(exception e){
  51.         cout<<e.what();
  52.     }
  53.     string encrypted_data = encrypt(data, shift_value);
  54.     cout<<"File data encrypted successfully!!"<<endl;
  55.     ofstream output_file;
  56.     try {
  57.         output_file.open("C:\\\\Users\\\\MOHIT\\\\Desktop\\\\ss.txt");
  58.         output_file << encrypted_data;
  59.         output_file.close();
  60.         cout<<"Encrypted file stored on Desktop!!"<<endl;
  61.     }catch(exception ee){
  62.         cout<<ee.what();
  63.     }
  64.  
  65.  
  66. }
  67.  
  68. int main()
  69. {
  70.     //cout << encrypt("My name is mohit, hello", 3) << endl;
  71.     cout<<"Program to implement Ceasar Cipher"<<endl;
  72.     cout<<"Enter shift value for Encryption : ";
  73.     int shift_value;
  74.     cin>>shift_value;
  75.     encrypt_file(shift_value);
  76. }
  77.  
  78. /*
  79. Output for encrypting a file
  80.  
  81.  
  82. Enter File name :F:/python_3.4/sample.txt
  83. Enter File name : F:/python_3.4/sample.txt
  84. File read successufully!!
  85. File data encrypted successfully!!
  86. Encrypted file stored on Desktop!!
  87.  
  88.  
  89.  */
');