-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
117 lines (94 loc) · 2.42 KB
/
test.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "aes.h"
int aes_ecb_encrypt(uint8* ptrinput, uint8 *ptroutput, uint32 len)
{
uint32 i = 0;
uint32 count = len / 16;
aes_context ctx;
uint8 *ptrplain = NULL;
uint8 *ptrencrypt = NULL;
uint8 key[16]={0x01, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
if (ptrinput == NULL || ptroutput == NULL)
{
return -1;
}
/* 长度必须是16的倍数 */
if (len < 16 || len % 16 != 0)
{
return -1;
}
aes_set_key(&ctx, key, 128);
ptrplain = ptrinput;
ptrencrypt = ptroutput;
for (i = 0; i < count; i++)
{
aes_encrypt(&ctx, ptrplain, ptrencrypt);
ptrplain += 16;
ptrencrypt += 16;
}
return 0;
}
int aes_ecb_decrypt(uint8* ptrinput, uint8 *ptroutput, uint32 len)
{
uint32 i = 0;
uint32 count = len / 16;
aes_context ctx;
uint8 *ptrplain = NULL;
uint8 *ptrdecrypt = NULL;
uint8 key[16]={0x01, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
if (ptrinput == NULL || ptroutput == NULL)
{
return -1;
}
/* 长度必须是16的倍数 */
if (len < 16 || len % 16 != 0)
{
return -1;
}
aes_set_key(&ctx, key, 128);
ptrplain = ptrinput;
ptrdecrypt = ptroutput;
for (i = 0; i < count; i++)
{
aes_decrypt(&ctx, ptrplain, ptrdecrypt);
ptrplain += 16;
ptrdecrypt += 16;
}
return 0;
}
int main()
{
char plain_text[32]="This is a test string.";
//char plain_text[32]="0123456789abcdef";
char encrypt_text[128]={0};
char decrypt_text[128]={0};
/* 长度必须是16的倍数 */
aes_ecb_encrypt(plain_text, encrypt_text, 32);
aes_ecb_decrypt(encrypt_text, decrypt_text, 32);
printf("plain_text: %s\n\n", plain_text);
printf("decrypt_text: %s\n", decrypt_text);
#if 0
int i = 0;
printf("plain_text: ");
for (i = 0; i < strlen(plain_text); i++)
{
printf("0x%x ", plain_text[i]);
}
printf("\n\n");
printf("enctypt_text: ");
for (i = 0; i < strlen(plain_text); i++)
{
printf("0x%x ", encrypt_text[i]);
}
printf("\n\n");
printf("dectypt_text: ");
for (i = 0; i < strlen(plain_text); i++)
{
printf("0x%x ", decrypt_text[i]);
}
printf("\n\n");
#endif
return 0;
}