|
@@ -7,7 +7,8 @@
|
|
|
#include <toolkit.h>
|
|
|
#include "include/aes/aes_utils.h"
|
|
|
|
|
|
-static const unsigned char HEX[16] = {0x10, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
|
|
|
+static const unsigned char HEX[16] = {0x10, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
|
|
|
+ 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
|
|
|
|
|
|
static const unsigned char CHAR_SET[36] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
|
|
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
|
|
@@ -66,12 +67,17 @@ void AesUtils::RemovePadding(uint8_t *out, size_t length) {
|
|
|
}
|
|
|
|
|
|
char *AesUtils::GetRawKey() {
|
|
|
- uint8_t *random_key = GetKey();
|
|
|
+ const int len = 26;
|
|
|
time_t ts = time(nullptr);
|
|
|
- char *key = static_cast<char *>(malloc(33));
|
|
|
- sprintf(key, "%ld%s", ts, random_key);
|
|
|
- key[33] = '\0';
|
|
|
- return key;
|
|
|
+ srandom(ts);
|
|
|
+ auto *raw_key = static_cast<char *>(malloc(len + 1));
|
|
|
+ memset(raw_key, 0, len + 1);
|
|
|
+ for (int i = 0; i < 16; ++i) {
|
|
|
+ raw_key[i] = CHAR_SET[random() % 36];
|
|
|
+ }
|
|
|
+ sprintf(raw_key, "%s%ld", raw_key, ts);
|
|
|
+ raw_key[len] = '\0';
|
|
|
+ return raw_key;
|
|
|
}
|
|
|
|
|
|
uint8_t *AesUtils::GetKey() {
|
|
@@ -133,7 +139,9 @@ char *AesUtils::Encrypt(const char *input, const uint8_t *key) {
|
|
|
* AES解密, CBC, PKCS5Padding
|
|
|
*/
|
|
|
char *AesUtils::Decrypt(const char *input, const uint8_t *key) {
|
|
|
+
|
|
|
const uint8_t *iv = GetIv(key);
|
|
|
+
|
|
|
|
|
|
size_t len = strlen(input);
|
|
|
unsigned char *input_des = HexUtils::HexDecode(input);
|