// program to implement ceasar cipher encryption
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
#include <sstream>
using namespace std;
// function that takes original message and shift value as input and returns encrypted message string as output
string encrypt(string original_msg, int shift_value){
string allChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ !\\"#$%&\'()*+,-./:;<=>?@[\\\\]^_`{|}~";
int all_chars_length = allChars.length();
string encrypted_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;
position %= all_chars_length;
char e_letter = allChars[position];
encrypted_msg += e_letter;
}else{
encrypted_msg += letter;
}
}
return encrypted_msg;
}
// function the takes path of the file to be encrypted and then stores the encrypted file on desktop
static void encrypt_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 encrypted_data = encrypt(data, shift_value);
cout<<"File data encrypted successfully!!"<<endl;
ofstream output_file;
try {
output_file.open("C:\\\\Users\\\\MOHIT\\\\Desktop\\\\ss.txt");
output_file << encrypted_data;
output_file.close();
cout<<"Encrypted file stored on Desktop!!"<<endl;
}catch(exception ee){
cout<<ee.what();
}
}
int main()
{
//cout << encrypt("My name is mohit, hello", 3) << endl;
cout<<"Program to implement Ceasar Cipher"<<endl;
cout<<"Enter shift value for Encryption : ";
int shift_value;
cin>>shift_value;
encrypt_file(shift_value);
}
/*
Output for encrypting a file
Enter File name :F:/python_3.4/sample.txt
Enter File name : F:/python_3.4/sample.txt
File read successufully!!
File data encrypted successfully!!
Encrypted file stored on Desktop!!
*/