/
aes_helper.cpp
159 lines (140 loc) · 4.84 KB
/
aes_helper.cpp
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
154
155
156
157
158
159
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Writed on 2015/7/16
* Author: JianYi, eyjian@qq.com or eyjian@gmail.com
*/
#include "utils/aes_helper.h"
#if MOOON_HAVE_OPENSSL == 1
# include <openssl/aes.h>
#endif // MOOON_HAVE_OPENSSL
UTILS_NAMESPACE_BEGIN
#if MOOON_HAVE_OPENSSL == 1
const int CAESHelper::aes_block_size = AES_BLOCK_SIZE; // 16
#else
const int CAESHelper::aes_block_size = 0;
#endif // MOOON_HAVE_OPENSSL
static std::string errcode2errmsg(int errcode)
{
std::string errmsg;
if (0 == errcode)
errmsg = "success";
else if (-1 == errcode)
errmsg = "userkey is empty";
else if (-2 == errcode)
errmsg = "length of userkey is invalid";
else
errmsg = "unknown error";
return errmsg;
}
CAESHelper::CAESHelper(const std::string& key)
{
_encrypt_key = NULL;
_decrypt_key = NULL;
_key = key;
const std::string::size_type LEN16 = 16;
const std::string::size_type LEN24 = 24;
const std::string::size_type LEN32 = 32;
const std::string::size_type len = key.size();
if ((len != LEN16) &&
(len != LEN24) &&
(len != LEN32))
{
if (len < LEN16)
_key.resize(LEN16);
else if (len < LEN24)
_key.resize(LEN24);
else if (len < LEN32)
_key.resize(LEN32);
else
_key.resize(LEN32);
}
}
CAESHelper::~CAESHelper()
{
#if MOOON_HAVE_OPENSSL == 1
delete (AES_KEY*)_encrypt_key;
delete (AES_KEY*)_decrypt_key;
#endif // MOOON_HAVE_OPENSSL
}
void CAESHelper::encrypt(const std::string& in, std::string* out)
{
#if MOOON_HAVE_OPENSSL == 1
if (NULL == _encrypt_key)
{
_encrypt_key = new AES_KEY;
const int errcode = AES_set_encrypt_key((const unsigned char*)(_key.data()), (int)(_key.size()*8), (AES_KEY*)_encrypt_key);
if (errcode != 0) // 理论上不会返回非0,因为构造函数已经处理好了key的长度
{
delete (AES_KEY*)_encrypt_key;
_encrypt_key = NULL;
THROW_EXCEPTION(errcode2errmsg(errcode), errcode);
}
}
aes(true, in, out, _encrypt_key);
#endif // MOOON_HAVE_OPENSSL
}
void CAESHelper::decrypt(const std::string& in, std::string* out)
{
#if MOOON_HAVE_OPENSSL == 1
if (NULL == _decrypt_key)
{
_decrypt_key = new AES_KEY;
const int errcode = AES_set_decrypt_key((const unsigned char*)(_key.data()), (int)(_key.size()*8), (AES_KEY*)_decrypt_key);
if (errcode != 0) // 理论上不会返回非0,因为构造函数已经处理好了key的长度
{
delete (AES_KEY*)_decrypt_key;
_decrypt_key = NULL;
THROW_EXCEPTION(errcode2errmsg(errcode), errcode);
}
}
aes(false, in, out, _decrypt_key);
#endif // MOOON_HAVE_OPENSSL
}
void CAESHelper::aes(bool flag, const std::string& in, std::string* out, void* aes_key)
{
#if MOOON_HAVE_OPENSSL == 1
AES_KEY* aes_key_ = (AES_KEY*)aes_key;
std::string in_tmp = in;
if (in.size() % AES_BLOCK_SIZE != 0)
{
std::string::size_type tmp_size = in.size() + (AES_BLOCK_SIZE - in.size() % AES_BLOCK_SIZE);
in_tmp.resize(tmp_size);
}
const char* in_p = in_tmp.data();
out->resize(in_tmp.size());
char* out_p = const_cast<char*>(out->data());
for (std::string::size_type i=0; i<in.size(); i+=AES_BLOCK_SIZE)
{
char out_tmp[AES_BLOCK_SIZE];
if (flag) {
// 加密
//AES_ecb_encrypt((const unsigned char*)(in_p), (unsigned char*)(out_tmp), aes_key_, AES_ENCRYPT);
AES_encrypt((const unsigned char*)(in_p), (unsigned char*)(out_tmp), aes_key_);
}
else {
// 解密
//AES_ecb_encrypt((const unsigned char*)(in_p), (unsigned char*)(out_tmp), aes_key_, AES_DECRYPT);
AES_decrypt((const unsigned char*)(in_p), (unsigned char*)(out_tmp), aes_key_);
}
in_p += AES_BLOCK_SIZE;
memcpy(out_p+i, out_tmp, AES_BLOCK_SIZE);
}
#else
*out = '\0'; // 需要加上这一句,不然难区分HAVE_OPENSSL值是否为1或不为1的情况
#endif // MOOON_HAVE_OPENSSL
}
UTILS_NAMESPACE_END