-
Notifications
You must be signed in to change notification settings - Fork 0
/
aes.c
41 lines (28 loc) · 1.13 KB
/
aes.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
void aes_ecb_encrypt(unsigned char * plain_text, unsigned char* key, int text_length) {
for (int i = 0; i < text_length; i += 16) {
AES_encrypt(plain_text + i, key);
}
}
void aes_ecb_decrypt(unsigned char * plain_text, unsigned char* key, int text_length) {
for (int i = 0; i < text_length; i += 16) {
AES_decrypt(plain_text + i, key);
}
}
void aes_cbc_encrypt(unsigned char * plain_text, unsigned char* key, int text_length, unsigned char * iv) {
unsigned char * xor_vector = iv;
for (int i = 0; i < text_length; i += 16) {
AddRoundKey(plain_text+i, xor_vector);
AES_encrypt(plain_text+i, key);
xor_vector = plain_text + i;
}
}
void aes_cbc_decrypt(unsigned char * plain_text, unsigned char* key, int text_length, unsigned char * iv) {
unsigned char * xor_vector = iv;
unsigned char place_holder[AES_SIZE];
for (int i = 0; i < text_length; i += 16) {
memcpy(place_holder, plain_text+i, AES_SIZE);
AES_decrypt(plain_text + i, key);
AddRoundKey(plain_text + i, xor_vector);
memcpy(xor_vector, place_holder, AES_SIZE);
}
}