BulldogTool.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // BulldogTool.cpp
  3. // UUTUtils
  4. //
  5. // Created by zhuge on 2018/1/23.
  6. // Copyright © 2018年 zhuge. All rights reserved.
  7. //
  8. #include "BulldogTool.h"
  9. #include <zlib.h>
  10. #include "base64.h"
  11. #include "ZipUtils.h"
  12. #include <stdlib.h>
  13. static BulldogTool* mBulldogTool = nullptr;
  14. BulldogTool* BulldogTool::getInstance(){
  15. if (!mBulldogTool) {
  16. srand((int)time(0));
  17. mBulldogTool = new(std::nothrow) BulldogTool();
  18. }
  19. return mBulldogTool;
  20. }
  21. string BulldogTool::encrypt(string content){
  22. string ret = "";
  23. uLong tlen = strlen(content.c_str()); /* 需要把字符串的结束符'\0'也一并处理 */
  24. char* buf = NULL;
  25. uLong blen;
  26. z_stream c_stream;
  27. int err = 0;
  28. c_stream.zalloc = NULL;
  29. c_stream.zfree = NULL;
  30. c_stream.opaque = NULL;
  31. //只有设置为MAX_WBITS + 16才能在在压缩文本中带header和trailer
  32. auto initRs = deflateInit2(&c_stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
  33. MAX_WBITS + 16, 8, Z_DEFAULT_STRATEGY);
  34. if (initRs != Z_OK) {
  35. return "";
  36. }
  37. /* 计算缓冲区大小,并为其分配内存 */
  38. blen = deflateBound(&c_stream,tlen); /* 压缩后的长度是不会超过blen的 */
  39. if((buf = (char*)malloc(sizeof(char) * blen)) == NULL){
  40. printf("no enough memory!\n");
  41. }
  42. memset(buf, 0, blen);
  43. c_stream.next_in = (Bytef*)content.c_str();
  44. c_stream.avail_in = tlen;
  45. c_stream.next_out = (Bytef*)buf;
  46. c_stream.avail_out = blen;
  47. while(c_stream.avail_in != 0 && c_stream.total_out < blen) {
  48. auto defateRs = deflate(&c_stream, Z_NO_FLUSH);
  49. if (!(defateRs == Z_OK)) {
  50. return "";
  51. }
  52. }
  53. if (!(c_stream.avail_in == 0)) {
  54. return "";
  55. }
  56. for(;;) {
  57. if((err = deflate(&c_stream, Z_FINISH)) == Z_STREAM_END) break;
  58. if (!(err == Z_OK)) {
  59. return "";
  60. }
  61. }
  62. auto deflatEndRs = deflateEnd(&c_stream);
  63. if (!(deflatEndRs == Z_OK)) {
  64. return "";
  65. }
  66. blen = c_stream.total_out;
  67. char *buffer = nullptr;
  68. int len = base64Encode((unsigned char*)buf, blen, &buffer);
  69. if (buffer != nullptr) {
  70. for (int i = 0 ; i < len; i++) {
  71. //base64的结果再1、2位置互换
  72. if (i % 2 == 0 && (i + 1) < len ) {
  73. char tmp = buffer[i];
  74. buffer[i] = buffer[i+1];
  75. buffer[i+1] = tmp;
  76. }
  77. }
  78. for (int i = 0 ; i < len; i++) {
  79. //加上干扰字符
  80. if (i % 10 == 0) {
  81. ret = ret + randomString(1);
  82. }
  83. //对base64Encode结果 '+'转'-' '/'转'_' (为了网络传输成功)
  84. if (buffer[i] == '+') {
  85. buffer[i] = '-';
  86. }
  87. if (buffer[i] == '/') {
  88. buffer[i] = '_';
  89. }
  90. ret = ret + string(buffer+i, buffer + i+1);
  91. }
  92. // ret = string(buffer, buffer + len);
  93. }
  94. free(buf);
  95. free(buffer);
  96. return ret;
  97. }
  98. string BulldogTool::decrypt(string content){
  99. if(content == ""){
  100. return "";
  101. }
  102. string clearContent = "";
  103. for (int i = 0; i < content.length(); i++) {
  104. //对base64Decode结果 '-'转'+' '_'转'/'
  105. if (content[i] == '-') {
  106. content[i] = '+';
  107. }
  108. if (content[i] == '_') {
  109. content[i] = '/';
  110. }
  111. //去掉干扰字符
  112. if (i % 11 != 0) {
  113. clearContent += content[i];
  114. }
  115. }
  116. for (int i = 0; i < clearContent.length(); i++) {
  117. //base64的结果再1、2位置互换
  118. if (i % 2 == 0 && (i + 1) < clearContent.length() ) {
  119. char tmp = clearContent[i];
  120. clearContent[i] = clearContent[i+1];
  121. clearContent[i+1] = tmp;
  122. }
  123. }
  124. string ret = "";
  125. unsigned char *buffer = nullptr;
  126. unsigned char *deflated = nullptr;
  127. int dataLen = strlen(clearContent.c_str());
  128. int decodeLen = base64Decode((unsigned char*)clearContent.c_str(), (unsigned int)dataLen, &buffer);
  129. if (buffer != nullptr) {
  130. ssize_t deflatedLen = ZipUtils::inflateMemory(buffer, decodeLen, &deflated);
  131. if (deflated != nullptr) {
  132. ret = string((char*)deflated, (char*)deflated + deflatedLen);
  133. }
  134. }
  135. free(buffer);
  136. free(deflated);
  137. return ret;
  138. }
  139. int BulldogTool::randomInt(int start, int end){
  140. if (end < start) {
  141. // ----------ERROR:随机值范围不对
  142. return 0;
  143. }
  144. int len = end - start + 1;
  145. int value = rand() % len;
  146. return value + start;
  147. }
  148. string BulldogTool::randomString(int len){
  149. char charPool[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r',
  150. 's','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N',
  151. 'O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9'};
  152. char retTmp[len+1];
  153. memset(&retTmp[0], 0, sizeof(retTmp) / sizeof(char));
  154. for (int i = 0 ; i < len; i++) {
  155. int randIndex = randomInt(0, (sizeof(charPool) / sizeof(char)) - 1);
  156. retTmp[i] = charPool[randIndex];
  157. }
  158. string ret = string(retTmp);
  159. return ret;
  160. }