-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencryption.cpp
192 lines (154 loc) · 3.84 KB
/
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <string>
#include <vector>
#include <fstream>
#include "encryption.h"
using namespace std;
/*!
* Constructor
* @param key - password used to encrypt and decrypt
*/
encryptor::encryptor(const string& key):
key(key) {};
/*!
* Compares given password with password used to encrypt
* @param user_key - password to verify
* @return - true if passwords match, otherwise false
*/
bool encryptor::validate_key(const string& user_key) const {
return (!user_key.compare(key)) ? true : false;
}
/*!
* Encrypts file
* @param file_name - name of file to be encrypted
*/
void encryptor::encrypt_file(const string& file_name) const {
fstream file(file_name, ios::binary | ios::in);
if (!file.is_open()) {
file.close();
throw invalid_argument("File does not exist");
}
string contents;
while (file.good()) {
contents += file.get();
}
if (file.bad()) {
file.close();
throw runtime_error("IO Error");
}
file.close();
file.open(file_name, ios::trunc | ios::out | ios::binary);
for (size_t i = 0; i < key.length(); ++i) {
file.put(key[i] >> 1);
}
file.put('\n');
for (size_t i = 0; i < contents.length() - 1; ++i) {
file.put(contents[i] ^ key[i % key.length()]);
}
file.close();
}
/*!
* Decrypts file
* @param file_name - name of file to be decrypted
*/
void encryptor::decrypt_file(const string& file_name) const {
fstream file(file_name, ios::binary | ios::in);
if (!file.is_open()) {
file.close();
throw invalid_argument("File does not exist");
}
string hashed_key;
getline(file, hashed_key);
if (key.length() == hashed_key.length()) {
for (size_t i = 0; i < key.length(); ++i) {
if ((key[i] >> 1) != hashed_key[i]) {
throw runtime_error("Password is invalid");
}
}
}
else {
throw runtime_error("Password is invalid");
}
string contents;
while (file.good()) {
contents += file.get();
}
if (file.bad()) {
file.close();
throw runtime_error("IO Error");
}
file.close();
file.open(file_name, ios::trunc | ios::out | ios::binary);
for (size_t i = 0; i < contents.length() - 1; ++i) {
file.put(contents[i] ^ key[i % key.length()]);
}
file.close();
}
/*!
* Imports file
* @param file_name - name of file to be imported
* @param buffer - where file is read into
*/
void encryptor::import_file(const string& file_name, vector<string>* buffer) const {
fstream file(file_name, ios::binary | ios::in);
buffer->push_back("");
if (!file.is_open()) {
throw invalid_argument("File does not exist");
}
string hashed_key;
getline(file, hashed_key);
if (key.length() == hashed_key.length()) {
for (size_t i = 0; i < key.length(); ++i) {
if ((key[i] >> 1) != hashed_key[i]) {
throw runtime_error("Password is invalid");
}
}
}
else {
throw runtime_error("Password is invalid");
}
size_t line = 0;
char ch;
for (size_t i = 0; file.good(); ++i) {
ch = file.get() ^ key[i % key.length()];
if (ch == '\n') {
++line;
buffer->push_back("");
}
else {
buffer->at(line) += ch;
}
}
buffer->back().pop_back();
if (file.bad()) {
file.close();
throw runtime_error("IO Error");
}
file.close();
}
/*!
* Exports file
* @param filename - name of file to be exported
* @param data - file contents
*/
void encryptor::export_file(const string& filename, vector<string>* data) const {
fstream file(filename, ios::binary | ios::trunc | ios::out);
if (!file.is_open()) {
throw invalid_argument("File does not exist");
}
for (size_t i = 0; i < key.length(); ++i) {
file.put(key[i] >> 1);
}
file.put('\n');
size_t count = 0;
for (size_t i = 0; i < data->size(); ++i) {
for (size_t j = 0; j < data->at(i).length(); ++j) {
file.put(data->at(i).at(j) ^ key[count % key.length()]);
++count;
}
if (i != data->size() - 1 || data->at(i).length() != 0) {
file.put('\n' ^ key[count % key.length()]);
++count;
}
}
file.close();
}