-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAES128.c
177 lines (164 loc) · 5.21 KB
/
AES128.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
//This is a quick and simple AES Encryption implementation using C Programming Language
//The below code takes in a Base64 encoded string(message) and and Base64 encoded Key from the
//user and encrypts it according to AES algorithms and standards.
//As of now only 128 - bit level encryption is supported.
/*
WARNING : THIS IMPLEMENTATION MUST NOT BE USED TO ENCRYPT AND DECRYPT TEXT,FILES OR ANYTHING
AS IT IS VULNERABLE TO CACHE ATTACKS AND MANY OTHER ATTACKS AND HE OR SHE WHO IS
ATTACKING CAN READ THE BYTES SUBSTITUTED FROM S_BOX BY TIMING THEIR ATTACK WHILE
THE ENCRYPTION IS TAKING PLACE.
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include"AES.h"
#include"AES_Encrypt.h"
#include"AES_Decrypt.h"
int system(const char* command);
void readMessage(unsigned char * message){
printf("Enter the message to be encrypted in Base64 : ");
unsigned char ch;
int index = 0;
while((ch=getchar())!='\n'){
message[index] = ch;
index++;
}
}
void readKey(unsigned char * key){
unsigned char c1;
int index2=0;
fflush(stdin);
printf("Enter the key in Base64 : ");
while((c1=getchar())!='\n' && index2<16){
key[index2] = c1;
index2++;
}
}
void readMessageHex(unsigned char *message){
int index=0;
printf("\nEnter the message you want to decrypt : \nNote: Your message must be in Hexadecimal Format (eg. ae 7a 9b...) and must be separated by spaces.\n");
printf("Message (Hex) : ");
fflush(stdin);
for(int i=0;i<index+16;i++){
scanf("%02x",(unsigned int*)&message[i]);
if(i==(index+16-1))
index+=16;
if(getchar()=='\n')
break;
}
}
void readHexKey(unsigned char *key){
fflush(stdin);
printf("\nEnter the key in Hexadecimal : \nNote: Your message must be in Hexadecimal Format (eg. ae 7a 9b...) and must be separated by spaces.\n");
printf("Key (Hex) : ");
for(int i=0;i<16;i++){
scanf("%02x",(unsigned int*)&key[i]);
if(getchar()=='\n')
break;
}
}
void readKeyType(unsigned char* key){
printf("Choose any of the following : \n");
printf("1. Enter Key in Base64.\n2. Enter Key in Hexadecimal.\n");
char ch = getchar();
if(ch=='1'){
fflush(stdin);
readKey(key);
}
else if(ch=='2'){
fflush(stdin);
readHexKey(key);
}
else{
printf("\nPlease enter a valid choice.\n\n");
fflush(stdin);
readKeyType(key);
}
}
char readChoice(unsigned char* message,unsigned char *key){
printf("\nEnter any one of the following :-\n");
printf("1. Encrypt Text\n2. Decrypt Text\n3. Quit\n");
char c = getchar();
if(c=='1'){
fflush(stdin);
readMessage(message);
printf("\n");
readKeyType(key);
return '1';
}
else if(c=='2'){
fflush(stdin);
readMessageHex(message);
printf("\n");
fflush(stdin);
readKeyType(key);
return '2';
}
else if(c=='3')
exit(0);
else{
printf("\nPlease enter a valid entry.\n\n");
fflush(stdin);
readChoice(message,key);
return '\0';
}
}
int main(void){
unsigned char key[16];
unsigned char message[1024];
fflush(stdin);
system("clear");
printf("\nADVANCED ENCRYPTION STANDARDS - 128 Bit\n");
fflush(stdin);
char choice = readChoice(message,key);
/*for(int i=0;i<64;i++)
printf("%02x ",message[i]);
printf("\n");*/
int originalLen = strlen((const char*)message);
int lenOfPaddedMessage = originalLen;
clock_t start_t,end_t;
if(lenOfPaddedMessage % 16!=0){
lenOfPaddedMessage = (lenOfPaddedMessage/16 + 1)*16;
}
unsigned char paddedMessage[lenOfPaddedMessage];
int k=0;
for(int i=0;i<lenOfPaddedMessage;i++){
if(i>=originalLen)
paddedMessage[i] = 0;
else
paddedMessage[i] = message[i];
k=i;
}
start_t = clock();
unsigned char expandedKey[176];
KeyExpansion(key,expandedKey);
if(choice == '1'){
for(int i=0;i<lenOfPaddedMessage;i+=16)
AES_Encrypt(paddedMessage+i,expandedKey);
end_t = clock();
double total_t = (double)(end_t - start_t)/CLOCKS_PER_SEC;
printf("\nEncrypted Message : \n");
for(int i=0;i<lenOfPaddedMessage;i++)
printf("%02x ",paddedMessage[i]);
printf("\n\n");
printf("Length of Message : %d\n",originalLen);
printf("Length of Encrypted Message : %d\n",lenOfPaddedMessage);
printf("Time taken to encrypt : %lf seconds\n",total_t);
}
else
{
for(int i=0;i<lenOfPaddedMessage;i+=16)
AES_Decrypt(paddedMessage+i,expandedKey);
end_t = clock();
double total_t = (double)(end_t - start_t)/CLOCKS_PER_SEC;
printf("\nDecrypted Message : \n");
for(int i=0;i<lenOfPaddedMessage;i++)
printf("%c",paddedMessage[i]);
printf("\n\n");
printf("Length of Message : %d\n",originalLen);
printf("Length of Decrypted Message : %lu\n",strlen((const char*)paddedMessage));
printf("Time taken to decrypt : %lf seconds\n",total_t);
}
return 0;
}