-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDy_EncCore.cpp
135 lines (124 loc) · 3.9 KB
/
Dy_EncCore.cpp
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
//CopyRight 2021 by DUYU. 杜宇保留所有权利。
//All Rights Reserved.
//2021-08-29.
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<cstdio>
#include<io.h>
void copy(char* src, char* dst, int key, int headt, int mdw);
int main(int argc,char *argv[])
{
using namespace std;
system("echo Copyright 2021 by DUYU.");
system("echo ***********************");
if (argc<5)
{
system("echo Arguement Error. 5 Arguements Needed.");
system("echo Arguement1: Source File.");
system("echo Arguement2: Destination File.");
system("echo Arguement3: KEY.");
system("echo Arguement4: Length of File Head.");
system("echo Arguement5: Running Mode,0=Encrypt,1=Unencrypt.");
system("echo For Example:");
system("echo Dy_EncCore.exe D:\\input.txt D:\\output.txt 9 100 0");
exit(1);
}
int head2 = atoi(argv[4]);
int x = atoi(argv[3]);
copy(argv[1], argv[2], x, head2, atoi(argv[5])); //argv[5]表明模式,0 = 加密,1 = 解密。
return 0;
}
void copy(char* src, char* dst, int key, int headt, int mdw)
{
if (mdw == 0)
{
using namespace std;
ifstream in(src,ios::binary);
ofstream out(dst,ios::binary);
if (!in.is_open())
{
cout << "Error Open File. " << src << endl;
exit(1);
}
if (!out.is_open())
{
cout << "Error Open File. " << dst << endl;
exit(1);
}
if (src == dst)
{
cout << "Source File Can't Be Same With Destination File." << endl;
exit(1);
}
char buf[2048];
long long totalBytes = headt;
out.clear();
out.seekp(headt,ios::beg);
while(in)
{
//read从in流中读取2048字节,放入buf数组中,同时文件指针向后移动2048字节
//若不足2048字节遇到文件结尾,则以实际提取字节读取。
in.read(buf, 2048);
//gcount()用来提取读取的字节数,write将buf中的内容写入out流。
char buft[2048];
for(int a=0;a<=2048;a=a+1)
{
buft[a]=(char)((int)buf[a]^(int)key);
}
out.write(buft, in.gcount());
totalBytes += in.gcount();
}
in.close();
out.close();
system("echo finish >> \"%tmp%\\finish.dyenc\"");
system("echo Encrypted Successfully.");
cout << "Total Bytes: " << totalBytes << " B" <<endl;
exit(0);
}
else
{
using namespace std;
ifstream in(src,ios::binary);
ofstream out(dst,ios::binary);
if (!in.is_open())
{
cout << "Error Open File. " << src << endl;
exit(1);
}
if (!out.is_open())
{
cout << "Error Open File. " << dst << endl;
exit(1);
}
if (src == dst)
{
cout << "Source File Can't Be Same With Destination File." << endl;
exit(1);
}
char buf[2048];
long long totalBytes = headt;
in.clear();
in.seekg(headt,ios::beg);
while(in)
{
//read从in流中读取2048字节,放入buf数组中,同时文件指针向后移动2048字节
//若不足2048字节遇到文件结尾,则以实际提取字节读取。
in.read(buf, 2048);
//gcount()用来提取读取的字节数,write将buf中的内容写入out流。
char buft[2048];
for(int a=0;a<=2048;a=a+1)
{
buft[a]=(char)((int)buf[a]^(int)key);
}
out.write(buft, in.gcount());
totalBytes += in.gcount();
}
in.close();
out.close();
system("echo finish >> \"%tmp%\\finish.dyenc\"");
system("echo Encrypted Successfully.");
cout << "Total Bytes: " << totalBytes << " B" <<endl;
exit(0);
}
}