Repository navigation
Expand file tree
/
Copy pathdes.c
More file actions
594 lines (534 loc) · 17.1 KB
/
Copy pathdes.c
File metadata and controls
594 lines (534 loc) · 17.1 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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
// des.c : Data Encryption Standard
// 2013.3.22 v1.2
#include "des.h"
//字节转换成二进制
int ByteToBit(ElemType ch, ElemType bit[8]){
int cnt;
for(cnt = 0;cnt < 8; cnt++){
*(bit+cnt) = (ch>>cnt)&1;
}
return 0;
}
//二进制转换成字节
int BitToByte(ElemType bit[8],ElemType *ch){
int cnt;
for(cnt = 0;cnt < 8; cnt++){
*ch |= *(bit + cnt)<<cnt;
}
return 0;
}
//将长度为8的字符串转为二进制位串
int Char8ToBit64(ElemType ch[8],ElemType bit[64]){
int cnt;
for(cnt = 0; cnt < 8; cnt++){
ByteToBit(*(ch+cnt),bit+(cnt<<3));
}
return 0;
}
//将二进制位串转为长度为8的字符串
int Bit64ToChar8(ElemType bit[64],ElemType ch[8]){
int cnt;
memset(ch,0,8);
for(cnt = 0; cnt < 8; cnt++){
BitToByte(bit+(cnt<<3),ch+cnt);
}
return 0;
}
//生成子密钥
int DES_MakeSubKeys(ElemType key[64],ElemType subKeys[16][48]){
ElemType temp[56];
int cnt;
DES_PC1_Transform(key,temp);//PC1置换
for(cnt = 0; cnt < ROUND; cnt++){//16轮跌代,产生16个子密钥
DES_ROL(temp,MOVE_TIMES[cnt]);//循环左移
DES_PC2_Transform(temp,subKeys[cnt]);//PC2置换,产生子密钥
}
return 0;
}
//密钥置换1
int DES_PC1_Transform(ElemType key[64], ElemType tempbts[56]){
int cnt;
for(cnt = 0; cnt < 56; cnt++){
tempbts[cnt] = key[PC_1[cnt]];
}
return 0;
}
//密钥置换2
int DES_PC2_Transform(ElemType key[56], ElemType tempbts[48]){
int cnt;
for(cnt = 0; cnt < 48; cnt++){
tempbts[cnt] = key[PC_2[cnt]];
}
return 0;
}
//循环左移
int DES_ROL(ElemType data[56], int time){
ElemType temp[56];
//保存将要循环移动到右边的位
memcpy(temp,data,time);
memcpy(temp+time,data+28,time);
//前28位移动
memcpy(data,data+time,28-time);
memcpy(data+28-time,temp,time);
//后28位移动
memcpy(data+28,data+28+time,28-time);
memcpy(data+56-time,temp+time,time);
return 0;
}
//IP置换
int DES_IP_Transform(ElemType data[64]){
int cnt;
ElemType temp[64];
for(cnt = 0; cnt < 64; cnt++){
temp[cnt] = data[IP_Table[cnt]];
}
memcpy(data,temp,64);
return 0;
}
//IP逆置换
int DES_IP_1_Transform(ElemType data[64]){
int cnt;
ElemType temp[64];
for(cnt = 0; cnt < 64; cnt++){
temp[cnt] = data[IP_1_Table[cnt]];
}
memcpy(data,temp,64);
return 0;
}
//扩展置换
int DES_E_Transform(ElemType data[48]){
int cnt;
ElemType temp[48];
for(cnt = 0; cnt < 48; cnt++){
temp[cnt] = data[E_Table[cnt]];
}
memcpy(data,temp,48);
return 0;
}
//P置换
int DES_P_Transform(ElemType data[32]){
int cnt;
ElemType temp[32];
for(cnt = 0; cnt < 32; cnt++){
temp[cnt] = data[P_Table[cnt]];
}
memcpy(data,temp,32);
return 0;
}
//异或
int DES_XOR(ElemType R[48], ElemType L[48] ,int count){
int cnt;
for(cnt = 0; cnt < count; cnt++){
R[cnt] ^= L[cnt];
}
return 0;
}
//S盒置换
int DES_SBOX(ElemType data[48]){
int cnt;
int line,row,output;
int cur1,cur2;
for(cnt = 0; cnt < 8; cnt++){
cur1 = cnt*6;
cur2 = cnt<<2;
//计算在S盒中的行与列
line = (data[cur1]<<1) + data[cur1+5];
row = (data[cur1+1]<<3) + (data[cur1+2]<<2)
+ (data[cur1+3]<<1) + data[cur1+4];
output = S[cnt][line][row];
//化为2进制
data[cur2] = (output&0X08)>>3;
data[cur2+1] = (output&0X04)>>2;
data[cur2+2] = (output&0X02)>>1;
data[cur2+3] = output&0x01;
}
return 0;
}
//交换
int DES_Swap(ElemType left[32], ElemType right[32]){
ElemType temp[32];
memcpy(temp,left,32);
memcpy(left,right,32);
memcpy(right,temp,32);
return 0;
}
//加密单个分组
int DES_EncryptBlock(ElemType plainBlock[8], ElemType subKeys[16][48], ElemType cipherBlock[8]){
ElemType plainBits[64];
ElemType copyRight[48];
int cnt;
Char8ToBit64(plainBlock,plainBits);
//初始置换(IP置换)
DES_IP_Transform(plainBits);
//16轮迭代
for(cnt = 0; cnt < ROUND; cnt++){
memcpy(copyRight,plainBits+32,32);
//将右半部分进行扩展置换,从32位扩展到48位
DES_E_Transform(copyRight);
//将右半部分与子密钥进行异或操作
DES_XOR(copyRight,subKeys[cnt],48);
//异或结果进入S盒,输出32位结果
DES_SBOX(copyRight);
//P置换
DES_P_Transform(copyRight);
//将明文左半部分与右半部分进行异或
DES_XOR(plainBits,copyRight,32);
if(cnt != ROUND - 1){
//最终完成左右部的交换
DES_Swap(plainBits,plainBits+32);
}
}
//逆初始置换(IP^1置换)
DES_IP_1_Transform(plainBits);
Bit64ToChar8(plainBits,cipherBlock);
return 0;
}
//解密单个分组
int DES_DecryptBlock(ElemType cipherBlock[8], ElemType subKeys[16][48],ElemType plainBlock[8]){
ElemType cipherBits[64];
ElemType copyRight[48];
int cnt;
Char8ToBit64(cipherBlock,cipherBits);
//初始置换(IP置换)
DES_IP_Transform(cipherBits);
//16轮迭代
for(cnt = ROUND - 1; cnt >= 0; cnt--){
memcpy(copyRight,cipherBits+32,32);
//将右半部分进行扩展置换,从32位扩展到48位
DES_E_Transform(copyRight);
//将右半部分与子密钥进行异或操作
DES_XOR(copyRight,subKeys[cnt],48);
//异或结果进入S盒,输出32位结果
DES_SBOX(copyRight);
//P置换
DES_P_Transform(copyRight);
//将明文左半部分与右半部分进行异或
DES_XOR(cipherBits,copyRight,32);
if(cnt != 0){
//最终完成左右部的交换
DES_Swap(cipherBits,cipherBits+32);
}
}
//逆初始置换(IP^1置换)
DES_IP_1_Transform(cipherBits);
Bit64ToChar8(cipherBits,plainBlock);
return 0;
}
/*加密数据(字符串)
*plainBuffer:输入数据
*keyBuffer:密钥(8字节)
*cipherBuffer:输出数据
*n:加密数据长度
*/
int DES_Encrypt(ElemType *plainBuffer, ElemType *keyBuffer, ElemType *cipherBuffer, int n){
int des_size = n;
int des_count = 0;
int count = 0;
ElemType plainBlock[8],cipherBlock[8],keyBlock[8];
ElemType bKey[64];
ElemType subKeys[16][48];
//设置密钥
memcpy(keyBlock,keyBuffer,8);
//将密钥转换为二进制流
Char8ToBit64(keyBlock,bKey);
//生成子密钥
DES_MakeSubKeys(bKey,subKeys);
while (des_count + 8 <= des_size){
memcpy(plainBlock,plainBuffer + des_count,8);
DES_EncryptBlock(plainBlock,subKeys,cipherBlock);
memcpy(cipherBuffer + des_count,cipherBlock,8) ;
des_count += 8;
}
if((count = des_size - des_count)){
memcpy(plainBlock,plainBuffer + des_count,count);
//填充
memset(plainBlock + count,'\0',7 - count);
//最后一个字符保存包括最后一个字符在内的所填充的字符数量
plainBlock[7] = 8 - count;
DES_EncryptBlock(plainBlock,subKeys,cipherBlock);
memcpy(cipherBuffer + des_count,cipherBlock,8);
}
return DES_OK;
}
/*解密数据(字符串)
*cipherBuffer:输入数据
*keyBuffer:密钥(8字节)
*plainBuffer:输出数据
*n:解密数据长度
*返回解密后数据长度
*/
int DES_Decrypt(ElemType *cipherBuffer, ElemType *keyBuffer, ElemType *plainBuffer, int n){
int des_size = n;
int des_count = 0;
int count = 0;
ElemType plainBlock[8],cipherBlock[8],keyBlock[8];
ElemType bKey[64];
ElemType subKeys[16][48];
//设置密钥
memcpy(keyBlock,keyBuffer,8);
//将密钥转换为二进制流
Char8ToBit64(keyBlock,bKey);
//生成子密钥
DES_MakeSubKeys(bKey,subKeys);
while(1){
memcpy(cipherBlock,cipherBuffer + des_count,8);
DES_DecryptBlock(cipherBlock,subKeys,plainBlock);
if(des_count + 8 < des_size){
memcpy(plainBuffer + des_count,plainBlock,8);
des_count += 8;
} else {
break;
}
}
//判断末尾是否被填充
if(plainBlock[7] < 8){
for(count = 8 - plainBlock[7]; count < 7; count++){
if(plainBlock[count] != '\0'){
break;
}
}
}
if(count == 7){
memcpy(plainBuffer + des_count,plainBlock,8 - plainBlock[7]);
return des_count +8 - plainBlock[7];
} else {
memcpy(plainBuffer + des_count,plainBlock,8);
return des_count +8 ;
}
return des_count + 8;
}
//加密文件
int DES_Encrypt_File(char *plainFile, char *keyStr,char *cipherFile){
FILE *plain,*cipher,*key;
ElemType *plainBlock,*cipherBlock,keyBlock[8];
long fileLen_in,fileLen_out;
int count;
if((plain = fopen(plainFile,"rb")) == NULL){
return PLAIN_FILE_OPEN_ERROR;
}
if((key = fopen(keyStr,"rb")) == NULL){
return KEY_FILE_OPEN_ERROR;
}
if((cipher = fopen(cipherFile,"wb")) == NULL){
return CIPHER_FILE_OPEN_ERROR;
}
if((count = fread(keyBlock,sizeof(char),8,key)) == 8){
}
fseek(plain,0,SEEK_END);//将文件指针置尾
fileLen_out = fileLen_in = ftell(plain);//取文件指针当前位置
rewind(plain);//将文件指针重指向文件头
if (fileLen_in % 8 != 0)
fileLen_out = (fileLen_in/8+1)*8;
plainBlock = (ElemType *)malloc(fileLen_out * sizeof(ElemType));
cipherBlock = (ElemType *)malloc(fileLen_out * sizeof(ElemType));
fread(plainBlock,sizeof(char),fileLen_in,plain);
DES_Encrypt(plainBlock,keyBlock,cipherBlock,fileLen_in);
fwrite(cipherBlock,sizeof(char),fileLen_out,cipher);
fclose(plain);
fclose(cipher);
fclose(key);
free(plainBlock);
free(cipherBlock);
return DES_OK;
}
//解密文件
int DES_Decrypt_File(char *cipherFile, char *keyStr,char *plainFile){
FILE *plain,*cipher,*key;
ElemType *plainBlock,*cipherBlock,keyBlock[8];
int count;
long fileLen_in,fileLen_out;
if((plain = fopen(plainFile,"wb")) == NULL){
return PLAIN_FILE_OPEN_ERROR;
}
if((key = fopen(keyStr,"rb")) == NULL){
return KEY_FILE_OPEN_ERROR;
}
if((cipher = fopen(cipherFile,"rb")) == NULL){
return CIPHER_FILE_OPEN_ERROR;
}
if((count = fread(keyBlock,sizeof(char),8,key)) == 8){
}
fseek(cipher,0,SEEK_END);//将文件指针置尾
fileLen_out = fileLen_in = ftell(cipher);//取文件指针当前位置
rewind(cipher);//将文件指针重指向文件头
plainBlock = (ElemType *)malloc(fileLen_in * sizeof(ElemType));
cipherBlock = (ElemType *)malloc(fileLen_in * sizeof(ElemType));
fread(cipherBlock,sizeof(char),fileLen_in,cipher);
count = DES_Decrypt(cipherBlock,keyBlock,plainBlock,fileLen_in);
fwrite(plainBlock,sizeof(char),count,plain);
fclose(plain);
fclose(cipher);
fclose(key);
free(plainBlock);
free(cipherBlock);
return DES_OK;
}
/*3DES加密数据(字符串)
*对区块使用3个64位密钥进行3次加密,相当于增加了密钥长度
*plainBuffer:输入数据
*keyBuffer:密钥(24字节)
*cipherBuffer:输出数据
*n:加密数据长度
*/
int D3DES_Encrypt(ElemType *plainBuffer, ElemType *keyBuffer, ElemType *cipherBuffer, int n){
int des_size = n;
int des_count = 0;
int count = 0;
int i;
ElemType plainBlock[8],cipherBlock[8],keyBlock[24];
ElemType bKey[3][64];
ElemType subKeys[3][16][48];
for (i = 0; i < 3 ; i++)
{
//设置密钥
memcpy(keyBlock+i*8,keyBuffer+i*8,8);
//将密钥转换为二进制流
Char8ToBit64(keyBlock+i*8,bKey[i]);
//生成子密钥
DES_MakeSubKeys(bKey[i],subKeys[i]);
}
while (des_count + 8 <= des_size){
memcpy(plainBlock,plainBuffer + des_count,8);
for (i = 0; i < 3 ; i++)
{
DES_EncryptBlock(plainBlock,subKeys[i],cipherBlock);
}
memcpy(cipherBuffer + des_count,cipherBlock,8) ;
des_count += 8;
}
if((count = des_size - des_count)){
memcpy(plainBlock,plainBuffer + des_count,count);
//填充
memset(plainBlock + count,'\0',7 - count);
//最后一个字符保存包括最后一个字符在内的所填充的字符数量
plainBlock[7] = 8 - count;
{
for (i = 0; i < 3 ; i++)
{
DES_EncryptBlock(plainBlock,subKeys[i],cipherBlock);
}
}
memcpy(cipherBuffer + des_count,cipherBlock,8);
}
return DES_OK;
}
/*3DES解密数据(字符串)
*cipherBuffer:输入数据
*keyBuffer:密钥(24字节)
*plainBuffer:输出数据
*n:解密数据长度
*返回解密后数据长度
*/
int D3DES_Decrypt(ElemType *cipherBuffer, ElemType *keyBuffer, ElemType *plainBuffer, int n){
int des_size = n;
int des_count = 0;
int count = 0;
int i;
ElemType plainBlock[8],cipherBlock[8],keyBlock[24];
ElemType bKey[3][64];
ElemType subKeys[3][16][48];
for (i = 0; i < 3 ; i++)
{
//设置密钥
memcpy(keyBlock+i*8,keyBuffer+i*8,8);
//将密钥转换为二进制流
Char8ToBit64(keyBlock+i*8,bKey[i]);
//生成子密钥
DES_MakeSubKeys(bKey[i],subKeys[i]);
}
while(1){
memcpy(cipherBlock,cipherBuffer + des_count,8);
for (i = 0; i < 3 ; i++)
{
DES_DecryptBlock(cipherBlock,subKeys[i],plainBlock);
}
if(des_count + 8 < des_size){
memcpy(plainBuffer + des_count,plainBlock,8);
des_count += 8;
} else {
break;
}
}
//判断末尾是否被填充
if(plainBlock[7] < 8){
for(count = 8 - plainBlock[7]; count < 7; count++){
if(plainBlock[count] != '\0'){
break;
}
}
}
if(count == 7){
memcpy(plainBuffer + des_count,plainBlock,8 - plainBlock[7]);
return des_count +8 - plainBlock[7];
} else {
memcpy(plainBuffer + des_count,plainBlock,8);
return des_count +8 ;
}
return des_count + 8;
}
//3DES加密文件
int D3DES_Encrypt_File(char *plainFile, char *keyStr,char *cipherFile){
FILE *plain,*cipher,*key;
ElemType *plainBlock,*cipherBlock,keyBlock[24];
long fileLen_in,fileLen_out;
int count;
if((plain = fopen(plainFile,"rb")) == NULL){
return PLAIN_FILE_OPEN_ERROR;
}
if((key = fopen(keyStr,"rb")) == NULL){
return KEY_FILE_OPEN_ERROR;
}
if((cipher = fopen(cipherFile,"wb")) == NULL){
return CIPHER_FILE_OPEN_ERROR;
}
if((count = fread(keyBlock,sizeof(char),24,key)) == 24){
}
fseek(plain,0,SEEK_END);//将文件指针置尾
fileLen_out = fileLen_in = ftell(plain);//取文件指针当前位置
rewind(plain);//将文件指针重指向文件头
if (fileLen_in % 8 != 0)
fileLen_out = (fileLen_in/8+1)*8;
plainBlock = (ElemType *)malloc(fileLen_out * sizeof(ElemType));
cipherBlock = (ElemType *)malloc(fileLen_out * sizeof(ElemType));
fread(plainBlock,sizeof(char),fileLen_in,plain);
D3DES_Encrypt(plainBlock,keyBlock,cipherBlock,fileLen_in);
fwrite(cipherBlock,sizeof(char),fileLen_out,cipher);
fclose(plain);
fclose(cipher);
fclose(key);
free(plainBlock);
free(cipherBlock);
return DES_OK;
}
//3DES解密文件
int D3DES_Decrypt_File(char *cipherFile, char *keyStr,char *plainFile){
FILE *plain,*cipher,*key;
ElemType *plainBlock,*cipherBlock,keyBlock[24];
int count;
long fileLen_in,fileLen_out;
if((plain = fopen(plainFile,"wb")) == NULL){
return PLAIN_FILE_OPEN_ERROR;
}
if((key = fopen(keyStr,"rb")) == NULL){
return KEY_FILE_OPEN_ERROR;
}
if((cipher = fopen(cipherFile,"rb")) == NULL){
return CIPHER_FILE_OPEN_ERROR;
}
if((count = fread(keyBlock,sizeof(char),24,key)) == 24){
}
fseek(cipher,0,SEEK_END);//将文件指针置尾
fileLen_out = fileLen_in = ftell(cipher);//取文件指针当前位置
rewind(cipher);//将文件指针重指向文件头
plainBlock = (ElemType *)malloc(fileLen_in * sizeof(ElemType));
cipherBlock = (ElemType *)malloc(fileLen_in * sizeof(ElemType));
fread(cipherBlock,sizeof(char),fileLen_in,cipher);
count = D3DES_Decrypt(cipherBlock,keyBlock,plainBlock,fileLen_in);
fwrite(plainBlock,sizeof(char),count,plain);
fclose(plain);
fclose(cipher);
fclose(key);
free(plainBlock);
free(cipherBlock);
return DES_OK;
}