Skip to content

Commit 2862f1c

Browse files
author
zhuchao-hit
committed
encrypt the password before save it to config file
1 parent 982b899 commit 2862f1c

1 file changed

Lines changed: 40 additions & 2 deletions

File tree

src/network-backend.c

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
#include <stdlib.h>
2222
#include <string.h>
23-
23+
#include <openssl/evp.h>
2424
#include <glib.h>
2525

2626
#include "network-mysqld-packet.h"
@@ -236,6 +236,42 @@ void append_key(guint *key, guint *value, GString *str) {
236236
}
237237
}
238238

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+
239275
int network_backends_save(network_backends_t *bs) {
240276
GKeyFile *keyfile = g_key_file_new();
241277
g_key_file_set_list_separator(keyfile, ',');
@@ -299,7 +335,9 @@ int network_backends_save(network_backends_t *bs) {
299335

300336
GString *pwds = g_string_new(NULL);
301337
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);
303341
}
304342

305343
if (pwds->len != 0) {

0 commit comments

Comments
 (0)