30 #include <boost/uuid/uuid_generators.hpp>
31 #include <boost/uuid/uuid_io.hpp>
33 #include <boost/functional/hash.hpp>
36 #include <openssl/rsa.h>
37 #include <openssl/bn.h>
38 #include <openssl/err.h>
41 static const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
42 static inline bool is_base64(
unsigned char c) {
return (isalnum(c) || (c ==
'+') || (c ==
'/')); }
77 uint8 char_array_3[3];
78 uint8 char_array_4[4];
80 int len = decoded_string.size();
83 char_array_3[i++] = decoded_string[pos++];
85 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
86 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
87 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
88 char_array_4[3] = char_array_3[2] & 0x3f;
90 for(i = 0; (i <4) ; i++)
91 ret += base64_chars[char_array_4[i]];
97 for(j = i; j < 3; j++)
98 char_array_3[j] =
'\0';
100 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
101 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
102 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
103 char_array_4[3] = char_array_3[2] & 0x3f;
105 for(j = 0; (j < i + 1); j++)
106 ret += base64_chars[char_array_4[j]];
117 int len = encoded_string.size();
121 uint8 char_array_4[4], char_array_3[3];
124 while(len-- && (encoded_string[in_] !=
'=') && is_base64(encoded_string[in_])) {
125 char_array_4[i++] = encoded_string[in_]; in_++;
127 for(i = 0; i <4; i++)
128 char_array_4[i] = base64_chars.find(char_array_4[i]);
130 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
131 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
132 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
134 for(i = 0; (i < 3); i++)
135 ret += char_array_3[i];
141 for(j = i; j <4; j++)
144 for(j = 0; j <4; j++)
145 char_array_4[j] = base64_chars.find(char_array_4[j]);
147 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
148 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
149 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
151 for(j = 0; (j < i - 1); j++)
152 ret += char_array_3[j];
161 out.resize(buffer.size());
163 for(i=0;i<buffer.size();++i) {
164 out[i] = buffer[i] ^ key[j++];
173 boost::uuids::random_generator gen;
174 boost::uuids::uuid u = gen();
182 uuidstr = _decrypt(uuidstr,
false);
183 if(uuidstr.length() != 16)
185 std::copy(uuidstr.begin(), uuidstr.end(), m_machineUUID.begin());
191 if(m_machineUUID.is_nil()) {
192 boost::uuids::random_generator gen;
193 m_machineUUID = gen();
195 return _encrypt(std::string(m_machineUUID.begin(), m_machineUUID.end()),
false);
198 std::string Crypt::getCryptKey(
bool useMachineUUID)
200 boost::hash<boost::uuids::uuid> uuid_hasher;
201 boost::uuids::uuid uuid;
203 uuid = m_machineUUID;
205 boost::uuids::nil_generator nilgen;
208 boost::uuids::name_generator namegen(uuid);
210 std::size_t hash = uuid_hasher(u);
212 key.assign((
const char *)&hash,
sizeof(hash));
216 std::string Crypt::_encrypt(
const std::string& decrypted_string,
bool useMachineUUID)
218 std::string tmp =
"0000" + decrypted_string;
225 std::string Crypt::_decrypt(
const std::string& encrypted_string,
bool useMachineUUID)
228 std::string tmp =
xorCrypt(decoded, getCryptKey(useMachineUUID));
229 if(tmp.length() >= 4) {
231 std::string decrypted_string = tmp.substr(4);
234 return decrypted_string;
236 return std::string();
242 mpz_set_str(m_n, n.c_str(), 10);
243 mpz_set_str(m_e, e.c_str(), 10);
245 #if OPENSSL_VERSION_NUMBER < 0x10100005L
246 BN_dec2bn(&m_rsa->n, n.c_str());
247 BN_dec2bn(&m_rsa->e, e.c_str());
249 if(m_rsa->_method_mod_n) {
250 BN_MONT_CTX_free(m_rsa->_method_mod_n);
251 m_rsa->_method_mod_n =
nullptr;
254 BIGNUM *bn =
nullptr, *be =
nullptr;
255 BN_dec2bn(&bn, n.c_str());
256 BN_dec2bn(&be, e.c_str());
257 RSA_set0_key(m_rsa, bn, be,
nullptr);
265 mpz_set_str(m_p, p.c_str(), 10);
266 mpz_set_str(m_q, q.c_str(), 10);
267 mpz_set_str(m_d, d.c_str(), 10);
272 #if OPENSSL_VERSION_NUMBER < 0x10100005L
273 BN_dec2bn(&m_rsa->p, p.c_str());
274 BN_dec2bn(&m_rsa->q, q.c_str());
275 BN_dec2bn(&m_rsa->d, d.c_str());
277 if(m_rsa->_method_mod_p) {
278 BN_MONT_CTX_free(m_rsa->_method_mod_p);
279 m_rsa->_method_mod_p =
nullptr;
281 if(m_rsa->_method_mod_q) {
282 BN_MONT_CTX_free(m_rsa->_method_mod_q);
283 m_rsa->_method_mod_q =
nullptr;
286 BIGNUM *bp =
nullptr, *bq =
nullptr, *bd =
nullptr;
287 BN_dec2bn(&bp, p.c_str());
288 BN_dec2bn(&bq, q.c_str());
289 BN_dec2bn(&bd, d.c_str());
290 RSA_set0_key(m_rsa,
nullptr,
nullptr, bd);
291 RSA_set0_factors(m_rsa, bp, bq);
305 mpz_import(m, size, 1, 1, 0, 0, msg);
308 mpz_powm(c, m, m_e, m_n);
310 size_t count = (mpz_sizeinbase(m, 2) + 7) / 8;
311 memset((
char*)msg, 0, size - count);
312 mpz_export((
char*)msg + (size - count),
nullptr, 1, 1, 0, 0, c);
319 return RSA_public_encrypt(size, msg, msg, m_rsa, RSA_NO_PADDING) != -1;
332 mpz_import(c, size, 1, 1, 0, 0, msg);
335 mpz_powm(m, c, m_d, m_n);
337 size_t count = (mpz_sizeinbase(m, 2) + 7) / 8;
338 memset((
char*)msg, 0, size - count);
339 mpz_export((
char*)msg + (size - count),
nullptr, 1, 1, 0, 0, m);
346 return RSA_private_decrypt(size, msg, msg, m_rsa, RSA_NO_PADDING) != -1;
353 size_t count = (mpz_sizeinbase(m_n, 2) + 7) / 8;
354 return ((
int)count / 128) * 128;
356 return RSA_size(m_rsa);