Repository navigation
Expand file tree
/
Copy pathcrypt_file.cpp
More file actions
145 lines (114 loc) · 3.46 KB
/
Copy pathcrypt_file.cpp
File metadata and controls
145 lines (114 loc) · 3.46 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
#include <iostream>
#include <memory>
#include <memory_resource>
#include "xcrypt.h"
#include "xread_task.h"
#include "xcrypt_task.h"
#include "xwrite_task.h"
#include "xfile_crypt.h"
#include <filesystem>
#include<list>
using namespace std;
using namespace filesystem;
using namespace std;
int main(int argc, char* argv[])
{
// 加密文件指令: crypt_file 输入文件夹 输出文件夹 密码
// 解密文件指令: crypt_file 输入文件夹 输出文件夹 密码 -d
if (argc < 4)
{
cerr << "para error" << endl;
return -1;
}
string in_file = argv[1]; //输入文件夹
string out_file = argv[2];//输出文件夹
string password = argv[3];//密码
bool is_encrypt = true;
if (argc > 4)is_encrypt = false;
//创建输出文件夹
create_directories(out_file);
//if (is_encrypt)
{
list<shared_ptr<XFileCrypt>> fes;
for (auto& p : directory_iterator(in_file)) //遍历输入目录
{
if (!p.is_regular_file())continue;//只处理文件
auto fe = make_shared<XFileCrypt>();
fe->Start(p.path().string(), //输入文件
out_file + "\\" + p.path().filename().string(), //输出文件
password, is_encrypt
);
fes.push_back(fe);
}
for (auto fe : fes)
{
fe->Wait();
}
}
cout << "目录下文件批量加密" << endl;
std::string passwd = "12345678";
#include "xfile_crypt.h"
{
auto xfc = make_shared<XFileCrypt>();
xfc->Start("test.txt", "test_end.txt", passwd, true);
xfc->Wait();
auto xfd = make_shared<XFileCrypt>();
xfd->Start("test_end.txt", "test_dec.txt", passwd, false);
xfd->Wait();
}
{
//创建一个内存池
shared_ptr<pmr::memory_resource> mp(new pmr::synchronized_pool_resource());
auto rt = make_shared<XReadTask>();
rt->Init("./img/test.png");
rt->set_mem_pool(mp);
auto ct = make_shared<XCryptTask>();
ct->set_mem_pool(mp);
ct->set_is_encrypt(true);
ct->Init(passwd);
rt->set_next(ct);
auto wt = make_shared<XWriteTask>();
wt->set_mem_pool(mp);
wt->Init("test_out.png");
ct->set_next(wt);
rt->Start();
ct->Start();
wt->Start();
rt->Wait();
ct->Wait();
wt->Wait();
}
//////////////////////////////////
//开始解密
{
shared_ptr<pmr::memory_resource> mp(new pmr::synchronized_pool_resource());
auto rt = make_shared<XReadTask>();
rt->Init("test_out.png");
rt->set_mem_pool(mp);
auto ct = make_shared<XCryptTask>();
ct->set_mem_pool(mp);
ct->Init(passwd);
ct->set_is_encrypt(false);
rt->set_next(ct);
auto wt = make_shared<XWriteTask>();
wt->set_mem_pool(mp);
wt->Init("test_dec.png");
ct->set_next(wt);
rt->Start();
ct->Start();
wt->Start();
rt->Wait();
ct->Wait();
wt->Wait();
}
XCrypt crypt;
crypt.Init("12345678");
char out[1024] = { 0 };
int en_size = crypt.Encrypt("abcdefgh", 8, out, true);
cout << "en_size = " << en_size << endl;
cout << out << endl;
char de_out[1024] = { 0 };
int de_size = crypt.Decrypt(out, en_size, de_out, true);
cout << de_size << ":de_out=" << de_out << endl;
return 0;
}