Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // program to implement ceasar cipher decryption
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <fstream>
- #include <sstream>
- using namespace std;
- string decrypt(string original_msg, int shift_value){
- string allChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
- int all_chars_length = allChars.length();
- string decrypted_msg = "";
- for(int i=0; i< original_msg.length(); i++){
- char letter = original_msg[i];
- int position = allChars.find(letter);
- if(position >= 0) {
- position -= shift_value;
- if(position < 0){
- position += all_chars_length;
- }
- position %= all_chars_length;
- char e_letter = allChars[position];
- decrypted_msg += e_letter;
- }else{
- decrypted_msg += letter;
- }
- }
- return decrypted_msg;
- }
- static void decrypt_file(int shift_value){
- ifstream input_file;
- string file_name, data;
- cout<<"Enter File name : ";
- cin>>file_name;
- try {
- input_file.open(file_name);
- stringstream ss;
- ss << input_file.rdbuf();
- data = ss.str();
- cout<<"File read successufully!!"<<endl;
- } catch(exception e){
- cout<<e.what();
- }
- string decrypted_data = decrypt(data, shift_value);
- cout<<"File data decrypted successfully!!"<<endl;
- ofstream output_file;
- try {
- output_file.open("C:\\Users\\MOHIT\\Desktop\\ss.txt");
- output_file << decrypted_data;
- output_file.close();
- cout<<"decrypted file stored on Desktop!!"<<endl;
- }catch(exception ee){
- cout<<ee.what();
- }
- }
- int main()
- {
- //cout << decrypt("My name is mohit, hello", 3) << endl;
- cout<<"Program to implement Ceasar Cipher"<<endl;
- cout<<"Enter shift value for decryption : ";
- int shift_value;
- cin>>shift_value;
- decrypt_file(shift_value);
- }
- /*
- Output for decrypting a file
- Program to implement Ceasar Cipher
- Enter shift value for decryption : 3
- Enter File name : F:/python_3.4/sample.txt
- File read successufully!!
- File data decrypted successfully!!
- decrypted file stored on Desktop!!
- */
Add Comment
Please, Sign In to add comment