-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
141 lines (116 loc) · 4.13 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/evp.h>
#include <openssl/aes.h>
#define AES_KEY_SIZE 16 // 128-bit key for AES-128
#define AES_BLOCK_SIZE 16 // AES block size
void encryptAES(const unsigned char *key, const unsigned char *plaintext, unsigned char *ciphertext, int size) {
EVP_CIPHER_CTX *ctx;
int len;
int ciphertext_len;
ctx = EVP_CIPHER_CTX_new();
EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, NULL);
EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, size);
ciphertext_len = len;
EVP_EncryptFinal_ex(ctx, ciphertext + len, &len);
ciphertext_len += len;
EVP_CIPHER_CTX_free(ctx);
}
void decryptAES(const unsigned char *key, const unsigned char *ciphertext, unsigned char *plaintext, int size) {
EVP_CIPHER_CTX *ctx;
int len;
int plaintext_len;
ctx = EVP_CIPHER_CTX_new();
EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, NULL);
EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, size);
plaintext_len = len;
EVP_DecryptFinal_ex(ctx, plaintext + len, &len);
plaintext_len += len;
EVP_CIPHER_CTX_free(ctx);
}
void getFileSize(FILE *inputFile, long *fileSize) {
fseek(inputFile, 0, SEEK_END);
*fileSize = ftell(inputFile);
rewind(inputFile);
}
int main(int argc, char *argv[]) {
FILE *inputFile;
FILE *outputFile;
char *buffer = NULL;
unsigned char *ciphertext = NULL;
unsigned char *decryptedText = NULL;
long fileSize;
if (argc != 5) {
printf("Usage: %s <encrypt|decrypt> <input file> <output file> <key>\n", argv[0]);
return 1;
}
char *mode = argv[1];
char *inputFileName = argv[2];
char *outputFileName = argv[3];
unsigned char key[AES_KEY_SIZE];
strncpy((char *)key, argv[4], AES_KEY_SIZE); // Copy key from argv[4] to key
if (strlen(argv[4]) != AES_KEY_SIZE) {
printf("Error: Key must be %d bytes long.\n", AES_KEY_SIZE);
return 1;
}
inputFile = fopen(inputFileName, "rb");
if (inputFile == NULL) {
printf("Error: Could not open input file.\n");
return 1;
}
getFileSize(inputFile, &fileSize);
buffer = (char *)malloc(fileSize * sizeof(char));
if (buffer == NULL) {
printf("Error: Memory allocation failed for buffer.\n");
fclose(inputFile);
return 1;
}
fread(buffer, sizeof(char), fileSize, inputFile);
fclose(inputFile);
if (strcmp(mode, "encrypt") == 0) {
ciphertext = (unsigned char *)malloc(fileSize + AES_BLOCK_SIZE); // Allocate extra space for padding
if (ciphertext == NULL) {
printf("Error: Memory allocation failed for ciphertext.\n");
free(buffer);
return 1;
}
encryptAES(key, (unsigned char *)buffer, ciphertext, fileSize);
outputFile = fopen(outputFileName, "wb");
if (outputFile == NULL) {
printf("Error: Could not open output file for encryption.\n");
free(buffer);
free(ciphertext);
return 1;
}
fwrite(ciphertext, sizeof(char), fileSize + AES_BLOCK_SIZE, outputFile);
fclose(outputFile);
printf("File encrypted successfully.\n");
free(ciphertext);
} else if (strcmp(mode, "decrypt") == 0) {
decryptedText = (unsigned char *)malloc(fileSize);
if (decryptedText == NULL) {
printf("Error: Memory allocation failed for decrypted text.\n");
free(buffer);
return 1;
}
decryptAES(key, (unsigned char *)buffer, decryptedText, fileSize);
outputFile = fopen(outputFileName, "wb");
if (outputFile == NULL) {
printf("Error: Could not open output file for decryption.\n");
free(buffer);
free(decryptedText);
return 1;
}
fwrite(decryptedText, sizeof(char), fileSize, outputFile);
fclose(outputFile);
printf("File decrypted successfully.\n");
free(decryptedText);
} else {
printf("Error: Invalid mode. Use 'encrypt' or 'decrypt'.\n");
free(buffer);
return 1;
}
free(buffer);
return 0;
}