m2skills

vignere cipher cpp

Sep 3rd, 2017
2,774
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.03 KB | None | 0 0
  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. // method that recives string as input and returns encrypted string as output
  12. string encrypt(string original_msg, string key){
  13.  
  14.     // allchars is our keyspace
  15.     string allChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  16.     int all_chars_length = allChars.length();
  17.  
  18.     // converting our string to uppercase
  19.     transform(original_msg.begin(), original_msg.end(), original_msg.begin(), ::toupper);
  20.    
  21.     // removing all numbers, punctuations
  22.     original_msg.erase(remove_if(original_msg.begin(), original_msg.end(), [](char c) { return (!isalpha(c) && c!='\n' && c!='\r'); } ), original_msg.end());
  23.    
  24.     // converting key to uppercase
  25.     transform(key.begin(), key.end(), key.begin(), ::toupper);
  26.     cout<<original_msg<<endl;
  27.     cout<<key<<endl;
  28.     int index = 0;
  29.     string encrypted_msg = "";
  30.    
  31.     // iterating over the original message character by chracter and encrypting it and appending it to the new string
  32.     for(int i=0; i< original_msg.length(); i++){
  33.         char letter = original_msg[i];
  34.         if(isalpha(letter)) {
  35.             int position = (allChars.find(letter) + allChars.find(key[index])) % 26;
  36.             char e_letter = allChars[position];
  37.             encrypted_msg += e_letter;
  38.             index++;
  39.             // reset index if it is greater that key length
  40.             if(index >= key.length()){
  41.                 index %= key.length();
  42.             }
  43.         // case when \n or \r is encountered   
  44.         }else{
  45.             encrypted_msg += letter;
  46.             index = 0;
  47.         }
  48.     }
  49.  
  50.     cout<<encrypted_msg;
  51.     return encrypted_msg;
  52. }
  53.  
  54.  
  55. // method that encrypts file
  56. static void encrypt_file(string key){
  57.     ifstream input_file;
  58.     string file_name, data;
  59.     cout<<"Enter File name : ";
  60.     cin>>file_name;
  61.    
  62.     // reading the file
  63.     try {
  64.         input_file.open(file_name);
  65.         stringstream ss;
  66.         ss << input_file.rdbuf();
  67.         data = ss.str();
  68.         cout<<"File read successufully!!"<<endl;
  69.     } catch(exception e){
  70.         cout<<e.what();
  71.     }
  72.    
  73.     // encrypt the string read
  74.     string encrypted_data = encrypt(data, key);
  75.     cout<<"File data encrypted successfully!!"<<endl;
  76.     ofstream output_file;
  77.     // save the encrypted string in the file
  78.     try {
  79.         output_file.open("C:\\Users\\MOHIT\\Desktop\\ss.txt");
  80.         output_file << encrypted_data;
  81.         output_file.close();
  82.         cout<<"encrypted file stored on Desktop!!"<<endl;
  83.     }catch(exception ee){
  84.         cout<<ee.what();
  85.     }
  86.  
  87.  
  88. }
  89.  
  90. int main()
  91. {
  92.     cout<<"Program to implement Vignere Cipher"<<endl;
  93.     cout<<"Enter the Encryption key : ";
  94.     string key;
  95.     cin>>key;
  96.     encrypt_file(key);
  97. }
  98.  
  99. /*
  100. Output for encrypting a file
  101.  
  102.  
  103. Enter File name :F:/python_3.4/sample.txt
  104. Enter File name : F:/python_3.4/sample.txt
  105. File read successufully!!
  106. File data encrypted successfully!!
  107. encrypted file stored on Desktop!!
  108.  
  109.  
  110.  */
Add Comment
Please, Sign In to add comment