Encrypt.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. #ifndef _ENCRYPT_RC4_
  2. #define _ENCRYPT_RC4_
  3. #include <iomanip>
  4. #include <sstream>
  5. #include <string.h>
  6. #include <curl/curl.h>
  7. #include <openssl/pem.h>
  8. #include <openssl/err.h>
  9. #include <openssl/bio.h>
  10. #include <openssl/evp.h>
  11. #include "log.hpp"
  12. #include "obfuscate.hpp"
  13. #define BOX_LEN 256
  14. int GetKey(const unsigned char *pass, int pass_len, unsigned char *out);
  15. // 十六进制编码
  16. std::string hex_encode(const std::string& input);
  17. // 十六进制解码
  18. std::string hex_decode(const std::string& encoded);
  19. int RC4(const unsigned char *data, int data_len, const unsigned char *key, int key_len, unsigned char *out,
  20. int *out_len);
  21. static void swap_byte(unsigned char *a, unsigned char *b);
  22. char *Encrypt(const char *szSource, const char *szPassWord); // 加密,返回加密结果
  23. char *Decrypt(const char *szSource, const char *szPassWord); // 解密,返回解密结果
  24. std::string RSA_Encrypt(const std::string& plaintext, const std::string& publicKeyPEM);
  25. std::string RSA_Decrypt(const std::string& encrypted, const std::string& privateKeyPEM);
  26. char *ByteToHex(const unsigned char *vByte, const int vLen); // 把字节码pbBuffer转为十六进制字符串,方便传输
  27. unsigned char *HexToByte(const char *szHex); // 把十六进制字符串转为字节码pbBuffer,解码
  28. #endif // #ifndef _ENCRYPT_RC4_
  29. char *Encrypt(const char *szSource, const char *szPassWord) // 加密,返回加密结果
  30. {
  31. if (szSource == NULL || szPassWord == NULL) return NULL;
  32. unsigned char *ret = new unsigned char[strlen(szSource)];
  33. int ret_len = 0;
  34. if (RC4((unsigned char *) szSource,
  35. strlen(szSource),
  36. (unsigned char *) szPassWord,
  37. strlen(szPassWord),
  38. ret,
  39. &ret_len) == 0)
  40. return NULL;
  41. char *ret2 = ByteToHex(ret, ret_len);
  42. delete[] ret;
  43. return ret2;
  44. }
  45. char *Decrypt(const char *szSource, const char *szPassWord) // 解密,返回解密结果
  46. {
  47. if (szSource == NULL || (strlen(szSource) % 2 != 0) || szPassWord == NULL)
  48. return NULL;
  49. unsigned char *src = HexToByte(szSource);
  50. unsigned char *ret = new unsigned char[strlen(szSource) / 2 + 1];
  51. int ret_len = 0;
  52. memset(ret, strlen(szSource) / 2 + 1, 0);
  53. if (RC4(src, strlen(szSource) / 2, (unsigned char *) szPassWord, strlen(szPassWord), ret, &ret_len) == 0)
  54. return NULL;
  55. ret[ret_len] = '\0';
  56. return (char *) ret;
  57. }
  58. // 十六进制编码
  59. std::string hex_encode(const std::string& input) {
  60. std::stringstream ss;
  61. for (size_t i = 0; i < input.size(); i++) {
  62. ss << std::hex << std::setw(2) << std::setfill('0') << (int)(unsigned char)input[i];
  63. }
  64. return ss.str();
  65. }
  66. // 十六进制解码
  67. std::string hex_decode(const std::string& encoded) {
  68. std::string result;
  69. for (size_t i = 0; i < encoded.length(); i += 2) {
  70. std::string byteString = encoded.substr(i, 2);
  71. char byte = (char)strtol(byteString.c_str(), NULL, 16);
  72. result.push_back(byte);
  73. }
  74. return result;
  75. }
  76. // RSA 加密(使用 EVP_PKEY)
  77. std::string RSA_Encrypt(const std::string& plaintext, const std::string& publicKeyPEM) {
  78. BIO* bio = BIO_new_mem_buf(publicKeyPEM.data(), -1); // 使用 -1 自动检测长度
  79. if (!bio) {
  80. // 无法创建 BIO 对象
  81. LOG_DEBUG("无法创建 BIO 对象");
  82. return "";
  83. }
  84. EVP_PKEY* publicKey = PEM_read_bio_PUBKEY(bio, nullptr, nullptr, nullptr);
  85. BIO_free(bio);
  86. if (!publicKey) {
  87. // 无法加载公钥
  88. LOG_DEBUG ("无法加载公钥");
  89. return "";
  90. }
  91. EVP_PKEY_CTX* ctx = EVP_PKEY_CTX_new(publicKey, NULL);
  92. if (!ctx) {
  93. // 无法创建 EVP_PKEY_CTX
  94. LOG_DEBUG ("无法创建 EVP_PKEY_CTX");
  95. EVP_PKEY_free(publicKey);
  96. return "";
  97. }
  98. if (EVP_PKEY_encrypt_init(ctx) <= 0) {
  99. // 初始化加密失败
  100. LOG_DEBUG ("初始化加密失败");
  101. EVP_PKEY_CTX_free(ctx);
  102. EVP_PKEY_free(publicKey);
  103. return "";
  104. }
  105. size_t outlen;
  106. unsigned char* encrypted = (unsigned char*)malloc(EVP_PKEY_size(publicKey));
  107. if (EVP_PKEY_encrypt(ctx, encrypted, &outlen, (unsigned char*)plaintext.data(), plaintext.size()) <= 0) {
  108. // 加密失败
  109. LOG_DEBUG ("加密失败");
  110. free(encrypted);
  111. EVP_PKEY_CTX_free(ctx);
  112. EVP_PKEY_free(publicKey);
  113. return "";
  114. }
  115. std::string encryptedStr((char*)encrypted, outlen);
  116. free(encrypted);
  117. EVP_PKEY_CTX_free(ctx);
  118. EVP_PKEY_free(publicKey);
  119. return hex_encode(encryptedStr);
  120. }
  121. // RSA 解密(使用 EVP_PKEY)
  122. std::string RSA_Decrypt(const std::string& encrypted, const std::string& privateKeyPEM) {
  123. BIO* bio = BIO_new_mem_buf(privateKeyPEM.data(), -1);
  124. if (!bio) {
  125. // 无法创建 BIO 对象
  126. return "";
  127. }
  128. EVP_PKEY* privateKey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
  129. BIO_free(bio);
  130. if (!privateKey) {
  131. // 无法加载私钥
  132. return "";
  133. }
  134. EVP_PKEY_CTX* ctx = EVP_PKEY_CTX_new(privateKey, NULL);
  135. if (!ctx) {
  136. // 无法创建 EVP_PKEY_CTX
  137. EVP_PKEY_free(privateKey);
  138. return "";
  139. }
  140. if (EVP_PKEY_decrypt_init(ctx) <= 0) {
  141. // 初始化解密失败
  142. EVP_PKEY_CTX_free(ctx);
  143. EVP_PKEY_free(privateKey);
  144. return "";
  145. }
  146. size_t outlen;
  147. unsigned char* decrypted = (unsigned char*)malloc(EVP_PKEY_size(privateKey));
  148. if (EVP_PKEY_decrypt(ctx, decrypted, &outlen, (unsigned char*)hex_decode(encrypted).data(), hex_decode(encrypted).size()) <= 0) {
  149. // 解密失败
  150. free(decrypted);
  151. EVP_PKEY_CTX_free(ctx);
  152. EVP_PKEY_free(privateKey);
  153. return "";
  154. }
  155. std::string decryptedStr((char*)decrypted, outlen);
  156. free(decrypted);
  157. EVP_PKEY_CTX_free(ctx);
  158. EVP_PKEY_free(privateKey);
  159. return decryptedStr;
  160. }
  161. int RC4(const unsigned char *data, int data_len, const unsigned char *key, int key_len, unsigned char *out,
  162. int *out_len) {
  163. if (data == NULL || key == NULL || out == NULL)
  164. return 0;
  165. unsigned char *mBox = new unsigned char[BOX_LEN];
  166. if (GetKey(key, key_len, mBox) == 0)
  167. return 0;
  168. int i = 0;
  169. int x = 0;
  170. int y = 0;
  171. for (int k = 0; k < data_len; k++) {
  172. x = (x + 1) % BOX_LEN;
  173. y = (mBox[x] + y) % BOX_LEN;
  174. swap_byte(&mBox[x], &mBox[y]);
  175. out[k] = data[k] ^ mBox[(mBox[x] + mBox[y]) % BOX_LEN];
  176. }
  177. *out_len = data_len;
  178. delete[] mBox;
  179. return -1;
  180. }
  181. int GetKey(const unsigned char *pass, int pass_len, unsigned char *out) {
  182. if (pass == NULL || out == NULL)
  183. return 0;
  184. int i;
  185. for (i = 0; i < BOX_LEN; i++)
  186. out[i] = i;
  187. int j = 0;
  188. for (i = 0; i < BOX_LEN; i++) {
  189. j = (pass[i % pass_len] + out[i] + j) % BOX_LEN;
  190. swap_byte(&out[i], &out[j]);
  191. }
  192. return -1;
  193. }
  194. static void swap_byte(unsigned char *a, unsigned char *b) {
  195. unsigned char swapByte;
  196. swapByte = *a;
  197. *a = *b;
  198. *b = swapByte;
  199. }
  200. // 把字节码转为十六进制码,一个字节两个十六进制,内部为字符串分配空间
  201. char *ByteToHex(const unsigned char *vByte, const int vLen) {
  202. if (!vByte)
  203. return NULL;
  204. char *tmp = new char[vLen * 2 + 1]; // 一个字节两个十六进制码,最后要多一个'\0'
  205. int tmp2;
  206. for (int i = 0; i < vLen; i++) {
  207. tmp2 = (int) (vByte[i]) / 16;
  208. tmp[i * 2] = (char) (tmp2 + ((tmp2 > 9) ? 'a' - 10 : '0'));
  209. tmp2 = (int) (vByte[i]) % 16;
  210. tmp[i * 2 + 1] = (char) (tmp2 + ((tmp2 > 9) ? 'a' - 10 : '0'));
  211. }
  212. tmp[vLen * 2] = '\0';
  213. return tmp;
  214. }
  215. // 把十六进制字符串,转为字节码,每两个十六进制字符作为一个字节
  216. unsigned char *HexToByte(const char *szHex) {
  217. if (!szHex)
  218. return NULL;
  219. int iLen = strlen(szHex);
  220. if (iLen <= 0 || 0 != iLen % 2)
  221. return NULL;
  222. unsigned char *pbBuf = new unsigned char[iLen / 2]; // 数据缓冲区
  223. int tmp1, tmp2;
  224. for (int i = 0; i < iLen / 2; i++) {
  225. tmp1 = (int) szHex[i * 2] - (((int) szHex[i * 2] >= 'a') ? 'a' - 10 : '0');
  226. if (tmp1 >= 16)
  227. return NULL;
  228. tmp2 = (int) szHex[i * 2 + 1] - (((int) szHex[i * 2 + 1] >= 'a') ? 'a' - 10 : '0');
  229. if (tmp2 >= 16)
  230. return NULL;
  231. pbBuf[i] = (tmp1 * 16 + tmp2);
  232. }
  233. return pbBuf;
  234. }