-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
178 lines (139 loc) · 6.84 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
#include "run/include/enc_dec.h"
#include "run/include/algo.h"
#include "run/include/utils.h"
#include "main.h" // For HexWord, HexByte, and any custom types
#include "modes/include/enc/cfb_enc.h" // For OfbEnc function
#include "modes/include/dec/cfb_dec.h" // For OfbDec function (if decryption is also needed)
#include "modes/include/enc/ofb_enc.h" // For OfbEnc function
#include "modes/include/dec/ofb_dec.h" // For OfbDec function (if decryption is also needed)
#include "modes/include/enc/cbc_enc.h" // For CbcEnc function
#include "modes/include/dec/cbc_dec.h" // For CbcDec function (if decryption is also needed)
#include "modes/include/enc/ecb_enc.h" // For EcbEnc function
#include "modes/include/dec/ecb_dec.h" // For EcbDec function (if decryption is also needed)
int main() {
HexWord IV[4], input_2[4];
FILE *iFile, *oFile;
char fName_enc[100], fName_dec[100];
int choice, i_File = 0, ch_File;
clock_t enc_t, dec_t;
double time_taken;
// char *key = "d4bf5d30e0b452aeb84111f11e2798e5"; // Example KEY
// char *key = "3243f6a8885a308d313198a2e0370734"; // Example KEY
// char *key = "2b7e151628aed2a6abf7158809cf4f3c"; // Example KEY
// string "YELLOW SUBMARINE"
char *key = "59454c4c4f57205355424d4152494e45"; // Example KEY
HexWord rowKeyArray[4]; // Array to store all Initial Keys
HexWord keyScheduling[44]; // Array to store all Round Keys
rowParseHexWords(key, rowKeyArray, 32); // Parse the hex string and store it into Array
keyExpansion(rowKeyArray, keyScheduling, 4); // Run the Key Scheduling Algorithm and store it inside the
// KeyScheduling DONE !
// The Keys being stored in Array keyScheduling[]
unsigned char temp_iv[16]; // Declare a 16-byte array for the IV
if (RAND_priv_bytes(temp_iv, sizeof(temp_iv)) != 1){
printf("Error Generating Random IV\n");
return 1;
}
// Now Store that IV in HexWord Format
IV[0].bytes[0].nibbles.low = temp_iv[0];
IV[0].bytes[0].nibbles.high = temp_iv[0] >> 4;
IV[0].bytes[1].nibbles.low = temp_iv[1];
IV[0].bytes[1].nibbles.high = temp_iv[1] >> 4;
IV[0].bytes[2].nibbles.low = temp_iv[2];
IV[0].bytes[2].nibbles.high = temp_iv[2] >> 4;
IV[0].bytes[3].nibbles.low = temp_iv[3];
IV[0].bytes[3].nibbles.high = temp_iv[3] >> 4;
IV[1].bytes[0].nibbles.low = temp_iv[4];
IV[1].bytes[0].nibbles.high = temp_iv[4] >> 4;
IV[1].bytes[1].nibbles.low = temp_iv[5];
IV[1].bytes[1].nibbles.high = temp_iv[5] >> 4;
IV[1].bytes[2].nibbles.low = temp_iv[6];
IV[1].bytes[2].nibbles.high = temp_iv[6] >> 4;
IV[1].bytes[3].nibbles.low = temp_iv[7];
IV[1].bytes[3].nibbles.high = temp_iv[7] >> 4;
IV[2].bytes[0].nibbles.low = temp_iv[8];
IV[2].bytes[0].nibbles.high = temp_iv[8] >> 4;
IV[2].bytes[1].nibbles.low = temp_iv[9];
IV[2].bytes[1].nibbles.high = temp_iv[9] >> 4;
IV[2].bytes[2].nibbles.low = temp_iv[10];
IV[2].bytes[2].nibbles.high = temp_iv[10] >> 4;
IV[2].bytes[3].nibbles.low = temp_iv[11];
IV[2].bytes[3].nibbles.high = temp_iv[11] >> 4;
IV[3].bytes[0].nibbles.low = temp_iv[12];
IV[3].bytes[0].nibbles.high = temp_iv[12] >> 4;
IV[3].bytes[1].nibbles.low = temp_iv[13];
IV[3].bytes[1].nibbles.high = temp_iv[13] >> 4;
IV[3].bytes[2].nibbles.low = temp_iv[14];
IV[3].bytes[2].nibbles.high = temp_iv[14] >> 4;
IV[3].bytes[3].nibbles.low = temp_iv[15];
IV[3].bytes[3].nibbles.high = temp_iv[15] >> 4;
printf(":: AES-128 ::\n");
printf ("\nEnter the Mode of Operation for Encryption :: \n1) Electronic Code Book (ECB)\n2) Cipher Block Chaining (CBC)\n3) Cipher Feedback Mode (CFB)\n4) Output Feedback Mode (OFB)\n5) PROCEED TO DECRYPT\n\nPlease enter your choice : ");
scanf ("%d", &choice);
if ( choice != 5 ){
input_2[0] = IV[0];
input_2[1] = IV[1];
input_2[2] = IV[2];
input_2[3] = IV[3];
printf("Enter the file you want to encrypt : ");
// To take the file name as Input (Without using any additional header files like <string.h>)
while ((ch_File = getc(stdin)) != '\n' && ch_File != EOF && i_File < (int)sizeof(fName_enc) - 1){
fName_enc[i_File++] = ch_File;}
fName_enc[i_File] = '\0'; // Null terminating the String
iFile = fopen(fName_enc, "rb");
oFile = fopen("encrypt.bin", "wb");
if (iFile == NULL || oFile == NULL) {
printf("File Couldn't be opened !!");
return 1; }
enc_t = clock();
switch (choice) {
case 1: EcbEnc(keyScheduling, iFile, oFile);
break;
case 2: CbcEnc(input_2, keyScheduling, iFile, oFile);
break;
case 3: OfbEnc(input_2, keyScheduling, iFile, oFile);
break;
case 4: CfbEnc(input_2, keyScheduling, iFile, oFile);
break;
default: printf("WRONG OPTION !\nQuitting !");
return 0;
}
enc_t = clock() - enc_t;
time_taken = ((double) enc_t)/CLOCKS_PER_SEC; // in seconds
printf("Encryption Time : %f Seconds\n", time_taken);
}
printf ("\nEnter the Mode of Operation for Decryption :: \n1) Electronic Code Book (ECB)\n2) Cipher Block Chaining (CBC)\n3) Cipher Feedback Mode (CFB)\n4) Output Feedback Mode (OFB)\n5) PROCEED TO EXIT ..\n\nPlease enter your choice : ");
scanf ("%d", &choice);
if ( choice != 5 ){
input_2[0] = IV[0];
input_2[1] = IV[1];
input_2[2] = IV[2];
input_2[3] = IV[3];
printf("Enter the file you want to decrypt : ");
// To take the file name as Input (Without using any additional header files like <string.h>)
while ((ch_File = getc(stdin)) != '\n' && ch_File != EOF && i_File < (int)sizeof(fName_dec) - 1){
fName_dec[i_File++] = ch_File;}
fName_dec[i_File] = '\0'; // Null terminating the String
iFile = fopen(fName_dec, "rb");
oFile = fopen("decrypt.bin", "wb");
if (iFile == NULL || oFile == NULL) {
printf("File Couldn't be opened !!");
return 1; }
dec_t = clock();
switch (choice) {
case 1: EcbDec(keyScheduling, iFile, oFile);
break;
case 2: CbcDec(input_2, keyScheduling, iFile, oFile);
break;
case 3: OfbDec(input_2, keyScheduling, iFile, oFile);
break;
case 4: CfbDec(input_2, keyScheduling, iFile, oFile);
break;
default: printf("WRONG OPTION !\nQuitting !");
return 0;
}
dec_t = clock() - dec_t;
time_taken = ((double) dec_t)/CLOCKS_PER_SEC; // in seconds
printf("Decryption Time : %f Seconds\n", time_taken);
}
return 0;
}