forked from Suxyuuu/SWP-of-searchable-encryption
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.cc
618 lines (571 loc) · 18.2 KB
/
client.cc
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <sstream>
#include <grpc++/grpc++.h>
#include <grpc/support/log.h>
#include "protoc/rpc.grpc.pb.h"
#include "client.h"
#include "submodule/encryption.h"
#include "submodule/tools.h"
using grpc::Channel;
using grpc::ClientAsyncResponseReader;
using grpc::ClientContext;
using grpc::CompletionQueue;
using grpc::Status;
std::string Client::Setup()
{
SetupRequestMessage request;
request.set_setup_request("setup");
SetupResponseMessage reply;
ClientContext context;
CompletionQueue cq;
Status status;
std::unique_ptr<ClientAsyncResponseReader<SetupResponseMessage>> rpc(stub_->Asyncsetup(&context, request, &cq));
rpc->Finish(&reply, &status, (void *)1);
void *got_tag;
bool ok = false;
GPR_ASSERT(cq.Next(&got_tag, &ok));
GPR_ASSERT(got_tag == (void *)1);
GPR_ASSERT(ok);
if (status.ok())
{
return reply.setup_response();
}
else
{
return "Failed: Can't connect to server.";
}
}
std::string Client::Search(std::string &search_word)
{
SearchRequestMessage request;
unsigned char Wi[20];
word_string2unisignedchar(search_word, &Wi);
unsigned char Xi[24];
std::string const_ecb_key = "ecb_key";
std::string const_hash_key = "hash_key";
unsigned char ecb_key[24];
key_string2unisignedchar(const_ecb_key, &ecb_key);
unsigned char hash_key[24];
key_string2unisignedchar(const_hash_key, &hash_key);
des_ecb_encryption(ecb_key, Wi, &Xi);
unsigned char Li[8];
for (size_t i = 0; i < 8; i++)
{
Li[i] = Xi[i];
}
unsigned char Ki[16];
HMAC(hash_key, 24, Li, 8, &Ki);
for (auto &&i : Ki)
{
request.add_ki(i);
}
// std::cout << request.ki_size() << std::endl;
for (auto &&i : Xi)
{
request.add_xi(i);
}
// std::cout << request.xi_size() << std::endl;
SearchResponseMessage reply;
ClientContext context;
CompletionQueue cq;
Status status;
std::unique_ptr<ClientAsyncResponseReader<SearchResponseMessage>> rpc(stub_->Asyncsearch(&context, request, &cq));
rpc->Finish(&reply, &status, (void *)1);
void *got_tag;
bool ok = false;
GPR_ASSERT(cq.Next(&got_tag, &ok));
GPR_ASSERT(got_tag == (void *)1);
GPR_ASSERT(ok);
if (status.ok())
{
std::cout << reply.search_response() << std::endl;
if (reply.search_cf_size() == 0)
{
return "Search Successfully! But no hitted.";
}
else
{
std::vector<std::string> cf;
for (auto &&i : reply.search_cf())
{
cf.push_back(i);
}
std::vector<int> kv_num;
int kv_all = 0;
for (auto &&i : reply.search_kv_num())
{
kv_all += i;
kv_num.push_back(i);
}
std::vector<int> key;
for (auto &&i : reply.search_kv())
{
key.push_back(i.key());
}
std::vector<std::vector<unsigned char>> value;
std::vector<unsigned char> en_word;
// unsigned char en_word[24];
for (auto &&item : reply.search_kv())
{
for (size_t i = 0; i < item.value_size(); i++)
{
en_word.push_back(item.value()[i]);
}
value.push_back(en_word);
en_word.clear();
}
int num = 0;
int count = 0;
for (size_t i = 0; i < cf.size(); i++)
{
std::cout << "COLUMNFAMILY NAME: " << cf[i] << ":" << std::endl;
num = kv_num[i];
for (size_t j = 0; j < num; j++)
{
std::cout << key[j + count] << " : ";
for (size_t m = 0; m < 24; m++)
{
printf("%02x", value[j + count][m]);
}
std::cout << std::endl;
// jiemi
std::string const_ecb_key = "ecb_key";
std::string const_hash_key = "hash_key";
unsigned char word[20];
unsigned char encode[24];
std::cout << "Encode the value: ";
for (size_t a = 0; a < 24; a++)
{
encode[a] = value[j + count][a];
}
decode_swp(encode, const_ecb_key, const_hash_key, key[j + count], &word);
printf("%s\n", word);
}
count += num;
}
return "Search successfully!";
}
}
else
{
return "Failed: Can't connect to server.";
}
}
bool Client::Add_Data(unsigned int &add_key, unsigned char (*add_value)[24], std::string &columnfamily)
{
AddRequestMessage request;
request.set_add_key(add_key);
for (int i = 0; i < 24; i++)
{
request.add_add_value((*add_value)[i]);
}
request.set_add_columnfamily(columnfamily);
AddResponseMessage reply;
ClientContext context;
CompletionQueue cq;
Status status;
std::unique_ptr<ClientAsyncResponseReader<AddResponseMessage>> rpc(stub_->Asyncadd_data(&context, request, &cq));
rpc->Finish(&reply, &status, (void *)1);
void *got_tag;
bool ok = false;
GPR_ASSERT(cq.Next(&got_tag, &ok));
GPR_ASSERT(got_tag == (void *)1);
GPR_ASSERT(ok);
if (status.ok())
{
return reply.add_response();
}
else
{
// return "Failed: Can't connect to server.";
return false;
}
}
std::string Client::Delete_Data(const std::string &del_cf)
{
DeleteRequestMessage request;
request.set_delete_columnfamily(del_cf);
DeleteResponseMessage reply;
ClientContext context;
CompletionQueue cq;
Status status;
std::unique_ptr<ClientAsyncResponseReader<DeleteResponseMessage>> rpc(stub_->Asyncdelete_data(&context, request, &cq));
rpc->Finish(&reply, &status, (void *)1);
void *got_tag;
bool ok = false;
GPR_ASSERT(cq.Next(&got_tag, &ok));
GPR_ASSERT(got_tag == (void *)1);
GPR_ASSERT(ok);
if (status.ok())
{
return reply.delete_response();
}
else
{
return "Failed: Can't connect to server.";
}
}
std::string Client::Show_All()
{
ShowAllRequestMessage request;
request.set_showall_request("show");
ShowAllResponseMessage reply;
ClientContext context;
CompletionQueue cq;
Status status;
std::unique_ptr<ClientAsyncResponseReader<ShowAllResponseMessage>> rpc(stub_->Asyncshow_all(&context, request, &cq));
rpc->Finish(&reply, &status, (void *)1);
void *got_tag;
bool ok = false;
GPR_ASSERT(cq.Next(&got_tag, &ok));
GPR_ASSERT(got_tag == (void *)1);
GPR_ASSERT(ok);
if (status.ok())
{
std::vector<std::string> cf;
for (auto &&i : reply.showall_cf())
{
cf.push_back(i);
}
std::vector<int> kv_num;
int kv_all = 0;
for (auto &&i : reply.kv_num())
{
kv_all += i;
kv_num.push_back(i);
}
std::vector<int> key;
for (auto &&i : reply.showall_kv())
{
key.push_back(i.key());
}
std::vector<std::vector<unsigned char>> value;
std::vector<unsigned char> en_word;
// unsigned char en_word[24];
for (auto &&item : reply.showall_kv())
{
for (size_t i = 0; i < item.value_size(); i++)
{
en_word.push_back(item.value()[i]);
}
value.push_back(en_word);
en_word.clear();
}
bool flag = false;
flag = reply.showall_response();
if (!flag)
{
return "Failed: Can't fetch data.";
}
else if (kv_all == 0)
{
return "null";
}
else
{
int num = 0;
int count = 0;
for (size_t i = 0; i < cf.size(); i++)
{
std::cout << "COLUMNFAMILY NAME: " << cf[i] << ":" << std::endl;
num = kv_num[i];
if (num == 0)
{
std::cout << "null" << std::endl;
}
else
{
for (size_t j = 0; j < num; j++)
{
std::cout << " " << key[j + count] << " : ";
for (size_t m = 0; m < 24; m++)
{
printf("%02x", value[j + count][m]);
}
// jiemi
std::string const_ecb_key = "ecb_key";
std::string const_hash_key = "hash_key";
unsigned char word[20];
unsigned char encode[24];
std::cout << " ==> ";
for (size_t a = 0; a < 24; a++)
{
encode[a] = value[j + count][a];
}
decode_swp(encode, const_ecb_key, const_hash_key, key[j + count], &word);
printf("%s\n", word);
}
}
count += num;
}
}
return "Show all data successfully!";
}
else
{
return "Failed: Can't connect to server.";
}
}
std::string Client::Random_Gen_DB(int &num)
{
std::vector<std::vector<std::string>> value;
random_generate_DB(num, value);
RandomGenerateDBRequestMessage request;
for (size_t i = 0; i < num; i++)
{
request.add_cf_name("cf_" + std::to_string(i + 1));
}
std::string const_ecb_key = "ecb_key";
std::string const_hash_key = "hash_key";
for (auto &&onecf : value)
{
int key = 0; // 记录每个cf的键值对数量 同时作为键值 1-value1
for (auto &&word : onecf)
{
unsigned char word_uchar[20];
unsigned char encode_word[24];
kv *k_v;
k_v = request.add_gen_kv();
word_string2unisignedchar(word, &word_uchar);
encode_swp(word_uchar, const_ecb_key, const_hash_key, key + 1, &encode_word);
k_v->set_key(key + 1);
for (size_t j = 0; j < 24; j++)
{
k_v->add_value(encode_word[j]);
}
key++;
}
request.add_gen_kv_num(key);
}
RandomGenerateDBResponseMessage reply;
ClientContext context;
CompletionQueue cq;
Status status;
std::unique_ptr<ClientAsyncResponseReader<RandomGenerateDBResponseMessage>> rpc(stub_->AsyncRanGenDB(&context, request, &cq));
rpc->Finish(&reply, &status, (void *)1);
void *got_tag;
bool ok = false;
GPR_ASSERT(cq.Next(&got_tag, &ok));
GPR_ASSERT(got_tag == (void *)1);
GPR_ASSERT(ok);
if (status.ok())
{
return reply.gen_response();
}
else
{
return "Failed: Can't connect to server.";
}
}
std::string Client::Destroy()
{
DestroyRequestMessage request;
request.set_destroy_request("clear");
DestroyResponseMessage reply;
ClientContext context;
CompletionQueue cq;
Status status;
std::unique_ptr<ClientAsyncResponseReader<DestroyResponseMessage>> rpc(stub_->AsyncDestroyDB(&context, request, &cq));
rpc->Finish(&reply, &status, (void *)1);
void *got_tag;
bool ok = false;
GPR_ASSERT(cq.Next(&got_tag, &ok));
GPR_ASSERT(got_tag == (void *)1);
GPR_ASSERT(ok);
if (status.ok())
{
return reply.destroy_response();
}
else
{
return "Failed: Can't connect to server.";
}
}
void showhelp()
{
std::cout << "==================================================================" << std::endl;
std::cout << " setup # 建立一个数据库:当且仅当数据库不存 " << std::endl;
std::cout << " 在的情况下使用,该命令仅建立数据库 " << std::endl;
std::cout << " 并无数据存入 " << std::endl;
std::cout << std::endl;
std::cout << " rangen [n] # 随机生成n个具有不同内容的列族来初 " << std::endl;
std::cout << " 始化数据库:每个列族中的数据均加密 " << std::endl;
std::cout << " 并已经插入到数据库中 当且仅当数据 " << std::endl;
std::cout << " 库内无数据的情况下可以使用 " << std::endl;
std::cout << std::endl;
std::cout << " search [value] # 查询某个value值:输入明文 返回该 " << std::endl;
std::cout << " 值所在列族的所有数据 " << std::endl;
std::cout << std::endl;
std::cout << " add [key] [value] [cf] # 向某个列族中插入一个键值对:[key] " << std::endl;
std::cout << " 为正整数 [value]为明文字符 [cf] " << std::endl;
std::cout << " 可省略 若省略将插入到默认列族 " << std::endl;
std::cout << " 注意:默认列族不建议使用 " << std::endl;
std::cout << std::endl;
std::cout << " delete [cf] # 删除某个列族:不可删除默认列族 " << std::endl;
std::cout << std::endl;
std::cout << " show # 输出数据库内所有数据:包括列族名 " << std::endl;
std::cout << std::endl;
std::cout << " destroy # 清除数据库:彻底删除数据库中的数据 " << std::endl;
std::cout << " 及其本身 再次使用需先执行setup命令 " << std::endl;
std::cout << "==================================================================" << std::endl;
}
// 客户端操作指令处理 0--quit 1--setup 2--search 3--add 4--delete 5--showall 负值 error
int client_operate(std::string &op)
{
if (op == "quit")
{
std::cout << "Client Closed." << std::endl;
return 0;
}
else if (op == "setup")
{
std::cout << "Building... " << std::endl;
return 1;
}
else if (op == "search")
{
std::cout << "Searching..." << std::endl;
return 2;
}
else if (op == "add")
{
std::cout << "Adding..." << std::endl;
return 3;
}
else if (op == "delete")
{
std::cout << "Deleting..." << std::endl;
return 4;
}
else if (op == "show")
{
std::cout << "Getting..." << std::endl;
return 5;
}
else if (op == "rangen")
{
std::cout << "Generating..." << std::endl;
return 6;
}
else if (op == "destroy")
{
std::cout << "Destroying..." << std::endl;
return 7;
}
else if (op == "help")
{
showhelp();
return 8;
}
else
{
std::cout << "Input ERROR! ";
return -1;
}
}
void runclient()
{
std::cout << "Welcome! You can input 'help' for more information about instructions." << std::endl;
// showhelp();
Client client(grpc::CreateChannel("localhost:50051", grpc::InsecureChannelCredentials()));
std::string operation;
std::vector<std::string> operation_splited;
int operation_code = 0;
std::string reply;
std::vector<std::string> re;
while (std::cout << std::endl
<< "ValidUser > $ ",
getline(std::cin, operation))
{
split(operation, operation_splited, ' ');
operation_code = client_operate(operation_splited.front());
if (operation_code < 0)
{
std::cout << "Please input your operation again: " << std::endl;
}
else if (operation_code == 0)
{
break;
}
else if (operation_code == 1)
{
reply = client.Setup();
std::cout << reply << std::endl;
}
else if (operation_code == 2)
{
reply = client.Search(operation_splited[1]);
std::cout << reply << std::endl;
}
else if (operation_code == 3)
{
unsigned int add_key = 0;
unsigned char add_value[24];
std::string columnfamily;
if (operation_splited.size() < 4)
{
columnfamily = "default";
}
else
{
columnfamily = operation_splited[3];
}
add_key = stoi(operation_splited[1]);
std::string const_ecb_key = "ecb_key";
std::string const_hash_key = "hash_key";
unsigned char word[20];
word_string2unisignedchar(operation_splited[2], &word);
encode_swp(word, const_ecb_key, const_hash_key, add_key, &add_value);
bool flag = false;
flag = client.Add_Data(add_key, &add_value, columnfamily);
if (flag)
{
std::cout << "Add Successfully!" << std::endl;
}
else
{
std::cout << "Add Failed!" << std::endl;
}
}
else if (operation_code == 4)
{
reply = client.Delete_Data(operation_splited[1]);
std::cout << reply << std::endl;
}
else if (operation_code == 5)
{
reply = client.Show_All();
if (reply == "null")
{
std::cout << "DB exists, but no data!" << std::endl;
}
else
{
std::cout << reply << std::endl;
}
}
else if (operation_code == 6)
{
int num = stoi(operation_splited[1]);
reply = client.Random_Gen_DB(num);
std::cout << reply << std::endl;
}
else if (operation_code == 7)
{
reply = client.Destroy();
std::cout << reply << std::endl;
}
else
{
;
}
}
}
int main(int argc, char **argv)
{
runclient();
return 0;
}