-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimpleCrytProtocol.cpp
More file actions
124 lines (107 loc) · 2.96 KB
/
Copy pathSimpleCrytProtocol.cpp
File metadata and controls
124 lines (107 loc) · 2.96 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
#include "SimpleCrytProtocol.h"
void SimpleCryptProtocol::reverse(char *buf, int start, int end)
{
while (start < end)
{
char tmp = buf[start];
buf[start] = buf[end];
buf[end] = tmp;
++start;
--end;
}
}
void SimpleCryptProtocol::rotate(char *buf, int bufSize, int steps)
{
reverse(buf, 0, steps - 1);
reverse(buf, steps, bufSize - 1);
reverse(buf, 0, bufSize - 1);
}
void SimpleCryptProtocol::encrypt(const PlainFile &file, ofstream &ofs)
{
cout << "\tencrypting file:\t" << file.fileName << endl;
//每个文件每次加密都随机种子
srand(std::time(nullptr));
magicNum = rand();
//write magicNum
if (magicNum == 0)
magicNum = 0x77;
ofs.write(&magicNum, sizeof(magicNum));
istream is(file.pStBuf.get());
//write filename
auto fileName = file.fileName;
char tmp = 0 ^ magicNum;
for (auto &c : fileName)
c ^= magicNum;
ofs.write(fileName.c_str(), fileName.length() * sizeof(char));
ofs.write(&tmp, sizeof(char)); // end of filename
//write filesize
auto fileSize = file.fileSize;
ofs.write((char *)(&fileSize), sizeof(fileSize));
//write encrypted content
char *buf = new char[fenceSize];
while (true)
{
is.read(buf, fenceSize);
if (!is.eof())
{
//栅栏加密
rotate(buf, fenceSize, movesteps);
// (a XOR b) XOR b = a
for (int i = 0; i < fenceSize; ++i)
buf[i] ^= magicNum;
ofs.write(buf, fenceSize);
}
else
{
ofs.write(buf, is.gcount());
break;
}
}
delete buf;
cout << "\tfile encrypted"<< endl;
}
PlainFile SimpleCryptProtocol::decrypt(istream &is)
{
is.read(&magicNum, sizeof(magicNum));
ostream os(nullptr);
char fileName[100];
char *cur = fileName;
while (true)
{
os.rdbuf(new stringbuf);
is.read(cur, sizeof(char));
*cur ^= magicNum;
*(cur + 1) = 0;
if (*cur == 0)
break;
if (is.eof())
{
cur[0] = 0;
return PlainFile(fileName, 0, nullptr);
}
++cur;
}
uint64_t fileSize;
is.read((char *)&fileSize, sizeof(fileSize));
auto remainSize = fileSize;
char *buf = new char[fenceSize];
while (remainSize >= (unsigned int)fenceSize)
{
is.read(buf, fenceSize);
remainSize -= fenceSize;
//解密
// (a XOR b) XOR b = a
for (int i = 0; i < fenceSize; ++i)
buf[i] ^= magicNum;
rotate(buf, fenceSize, fenceSize - movesteps);
os.write(buf, fenceSize);
}
is.read(buf, remainSize);
os.write(buf, remainSize);
auto ps = os.rdbuf();
os.rdbuf(nullptr);
cout << "file decrypted" << endl
<< "\tfile name:\t" << fileName << endl
<< "\tfile size:\t" << fileSize<< " bytes" << endl;
return PlainFile(fileName, fileSize, ps);
}