-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.cpp
More file actions
102 lines (90 loc) · 2.44 KB
/
Copy pathtest.cpp
File metadata and controls
102 lines (90 loc) · 2.44 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
#include "SimpleCrytProtocol.h"
#include "def.h"
using namespace std;
namespace fs = std::filesystem;
void encryptTest(vector<fs::path> &paths, const string &outputName, Encrypter cp)
{
cp.encryptFiles(paths, outputName);
}
void decryptTest(fs::path &filePath, Encrypter cp)
{
auto dlls = cp.decryptFile(filePath);
for (auto &dll : dlls)
{
istream is(dll.pStBuf.get());
ofstream ofs;
auto oPath =filePath.parent_path().string() + "\\" +dll.fileName;
ofs.open(oPath + ".decrypted", ios_base::binary);
char buf[BUFFER_SIZE];
while (true)
{
is.read(buf, BUFFER_SIZE);
if (!is.eof())
ofs.write(buf, BUFFER_SIZE);
else
{
ofs.write(buf, is.gcount());
break;
}
}
}
}
bool compareTwoFile(const fs::path &p1, const fs::path &p2)
{
auto fileSize1 = (uint64_t)fs::file_size(p1);
auto fileSize2 = (uint64_t)fs::file_size(p2);
if (fileSize1 != fileSize2)
return false;
ifstream ifs1;
ifstream ifs2;
ifs1.open(p1, ios::binary);
ifs2.open(p2, ios::binary);
char c1;
char c2;
int loc = 0;
while (true)
{
ifs1.read(&c1, sizeof(char));
if(ifs1.eof())
return true;
ifs2.read(&c2, sizeof(char));
++loc;
if (c1 != c2)
{
ifs1.close();
ifs2.close();
cout << p1.filename() << "&" << p2.filename() << ":" << loc << endl;
return false;
}
}
ifs1.close();
ifs2.close();
return true;
}
bool cryptProtocolTest(CryptProtocol* prot)
{
vector<fs::path> fileNames;
fs::path f1 = "C:\\Users\\Administrator\\source\\repos\\test2\\Release\\test2.dll";
fileNames.push_back(f1);
auto outputName = ".\\output\\test2.dll.encrypted";
fs::path outputPath = ".\\output\\test2.dll.encrypted";
Encrypter cp(prot);
encryptTest(fileNames, outputName, cp);
decryptTest(outputPath, cp);
for(auto& path:fileNames)
{
auto p = outputPath.parent_path().string() +"\\"+ path.filename().string();
if(!compareTwoFile(path, p + ".decrypted"))
return false;
}
return true;
}
int main()
{
SimpleCryptProtocol scp;
// CryptProtocol scp;
if(cryptProtocolTest(&scp))
cout << "SimpleCryptProtocol passed" << endl;
else
cout << "SimpleCryptProtocol failed" << endl;
}