|
20 | 20 |
|
21 | 21 | #include <stdlib.h> |
22 | 22 | #include <string.h> |
23 | | - |
| 23 | +#include <openssl/evp.h> |
24 | 24 | #include <glib.h> |
25 | 25 |
|
26 | 26 | #include "network-mysqld-packet.h" |
@@ -236,6 +236,42 @@ void append_key(guint *key, guint *value, GString *str) { |
236 | 236 | } |
237 | 237 | } |
238 | 238 |
|
| 239 | +char *encrypt(char *in) { |
| 240 | + EVP_CIPHER_CTX ctx; |
| 241 | + const EVP_CIPHER *cipher = EVP_des_ecb(); |
| 242 | + unsigned char key[] = "aCtZlHaUs"; |
| 243 | + |
| 244 | + //1. DES¼ÓÃÜ |
| 245 | + EVP_CIPHER_CTX_init(&ctx); |
| 246 | + if (EVP_EncryptInit_ex(&ctx, cipher, NULL, key, NULL) != 1) return NULL; |
| 247 | + |
| 248 | + int inl = strlen(in); |
| 249 | + |
| 250 | + unsigned char inter[512] = {}; |
| 251 | + int interl = 0; |
| 252 | + |
| 253 | + if (EVP_EncryptUpdate(&ctx, inter, &interl, in, inl) != 1) return NULL; |
| 254 | + int len = interl; |
| 255 | + if (EVP_EncryptFinal_ex(&ctx, inter+len, &interl) != 1) return NULL; |
| 256 | + len += interl; |
| 257 | + EVP_CIPHER_CTX_cleanup(&ctx); |
| 258 | + |
| 259 | + //2. Base64±àÂë |
| 260 | + EVP_ENCODE_CTX ectx; |
| 261 | + EVP_EncodeInit(&ectx); |
| 262 | + |
| 263 | + char *out = g_malloc0(512); |
| 264 | + int outl = 0; |
| 265 | + |
| 266 | + EVP_EncodeUpdate(&ectx, out, &outl, inter, len); |
| 267 | + len = outl; |
| 268 | + EVP_EncodeFinal(&ectx, out+len, &outl); |
| 269 | + len += outl; |
| 270 | + |
| 271 | + if (out[len-1] == 10) out[len-1] = '\0'; |
| 272 | + return out; |
| 273 | +} |
| 274 | + |
239 | 275 | int network_backends_save(network_backends_t *bs) { |
240 | 276 | GKeyFile *keyfile = g_key_file_new(); |
241 | 277 | g_key_file_set_list_separator(keyfile, ','); |
@@ -299,7 +335,9 @@ int network_backends_save(network_backends_t *bs) { |
299 | 335 |
|
300 | 336 | GString *pwds = g_string_new(NULL); |
301 | 337 | while (g_hash_table_iter_next(&iter, &user, &pwd)) { |
302 | | - g_string_append_printf(pwds, ",%s:%s", user, pwd); |
| 338 | + gchar *encrypt_pwd = encrypt(pwd); |
| 339 | + g_string_append_printf(pwds, ",%s:%s", user, encrypt_pwd); |
| 340 | + g_free(encrypt_pwd); |
303 | 341 | } |
304 | 342 |
|
305 | 343 | if (pwds->len != 0) { |
|
0 commit comments