-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
375 lines (362 loc) · 6.34 KB
/
main.c
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#include <string.h>
#include "AES.c"
#include "SHA1.h"
unsigned char data[4][4]={{0},{0}},k[4][4]={{0},{0}},cy[4][4]={{0},{0}};
FILE *fp,*fpk,*fpo,*fpt;
unsigned char start[4][4]={{0},{0}};//tic[8]={0};
void firstsec();
void dataset0();
int main(int argc, char *argv[]) {
DLLIMPORT void SHA1(FILE *fp,char bf[]);
HINSTANCE zhi=LoadLibrary("SHA1.dll");
typedef void (*usedll)(FILE *fp, char bf[]);
usedll USE =(usedll)GetProcAddress(zhi,"SHA1");
bool en=true;
int i,i1,i2,shi,yu=0;
int fcon=0;
unsigned long long size,sizek=0,iz=0;
if(argc!=4 || strcmp(argv[1],"?")==0)
{
//printf("%d",argc);
printf(" \nUsage: AES.exe [filepath about to Encrypt or Decrypt] [keyfilepath] [de/en]\n \"en\" for Encrypt and \"de\" for Decrypt.\n");
printf(" 用法:程序 要加密或解密的文件 密钥文件 de或者en “en”表示加密,“de”表示解密\n");
exit(2);
}
if(strcmp(argv[3],"en")==0)
{
en=true;
}
else
{
if(strcmp(argv[3],"de")==0)
{
en=false;
}
else
{
printf(" please type \"en\" or \"de\"");
exit(2);
}
}
//en=false;
/*if((fpt=fopen("SHA1.dll","rb"))==NULL)
{
printf("SHA1.dll file missing\nSHA1.dll文件缺失\n");
exit(2);
}
fclose(fpt);*/
if((fp=fopen(argv[1],"rb"))==NULL)
{
printf(" Can not open input file\n");
exit(2);
}
char path[256]={0};
int end;
strcpy(path,argv[1]);
if(en) strcat(path,".aesfile");
else
{
end=strlen(path);
for(i=0;i<=8;i++)
{
path[end-i]=0;
}
}
if((fpo=(fopen(path,"wb")))==NULL)
{
printf("Can not creat output file\n");
fclose(fp);
exit(2);
}
if((fpk=(fopen(argv[2],"rb")))==NULL)
{
printf("Can not open key file\n");
fclose(fp);
fclose(fpo);
exit(2);
}
else
{
fseek(fpk,0,2);
sizek=ftell(fpk);
rewind(fpk);
if(sizek!=16)
{
printf("key file error\n");
fclose(fp);
fclose(fpo);
fclose(fpk);
exit(2);
}
}
fseek(fp,0,2);
size=ftell(fp);
//printf("size:%d\n",size);
rewind(fp);
//printf("t1:%d\n",ftell(fp));
yu=(size+16)%16;
yu=16-yu;
//printf("yu:%d\n",yu);
fread(k,16,1,fpk);
char bf[45];
int con=0;
if(en)//0x530x6e0x6f0x770x640x610x730x68
{
printf(" Start encrypt\n");
firstsec();
rewind(fp);
USE(fp,bf);
rewind(fp);
for(iz=0;iz<(size/16)+1+3;iz++)
{
dataset0();
if(iz<(size/16))
{
fread(data,16,1,fp);
}
else
{
if(iz==(size/16))
{
//if(yu!=16)
{
int readcon=0;
for(i1=0;i1<4;i1++)
{
for(i2=0;i2<4;i2++)
{
if(readcon>=(16-yu))
{
data[i1][i2]=yu;
}
else
{
//rewind(fp);
//printf("t:%d\n",ftell(fp));
fread(&data[i1][i2],1,1,fp);
//printf("%d %d %d\n",i1,i2,data[i1][i2]);
}
readcon++;
}
}
}
//else
{
;
}
//for(i=0;i<(16-yu);i++)
}
else
for(i=0;i<4;i++)
{
for(i1=0;i1<4;i1++)
{
if(con>=40)
{
data[i][i1]=0;
}
else
{
data[i][i1]=bf[con];
}
con++;
}
}
}
for(i=0;i<4;i++)
{
for(i1=0;i1<4;i1++)
{
data[i][i1]=data[i][i1] ^ start[i][i1];
}
}
AES(data,k,cy);
fwrite(cy,16,1,fpo);
fcon++;
if(fcon==1000)
{
fflush(fpo);
fcon=0;
}
for(i=0;i<4;i++)
{
for(i1=0;i1<4;i1++)
{
start[i][i1]=cy[i][i1];
}
}
}
//fwrite(bf,40,1,fpo);
printf(" Encrypt Complete\n");
fclose(fpk);
fclose(fpo);
fclose(fp);
}
if(!en)
{
con=0;
char yan[9]="Snowdash";
char bf[45]={0};
char bf1[45]={0};
if(size%16!=0)
{
printf("Input file error\n");
fclose(fpk);
fclose(fpo);
fclose(fp);
remove(path);
exit(2);
}
fread(cy,16,1,fp);
AESde(cy,k,data);
for(i=0;i<2;i++)
{
for(i1=0;i1<4;i1++)
{
if(data[i][i1]!=yan[con])
{
printf(" Input file error or key error\n");
fclose(fpk);
fclose(fpo);
fclose(fp);
exit(2);
}
con++;
}
}
for(i=0;i<4;i++)
{
for(i1=0;i1<4;i1++)
{
start[i][i1]=data[i][i1];
}
}
con=0;
int yucon=0;
for(iz=0;iz<((size-16)/16);iz++)
{
fread(cy,16,1,fp);
AESde(cy,k,data);
for(i=0;i<4;i++)
{
for(i1=0;i1<4;i1++)
{
data[i][i1]=data[i][i1] ^ start[i][i1];
}
}
for(i=0;i<4;i++)
{
for(i1=0;i1<4;i1++)
{
start[i][i1]=cy[i][i1];
}
}
if(iz>=(((size-16)/16)-3))
{
for(i=0;i<4;i++)
{
for(i1=0;i1<4;i1++)
{
bf[con]=data[i][i1];
con++;
}
}
}
else
{
if(iz==(((size-16)/16)-4))
{
yucon=0;
yu=data[3][3];
yu=16-yu;
//printf("de:yu:%d\n",yu);
for(i=0;i<4;i++)
{
for(i1=0;i1<4;i1++)
{
if(yucon>=yu) break;
fwrite(&data[i][i1],1,1,fpo);
yucon++;
}
if(yucon>=yu) break;
}
}
else
fwrite(data,16,1,fpo);
fcon++;
if(fcon==1000)
{
fflush(fpo);
fcon=0;
}
}
}
fclose(fpo);
fpo=fopen(path,"rb");
USE(fpo,bf1);
if(strcmp(bf,bf1)!=0)
{
printf(" File has been changed\n");
/*printf("bf :%s\n",bf);
printf("bf1:%s",bf1);*/
}
else
{
printf(" Decrypt complete\n");
}
}
//
//USE(fpk,bf);
fclose(fpk);
fclose(fpo);
fclose(fp);
return 0;
}
void firstsec()
{
unsigned long long ti=0;
//unsigned long long dash=7526466486394646099;
unsigned char dashc[8]={0x53,0x6e,0x6f,0x77,0x64,0x61,0x73,0x68};
int i,i1;
ti=time(NULL);
//ltoa(ti,tic,2);
//fwrite(&start,sizeof(start),1,fpo);
//fwrite(&dash,8,1,fpo);
//fwrite(&ti,sizeof(ti),1,fpo);
int con=0;
for(i=0;i<4;i++)
{
for(i1=0;i1<4;i1++)
{
if(con>=8)
{
start[i][i1]=( (ti >> ((con-8)*8)) & 0xff);
}
else
{
start[i][i1]=dashc[con];
}
con++;
}
}
AES(start,k,cy);
fwrite(cy,16,1,fpo);
}
void dataset0()
{
int i,i1;
for(i=0;i<4;i++)
{
for(i1=0;i1<4;i1++)
{
data[i][i1]=0;
}
}
}
/*void cyset0()
{
}*/