forked from xinzsky/libutils-c
/
crypt.c
154 lines (126 loc) · 3.1 KB
/
crypt.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
/*
+----------------------------------------------------------------------+
| Author: Xingzhi Liu <dudubird2006@163.com> |
+----------------------------------------------------------------------+
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "aes.h"
#include "utils.h"
#include "xmalloc.h"
#include "xstring.h"
void *crypt_init(const char *pwdfile, int pwdmax)
{
aes_context *aesctx;
char *pwd;
int pwdlen;
char key[33];//加密密码,最多32字符,256bits
if (!pwdfile || (pwdmax != 16 && pwdmax != 24 && pwdmax != 32))
return NULL;
pwd = loadfile(pwdfile, NULL);
if (!pwd)
return NULL;
trim(pwd);
pwdlen = strlen(pwd);
if (pwdlen > pwdmax)
{
xfree(pwd);
return NULL;
}
memset(key, 0, 33);
strcpy(key, pwd);
xfree(pwd);
aesctx = (aes_context *)xcalloc(1, sizeof(aes_context));
if (aes_set_key(aesctx, (uint8 *)key, pwdmax*8))
{
xfree(aesctx);
return NULL;
}
return aesctx;
}
void crypt_free(void *ctx)
{
aes_context *aesctx = (aes_context *)ctx;
xfree(aesctx);
}
// @return: need free.
char * crypt_encode(void *ctx, const char *input, int inlen, int *outlen)
{
aes_context *aesctx;
int proclen, rmdlen;
char *cryrec;
int cryreclen;
char block[16];
aesctx = (aes_context *)ctx;
if (!ctx || !input || !inlen || !outlen)
return NULL;
cryrec = (char *)xcalloc(1, inlen + 16);
proclen = 0;
rmdlen = inlen;
while (rmdlen >= 16)
{
aes_encrypt(aesctx, (unsigned char *)input + proclen, (unsigned char *)cryrec + proclen);
proclen += 16;
rmdlen -= 16;
}
//采用pkcs5填充方式
//对不够16字节的部分进行加密,填充剩余字节数
//对于正好是16的整数倍的,最后16个字节全部填充为16
if (rmdlen >= 0 && rmdlen < 16)
{
memcpy(block, input + proclen, rmdlen);
memset(block + rmdlen, 16 - rmdlen, 16 - rmdlen);
aes_encrypt(aesctx, (unsigned char *)block, (unsigned char *)cryrec + proclen);
proclen += 16;
rmdlen = 0;
}
cryreclen = proclen;
*outlen = cryreclen;
return cryrec;
}
// @return: need free.
char * crypt_decode(void *ctx, const char *input, int inlen, int *outlen)
{
aes_context *aesctx;
int proclen, rmdlen;
char *decryrec;
aesctx = (aes_context *)ctx;
if (!ctx || !input || !inlen || !outlen)
return NULL;
decryrec = (char *)xcalloc(1, inlen + 1);
proclen = 0;
rmdlen = inlen;
while (rmdlen >= 16)
{
aes_decrypt(aesctx, (unsigned char *)input + proclen, (unsigned char *)decryrec + proclen);
proclen += 16;
rmdlen -= 16;
}
rmdlen = 16 - decryrec[proclen-1];
*outlen = proclen - 16 + rmdlen;
decryrec[*outlen] = 0;
return decryrec;
}
#ifdef TEST_CRYPT
#include "crypt.h"
int main()
{
void *aesctx;
char *s = "hello,world!";
char *r,*t;
int len;
aesctx = crypt_init(CRYPT_PWD_FILE,CRYPT_PWD_LEN16);
if(!aesctx) {
printf("call crypt_init() error.\n");
return 1;
}
r = crypt_encode(aesctx,s,strlen(s),&len);
t = crypt_decode(aesctx,r,len,&len);
printf("%s\n",t);
xfree(r);
xfree(t);
crypt_free(aesctx);
return 0;
}
#endif