m2skills

vignere decipher cpp

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