-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.c
379 lines (340 loc) · 11.6 KB
/
function.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
376
377
378
379
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include <minix/mthread.h>
#include <openssl/aes.h>
#include <stdbool.h>
#define BLOCK_SIZE 16
#define BUFFER_SIZE 320000
struct ThreadData
{
struct Buffer *input_buffer;
struct Buffer *output_buffer;
AES_KEY *key;
bool last_block;
};
struct Buffer
{
unsigned char data[BUFFER_SIZE];
size_t size;
};
// The key is adjusted to 16 bytes, the insufficient part is filled with 0, and the excess part is truncated
void adjust_key(const unsigned char *user_key, unsigned char *adjusted_key)
{
int len = strlen((char *)user_key);
if (len < 16)
{
memcpy(adjusted_key, user_key, len);
memset(adjusted_key + len, 0, 16 - len);
}
else
{
memcpy(adjusted_key, user_key, 16);
}
}
void write_log(const char *message, const unsigned char *data, size_t size)
{
FILE *fp;
fp = fopen("log.txt", "a"); // Open the file in append mode
if (fp == NULL)
{
printf("Error opening file!\n");
return;
}
// get time
time_t rawtime;
struct tm *timeinfo;
char timestamp[20];
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", timeinfo);
fprintf(fp, "[%s] %s: ", timestamp, message);
for (size_t i = 0; i < size; ++i)
{
fprintf(fp, "%02x", data[i]);
}
fprintf(fp, "\n");
fclose(fp);
}
void *encrypt_thread(void *thread_arg)
{
struct ThreadData *data = (struct ThreadData *)thread_arg;
unsigned char p[BLOCK_SIZE];
unsigned char e[BLOCK_SIZE];
memset(p, 0, BLOCK_SIZE);
memset(e, 0, BLOCK_SIZE);
size_t effset = 0; // Where to start reading or writing each time
size_t bytes_to_read = 0;
size_t size_copy = data->input_buffer->size;
while (1)
{
// Check if there is data available in input buffer
if (size_copy == 0)
{
// If no data available, exit thread
break;
}
// Read data from input buffer
bytes_to_read = size_copy < BLOCK_SIZE ? size_copy : BLOCK_SIZE;
memcpy(p, data->input_buffer->data + effset, bytes_to_read);
AES_encrypt(p, e, data->key);
memcpy(data->output_buffer->data + effset, e, BLOCK_SIZE);
size_copy -= bytes_to_read;
effset += bytes_to_read;
// Encrypt data
// printf("wf\n");
// print_buffer(p);
// write_log("encrypt_thread:p", p, BLOCK_SIZE);
// write_log("encrypt_thread:e", e, BLOCK_SIZE);
// print_buffer(e);
// Write encrypted data to output buffer
data->output_buffer->size += BLOCK_SIZE;
memset(p, 0, BLOCK_SIZE);
memset(e, 0, BLOCK_SIZE);
// print_buffer(data->output_buffer->data);
// write_log("encrypt_thread:output_buffer", data->output_buffer->data, data->output_buffer->size);
// printf("out_buff_size%d\n", data->output_buffer->size);
}
if (data->last_block)
{
// Encrypt remaining bytes count and append to output buffer
printf("last_block!!!!!!!!!!!\n");
if (bytes_to_read == 0)
{
bytes_to_read = BLOCK_SIZE;
}
sprintf(p, "%d", bytes_to_read);
AES_encrypt(p, e, data->key);
write_log("encrypt_thread last block:p", p, BLOCK_SIZE);
write_log("encrypt_thread last block:e", e, BLOCK_SIZE);
// print_buffer(p);
// print_buffer(e);
size_copy = data->output_buffer->size;
// Write encrypted bytes count to output buffer
memcpy(data->output_buffer->data + size_copy, e, BLOCK_SIZE);
size_copy += BLOCK_SIZE;
data->output_buffer->size += BLOCK_SIZE;
// print_buffer(data->output_buffer->data);
write_log("encrypt_thread last block:output_buffer", data->output_buffer->data, data->output_buffer->size);
printf("out_buff_size%d\n", data->output_buffer->size);
}
return NULL;
}
void *decrypt_thread(void *thread_arg)
{
struct ThreadData *data = (struct ThreadData *)thread_arg;
unsigned char p[BLOCK_SIZE], d[BLOCK_SIZE];
memset(p, 0, BLOCK_SIZE);
memset(d, 0, BLOCK_SIZE);
size_t effset = 0; // Where to start reading or writing each time
size_t bytes_to_read = 0;
size_t size_copy = data->input_buffer->size;
int last_block_size = 0;
if (data->last_block)
{
// Decrypt the size of the last block
bytes_to_read = BLOCK_SIZE;
memcpy(p, data->input_buffer->data + (size_copy - BLOCK_SIZE), BLOCK_SIZE);
// print_buffer(p);
write_log("decrypt_thread:p", p, BLOCK_SIZE);
AES_decrypt(p, d, data->key);
// print_buffer(d);
write_log("decrypt_thread:d", d, BLOCK_SIZE);
printf("size_copy%d\n", size_copy);
last_block_size = atoi(d);
// size_copy -= BLOCK_SIZE;
printf("Decrypted data: %s\n", d);
memset(p, 0, BLOCK_SIZE);
memset(d, 0, BLOCK_SIZE);
printf("last block!!!!!!!!!!\n");
}
while (size_copy > 0)
{
// Reads data from the input buffer
// print_buffer(data->input_buffer->data);
// printf("Size copy: %d\n", size_copy);
bytes_to_read = (size_copy < BLOCK_SIZE) ? size_copy : BLOCK_SIZE;
memcpy(p, data->input_buffer->data + effset, bytes_to_read);
// Decrypt the data
// printf("Decrypting data\n");
// print_buffer(p);
// write_log("decrypt_thread:p", p, BLOCK_SIZE);
AES_decrypt(p, d, data->key);
// print_buffer(d);
// write_log("decrypt_thread:d", d, BLOCK_SIZE);
// Write the decrypted data to the output buffer
if (data->last_block && size_copy == 0)
{
memcpy(data->output_buffer->data + effset, d, last_block_size);
data->output_buffer->size += last_block_size;
}
else
{
memcpy(data->output_buffer->data + effset, d, BLOCK_SIZE);
data->output_buffer->size += BLOCK_SIZE;
}
size_copy -= bytes_to_read;
effset += bytes_to_read;
memset(p, 0, BLOCK_SIZE);
memset(d, 0, BLOCK_SIZE);
}
return NULL;
}
void encrypt_decrypt_string(const unsigned char *key)
{
const unsigned char plaintext[] = "hello world!";
unsigned char ciphertext[16];
AES_KEY aes_key;
AES_set_encrypt_key(key, 128, &aes_key);
AES_encrypt(plaintext, ciphertext, &aes_key);
// Print the encrypted result (as a hexadecimal string)
printf("Encrypted: ");
for (int i = 0; i < AES_BLOCK_SIZE; ++i)
{
printf("%02x", ciphertext[i]);
}
printf("\n");
AES_set_decrypt_key(key, 128, &aes_key);
AES_decrypt(ciphertext, plaintext, &aes_key);
printf("Decrypted: %s\n", plaintext);
}
int main(int argc, char **argv)
{
if (argc != 5)
{
fprintf(stderr, "Parameter input error\n");
exit(1);
}
// Prints each command line argument
for (int i = 0; i < argc; ++i)
{
printf("Argument %d: %s\n", i, argv[i]);
}
unsigned char user_key[17];
unsigned char adjusted_key[17];
strcpy((char *)user_key, argv[4]);
// Change the key length to 16 bytes
adjust_key(user_key, adjusted_key);
encrypt_decrypt_string(adjusted_key);
FILE *fp_input = fopen(argv[2], "rb");
if (NULL == fp_input)
{
fprintf(stderr, "open %s fail: %s\n", argv[3], strerror(errno));
exit(1);
}
FILE *fp_output = fopen(argv[3], "wb");
if (NULL == fp_output)
{
fprintf(stderr, "open %s fail: %s\n", argv[4], strerror(errno));
fclose(fp_output);
exit(1);
}
// Count the thread count
fseek(fp_input, 0, SEEK_END);
size_t file_size = ftell(fp_input);
int num_threads = file_size / BUFFER_SIZE;
if (file_size % BUFFER_SIZE != 0)
{
num_threads++;
}
printf("\nNumber of threads: %d\n", num_threads);
mthread_thread_t threads[num_threads];
struct ThreadData thread_data[num_threads];
// Initializes the thread data structure
for (int i = 0; i < num_threads; ++i)
{
thread_data[i] = *(struct ThreadData *)malloc(sizeof(struct ThreadData));
thread_data[i].input_buffer = (struct Buffer *)malloc(sizeof(struct Buffer));
thread_data[i].output_buffer = (struct Buffer *)malloc(sizeof(struct Buffer));
}
// Reset the file pointer
fseek(fp_input, 0, SEEK_SET);
if (strcmp(argv[1], "-e") == 0)
{
// Create an encrypted thread
printf("\nEncrypting...\n");
for (int i = 0; i < num_threads; ++i)
{
thread_data[i].input_buffer->size = fread(thread_data[i].input_buffer->data, 1, BUFFER_SIZE, fp_input);
if (thread_data[i].input_buffer->size == 0)
{
printf("\nNo data to read\n");
break;
}
thread_data[i].output_buffer->size = 0;
thread_data[i].last_block = (i == num_threads - 1); // Set last_block to true for the last thread
AES_KEY key;
thread_data[i].key = &key;
AES_set_encrypt_key(adjusted_key, 128, thread_data[i].key);
mthread_create(&threads[i], NULL, encrypt_thread, &thread_data[i]);
// printf("\nThread %d created\n", i);
}
}
else if (strcmp(argv[1], "-d") == 0)
{
// Create a decryption thread
printf("\nDecrypting...\n");
for (int i = 0; i < num_threads; ++i)
{
thread_data[i].input_buffer->size = fread(thread_data[i].input_buffer->data, 1, BUFFER_SIZE, fp_input);
if (thread_data[i].input_buffer->size == 0)
{
break;
}
AES_KEY key;
thread_data[i].key = &key;
AES_set_decrypt_key(adjusted_key, 128, thread_data[i].key);
thread_data[i].output_buffer->size = 0;
thread_data[i].last_block = (i == num_threads - 1); // Set last_block to true for the last thread
// printf("last_block:%d\n", thread_data[i].last_block);
mthread_create(&threads[i], NULL, decrypt_thread, &thread_data[i]);
// printf("\nThread %d created\n", i);
}
}
else
{
fprintf(stderr, "Parameter input error\n");
fclose(fp_input);
fclose(fp_output);
exit(1);
}
// Wait for all threads to finish
for (int i = 0; i < num_threads; ++i)
{
mthread_join(threads[i], NULL);
}
// writes the data from the output buffer to the output file
for (int i = 0; i < num_threads; ++i)
{
// printf("\nWriting to output file...\n");
// write_log("output_file", thread_data[i].output_buffer->data, thread_data[i].output_buffer->size);
// print_buffer(thread_data[i].output_buffer->data);
fwrite(thread_data[i].output_buffer->data, 1, thread_data[i].output_buffer->size, fp_output);
}
// Close the file
fclose(fp_input);
fclose(fp_output);
// Free memory
for (int i = 0; i < num_threads; ++i)
{
if (thread_data[i].input_buffer != NULL)
{
free(thread_data[i].input_buffer);
thread_data[i].input_buffer = NULL; // Set pointer to NULL to avoid dangling pointer
}
if (thread_data[i].output_buffer != NULL)
{
free(thread_data[i].output_buffer);
thread_data[i].output_buffer = NULL; // Set pointer to NULL to avoid dangling pointer
}
}
// // Free the array of thread_data structures
// if (thread_data != NULL)
// {
// free(thread_data);
// thread_data = NULL; // Set pointer to NULL to avoid dangling pointer
// }
return 0;
}