This repository was archived by the owner on Dec 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcommon_ecb.h
More file actions
85 lines (67 loc) · 1.77 KB
/
common_ecb.h
File metadata and controls
85 lines (67 loc) · 1.77 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
//
// Created by Ghost on 2018/11/13.
//
#ifndef CRYPTO_COMMON_ECB_H
#define CRYPTO_COMMON_ECB_H
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#ifdef DES_ECB
#include "des_ecb.h"
#endif
#ifdef AES_ECB
#include "aes_ecb.h"
#endif
#ifdef RC4_T
#include "rc4.h"
#endif
#define UEOF 255
void file_encrypt(crypto_data info, FILE *in_file, FILE *key_file, FILE *out_file) {
unsigned char buffer[BUFFER_SIZE];
unsigned char key[KEY_SIZE];
// read key
int c = 0;
for (; (c < KEY_SIZE) && (feof(key_file) == 0); c++) {
key[c] = (unsigned char) fgetc(key_file);
}
fclose(key_file);
for (; c < KEY_SIZE; ++c) {
key[c] = 0;
}
int count = 0;
while (feof(in_file) == 0) {
buffer[count] = (unsigned char) fgetc(in_file);
if (buffer[count] == UEOF) {
buffer[count] = 0;
break;
}
count++;
if (count == BUFFER_SIZE) {
count = 0;
unsigned char result[BUFFER_SIZE];
encrypt(buffer, key, info.mode, result);
// output
for (int i = 0; i < BUFFER_SIZE; ++i) {
if (result[i] == UEOF || result[i] == 0) break;
fputc((int) result[i], out_file);
}
// clear buffer
for (int j = 0; j < BUFFER_SIZE; ++j) {
buffer[j] = 0;
}
}
}
fclose(in_file);
if (count) {
unsigned char result[BUFFER_SIZE];
encrypt(buffer, key, info.mode, result);
// output
for (int i = 0; i < BUFFER_SIZE; ++i) {
if (result[i] == UEOF || result[i] == 0) break;
fputc((int) result[i], out_file);
}
}
fclose(out_file);
}
#endif //CRYPTO_COMMON_ECB_H