-
Notifications
You must be signed in to change notification settings - Fork 0
/
encryption.cpp
45 lines (35 loc) · 1014 Bytes
/
encryption.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "encryption.h"
#include <fstream>
#include <cctype>
using namespace std;
bool performCeaserCipher(string& content, bool encrypt){
//if true positive 3 else -3
int shift= encrypt? 3:-3;
for(char& ch :content){
if(isalpha(ch)){
char base = isupper(ch)? 'A' : 'a';
ch = static_cast<char>((ch-base+ shift +26)% 26 + base);
}
}
return true;
}
bool encryptFile(const string& filename, bool encrypt){
//Open the input file
ifstream inFile (filename);
if(!inFile){
return false;
}
//read the contents of the file
string content(istreambuf_iterator<char>(inFile),{});
inFile.close();
if(performCeaserCipher(content, encrypt)){
//Creating an output file and writing the modified content
ofstream outFile(encrypt ? "encrypted_"+filename : "decrypted_"+ filename);
if(!outFile){
return false;
}
outFile << content;
outFile.close();
return true;
}
}