-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_main.cpp
More file actions
359 lines (299 loc) · 10.3 KB
/
Copy pathclient_main.cpp
File metadata and controls
359 lines (299 loc) · 10.3 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#include <iostream>
#include <fstream>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <string>
#include "encrypt.cpp"
#include "decrypt.cpp"
#include "client_socket.cpp"
using namespace std;
class personal_encyption{
// Simple XOR-based encryption function
std::string encrypt(const std::string& message, const std::string& key) {
std::string encrypted;
for (size_t i = 0; i < message.size(); ++i) {
encrypted += message[i] ^ key[i % key.size()]; // XOR with corresponding character from key
}
return encrypted;
}
// Simple XOR-based decryption function
std::string decrypt(const std::string& encrypted, const std::string& key) {
std::string decrypted;
for (size_t i = 0; i < encrypted.size(); ++i) {
decrypted += encrypted[i] ^ key[i % key.size()]; // XOR with corresponding character from key
}
return decrypted;
}
// Base64 encoding table
const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
// Function to encode a string to Base64
std::string base64_encode(const std::string &input) {
std::string encoded;
size_t i = 0;
size_t j = 0;
unsigned char char_array_3[3];
unsigned char char_array_4[4];
for (auto c : input) {
char_array_3[i++] = c;
if (i == 3) {
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for (i = 0; i < 4; i++) {
encoded += base64_chars[char_array_4[i]];
}
i = 0;
}
}
if (i) {
for (j = i; j < 3; j++) {
char_array_3[j] = '\0';
}
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
for (j = 0; j < i + 1; j++) {
encoded += base64_chars[char_array_4[j]];
}
while (i++ < 3) {
encoded += '=';
}
}
return encoded;
}
// Function to decode a Base64 string
std::string base64_decode(const std::string &encoded_string) {
size_t in_len = encoded_string.size();
size_t i = 0;
size_t j = 0;
int in_ = 0;
unsigned char char_array_4[4], char_array_3[3];
std::string decoded;
while (in_len-- && (encoded_string[in_] != '=') &&
(isalnum(encoded_string[in_]) || (encoded_string[in_] == '+') || (encoded_string[in_] == '/'))) {
char_array_4[i++] = encoded_string[in_];
in_++;
if (i == 4) {
for (i = 0; i < 4; i++) {
char_array_4[i] = base64_chars.find(char_array_4[i]);
}
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (i = 0; i < 3; i++) {
decoded += char_array_3[i];
}
i = 0;
}
}
if (i) {
for (j = i; j < 4; j++) {
char_array_4[j] = 0;
}
for (j = 0; j < 4; j++) {
char_array_4[j] = base64_chars.find(char_array_4[j]);
}
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
for (j = 0; j < i - 1; j++) {
decoded += char_array_3[j];
}
}
return decoded;
}
public:
std::string key;
personal_encyption(string key)
{
this->key=key;
}
string encryption(string message){
string a=base64_encode(encrypt(message, key));
if(a.length()>=16)
return a.substr(0,16);
return a+a.substr(0,16-a.length());
}
string decryption(string cyphertext)
{
return decrypt(base64_decode(cyphertext), key);
}
};
class InvertedIndex {
private:
unordered_map<string, unordered_set<string>> invIndex;
unordered_set<string> stop_word;
string password;
public:
vector<string> file_names;
void create_inv_index() {
int noFile;
cout << "ENTER NUMBER OF FILES FOR OUTSOURCING: ";
cin >> noFile;
// storing stop words in set
ifstream file("stop_word.txt");
if (!file.is_open()) {
cout << "Error opening file: stop_word.txt\n";
exit(1); // Exit with error code 1
}
string word;
while(file>>word)
{
stop_word.insert(word);
}
for (int i = 0; i < noFile; i++) {
string file_name;
cout << "ENTER FILE NAME: ";
cin >> file_name;
ifstream file(file_name+".txt");
if (!file.is_open()) {
cout << "Error opening file: " << file_name << endl;
exit(1); // Exit with error code 1
}
// Read file contents into stringstream
stringstream buffer;
buffer << file.rdbuf();
// Tokenize and index the file content
makeIndex(buffer.str(), file_name);
file_names.push_back(file_name+".dat");
// encrypt the file using AES cbc
AES_encrypt(file_name);
file.close();
}
file_names.push_back("encrypted_inverted_index.txt");
}
// Function to tokenize and index the file content
void makeIndex(const string& s, string Id) {
string st = "";
for (char c : s) {
if ((c>='a' && c<='z') || (c>='A' && c<='Z') || (c>='0' && c<='9') || c=='"' || c=='\'') {
if(c>='A' && c<='Z')
c=tolower(c);
st += c; // Convert characters to lowercase
} else if (!st.empty()) {
if(stop_word.find(st)==stop_word.end())
invIndex[st].insert(Id); // Store document ID for the current word
st = ""; // Reset the word
}
}
if (!st.empty()) {
if(stop_word.find(st)==stop_word.end())
invIndex[st].insert(Id); // Store document ID for the last word
}
}
void printInvertedIndex() {
for (const auto& word : invIndex) {
cout << word.first << " --> ";
for (const string& docId : word.second) {
cout << docId << " ";
}
cout << endl;
}
}
// Serialize the encrypted inverted index to a file
void store_encrypted_invindex(const string& filename) {
personal_encyption enc_dec(password);
ofstream outFile(filename);
if (!outFile.is_open()) {
cerr << "Error opening file for serialization: " << filename << endl;
return;
}
for (const auto& word : invIndex) {
outFile << enc_dec.encryption(word.first) << " ";
// Encrypt document IDs and store them in the file
for (const string& file_id : word.second) {
outFile << file_id <<" ";
}
outFile << endl;
}
outFile.close();
}
// Function to search for a keyword in the encrypted inverted index
void search(const string& keyword) {
// Encrypt keyword
personal_encyption enc_dec(password);
string encryptedKeyword = enc_dec.encryption(keyword);
// Perform search using encrypted keyword
// auto it = invIndex.find(encryptedKeyword);
auto it = invIndex.find(keyword);
if (it != invIndex.end()) {
cout << "Keyword found. Files present in location: ";
for (const string& loc : it->second) {
cout << loc << " ";
}
cout << endl;
} else {
cout << "Keyword not found." << endl;
}
}
void get_password(const string password){
this->password=password;
}
};
int main() {
InvertedIndex indexing;
string pass,keyword;
vector<string> querys;
cout<<"ENTER PASSWORD\n";
cin>>pass;
personal_encyption hash(pass);
int token;
cout<<"ENTER OPTION OF CLIENT \n";
cout<<"PRESS \n 1 --> FOR ENCRYPTING FILES with INVERTED INDEX\n 2 --> FOR QUERY RETRIEVAL\n 3 --> FOR UPLOADING FILES\n 4 -->DECRYPTING RECIEVED FILES\n";
while(true){
cin>>token;
switch(token){
case 1:
cout<<"ENCRYPTING FILES.. \n";
indexing.get_password(pass);
key_generate();
// Create index from files
indexing.create_inv_index();
// Print index
// indexing.printInvertedIndex();
// Serialize and store the encrypted index to a file
indexing.store_encrypted_invindex("encrypted_inverted_index.txt");
break;
case 2:
cout<<"QUERY RETRIEVAL \n";
// Search for a keyword
cout<<"ENTER KEYWORDS TO RETRIEVE DATA (press (end) to terminate query at last )\n";
cin>>keyword;
while(keyword!="end"){
string enc_key=hash.encryption(keyword);
cout<<enc_key<<" as query \n";
querys.push_back(enc_key);
cin>>keyword;
}
sendFiles_to_server(querys,false);
cout<< "query received\n";
break;
case 3:
cout<<"UPLOADING FILES... \n";
// upload files to server
sendFiles_to_server(indexing.file_names,true);
break;
case 4:
cout<<"DECRYPTING RECIEVED FILES\n";
AES_decrypt(received_files);
break;
default :return 0;
}
}
return 0;
}
/*
ameer123@khan
3
file1.txt
file2.txt
file3.txt
g++ -o program client_main.cpp -lssl -lcrypto
./program
cd /mnt/c/users/adnan/C_Tutorials/project/searchable_encryption
*/