-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.c
124 lines (95 loc) · 3.12 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
#include <stdio.h>
#include "read.c"
#include "aes.c"
int main(){
// last extra byte is for '\0' character
unsigned char plainText[BYTES+1]; // plaintext
unsigned char cipherText[BYTES+1]; // ciphertext
unsigned char key[BYTES+1]; // encryption key
unsigned char output[BYTES+1]; // string to prints the results
int answer; // user answer
unsigned int state4x4[BLOCK_SIZE][BLOCK_SIZE]; // input
unsigned int key4x4[BLOCK_SIZE][BLOCK_SIZE]; // encryption or decryption key
// we have 10 round, so we need 40 words in array plus 4 for the given key
// each word have 4 bytes, so we need 44 * 4 = 176
unsigned int w[176]; // all round keys
// print the menu to user and read the answer
printf("-----------------------\n1. Encryption\n2. Decryption\n3. Encrypt and Decrypt\n4. Run example\n-----------------------\n");
do{
printf("Choose action: ");
scanf("%d", &answer);
}while(answer < 1 || answer > 4);
switch(answer){
case 1:
// read the plaintext
printf("Text: ");
readInput(plainText);
// read the key
printf("Key: ");
readInput(key);
// convert plaintext and key to 4x4 blocks
convertStringToBlock(plainText, state4x4);
convertStringToBlock(key, key4x4);
// we calculate all round keys 1-10
key_schedule(w, key4x4);
// encrypt the plaintext
encryption(state4x4, w);
// print the output to user as string
convertBlockToString(state4x4, output);
output[BYTES] = '\0';
printf("Ciphertext: %s", output);
break;
case 2:
// read the ciphertext
printf("Text: ");
readInput(cipherText);
// read the key
printf("Key: ");
readInput(key);
// convert ciphertext and key to 4x4 blocks
convertStringToBlock(cipherText, state4x4);
convertStringToBlock(key, key4x4);
// we calculate all round keys 1-10
key_schedule(w, key4x4);
// decrypt the ciphertext
decryption(state4x4, w);
// print the output to user as string
convertBlockToString(state4x4, output);
output[BYTES] = '\0';
printf("Plaintext: %s", output);
break;
case 3:
// read the plaintext
printf("Text: ");
readInput(plainText);
// read the key
printf("Key: ");
readInput(key);
// convert plaintext and key to 4x4 blocks
convertStringToBlock(plainText, state4x4);
convertStringToBlock(key, key4x4);
// we calculate all round keys 1-10
key_schedule(w, key4x4);
printf("\nENCRYPTION\n");
// encrypt the plaintext
encryption(state4x4, w);
// print the output to user as string
convertBlockToString(state4x4, output);
output[BYTES] = '\0';
printf("Ciphertext: %s\n", output);
printf("END OF ENCRYPTION\n");
printf("\n\nDECRYPTION\n");
// decrypt the ciphertext
decryption(state4x4, w);
// print the output to user as string
convertBlockToString(state4x4, output);
output[BYTES] = '\0';
printf("Plaintext: %s\n", output);
printf("END OF DECRYPTION\n");
break;
case 4:
test();
break;
}
return 0;
}