Otclient  14/8/2020
crypt.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #ifndef CRYPT_H
24 #define CRYPT_H
25 
26 #include "../stdext/types.h"
27 #include <string>
28 
29 #include <boost/uuid/uuid.hpp>
30 #ifdef USE_GMP
31 #include <gmp.h>
32 #else
33 typedef struct rsa_st RSA;
34 #endif
35 
36 class Crypt
37 {
38 public:
39  Crypt();
40  ~Crypt();
41 
42  std::string base64Encode(const std::string& decoded_string);
43  std::string base64Decode(const std::string& encoded_string);
44  std::string xorCrypt(const std::string& buffer, const std::string& key);
45  std::string encrypt(const std::string& decrypted_string) { return _encrypt(decrypted_string, true); }
46  std::string decrypt(const std::string& encrypted_string) { return _decrypt(encrypted_string, true); }
47  std::string genUUID();
48  bool setMachineUUID(std::string uuidstr);
49  std::string getMachineUUID();
50 
51  void rsaSetPublicKey(const std::string& n, const std::string& e);
52  void rsaSetPrivateKey(const std::string &p, const std::string &q, const std::string &d);
53  bool rsaEncrypt(unsigned char *msg, int size);
54  bool rsaDecrypt(unsigned char *msg, int size);
55  int rsaGetSize();
56 
57 private:
58  std::string _encrypt(const std::string& decrypted_string, bool useMachineUUID);
59  std::string _decrypt(const std::string& encrypted_string, bool useMachineUUID);
60  std::string getCryptKey(bool useMachineUUID);
61  boost::uuids::uuid m_machineUUID;
62 #ifdef USE_GMP
63  mpz_t m_p, m_q, m_n, m_e, m_d;
64 #else
65  RSA *m_rsa;
66 #endif
67 };
68 
69 extern Crypt g_crypt;
70 
71 #endif
Crypt::rsaSetPrivateKey
void rsaSetPrivateKey(const std::string &p, const std::string &q, const std::string &d)
Definition: crypt.cpp:262
Crypt::rsaEncrypt
bool rsaEncrypt(unsigned char *msg, int size)
Definition: crypt.cpp:296
Crypt::base64Decode
std::string base64Decode(const std::string &encoded_string)
Definition: crypt.cpp:115
Crypt::rsaGetSize
int rsaGetSize()
Definition: crypt.cpp:350
Crypt::Crypt
Crypt()
Definition: crypt.cpp:46
Crypt::xorCrypt
std::string xorCrypt(const std::string &buffer, const std::string &key)
Definition: crypt.cpp:158
Crypt::rsaSetPublicKey
void rsaSetPublicKey(const std::string &n, const std::string &e)
Definition: crypt.cpp:239
Crypt::getMachineUUID
std::string getMachineUUID()
Definition: crypt.cpp:189
g_crypt
Crypt g_crypt
Definition: crypt.cpp:44
Crypt::decrypt
std::string decrypt(const std::string &encrypted_string)
Definition: crypt.h:46
Crypt::setMachineUUID
bool setMachineUUID(std::string uuidstr)
Definition: crypt.cpp:178
Crypt::~Crypt
~Crypt()
Definition: crypt.cpp:59
RSA
struct rsa_st RSA
Definition: crypt.h:33
Crypt::rsaDecrypt
bool rsaDecrypt(unsigned char *msg, int size)
Definition: crypt.cpp:323
Crypt::base64Encode
std::string base64Encode(const std::string &decoded_string)
Definition: crypt.cpp:72
Crypt
Definition: crypt.h:36
Crypt::encrypt
std::string encrypt(const std::string &decrypted_string)
Definition: crypt.h:45
Crypt::genUUID
std::string genUUID()
Definition: crypt.cpp:171