123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- //
- // BulldogTool.cpp
- // UUTUtils
- //
- // Created by zhuge on 2018/1/23.
- // Copyright © 2018年 zhuge. All rights reserved.
- //
- #include "BulldogTool.h"
- #include <zlib.h>
- #include "base64.h"
- #include "ZipUtils.h"
- #include <stdlib.h>
- static BulldogTool* mBulldogTool = nullptr;
- BulldogTool* BulldogTool::getInstance(){
- if (!mBulldogTool) {
- srand((int)time(0));
- mBulldogTool = new(std::nothrow) BulldogTool();
- }
- return mBulldogTool;
- }
- string BulldogTool::encrypt(string content){
- string ret = "";
- uLong tlen = strlen(content.c_str()); /* 需要把字符串的结束符'\0'也一并处理 */
-
- char* buf = NULL;
- uLong blen;
-
-
- z_stream c_stream;
- int err = 0;
-
- c_stream.zalloc = NULL;
- c_stream.zfree = NULL;
- c_stream.opaque = NULL;
- //只有设置为MAX_WBITS + 16才能在在压缩文本中带header和trailer
- auto initRs = deflateInit2(&c_stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
- MAX_WBITS + 16, 8, Z_DEFAULT_STRATEGY);
- if (initRs != Z_OK) {
- return "";
- }
- /* 计算缓冲区大小,并为其分配内存 */
- blen = deflateBound(&c_stream,tlen); /* 压缩后的长度是不会超过blen的 */
- if((buf = (char*)malloc(sizeof(char) * blen)) == NULL){
- printf("no enough memory!\n");
- }
- memset(buf, 0, blen);
-
- c_stream.next_in = (Bytef*)content.c_str();
- c_stream.avail_in = tlen;
- c_stream.next_out = (Bytef*)buf;
- c_stream.avail_out = blen;
- while(c_stream.avail_in != 0 && c_stream.total_out < blen) {
- auto defateRs = deflate(&c_stream, Z_NO_FLUSH);
- if (!(defateRs == Z_OK)) {
- return "";
- }
- }
- if (!(c_stream.avail_in == 0)) {
- return "";
- }
- for(;;) {
- if((err = deflate(&c_stream, Z_FINISH)) == Z_STREAM_END) break;
- if (!(err == Z_OK)) {
- return "";
- }
- }
- auto deflatEndRs = deflateEnd(&c_stream);
- if (!(deflatEndRs == Z_OK)) {
- return "";
- }
- blen = c_stream.total_out;
-
- char *buffer = nullptr;
- int len = base64Encode((unsigned char*)buf, blen, &buffer);
- if (buffer != nullptr) {
- for (int i = 0 ; i < len; i++) {
- //base64的结果再1、2位置互换
- if (i % 2 == 0 && (i + 1) < len ) {
- char tmp = buffer[i];
- buffer[i] = buffer[i+1];
- buffer[i+1] = tmp;
- }
- }
-
- for (int i = 0 ; i < len; i++) {
- //加上干扰字符
- if (i % 10 == 0) {
- ret = ret + randomString(1);
- }
- //对base64Encode结果 '+'转'-' '/'转'_' (为了网络传输成功)
- if (buffer[i] == '+') {
- buffer[i] = '-';
- }
- if (buffer[i] == '/') {
- buffer[i] = '_';
- }
- ret = ret + string(buffer+i, buffer + i+1);
- }
- // ret = string(buffer, buffer + len);
- }
- free(buf);
- free(buffer);
-
- return ret;
- }
- string BulldogTool::decrypt(string content){
- if(content == ""){
- return "";
- }
-
- string clearContent = "";
- for (int i = 0; i < content.length(); i++) {
- //对base64Decode结果 '-'转'+' '_'转'/'
- if (content[i] == '-') {
- content[i] = '+';
- }
- if (content[i] == '_') {
- content[i] = '/';
- }
- //去掉干扰字符
- if (i % 11 != 0) {
- clearContent += content[i];
- }
- }
- for (int i = 0; i < clearContent.length(); i++) {
- //base64的结果再1、2位置互换
- if (i % 2 == 0 && (i + 1) < clearContent.length() ) {
- char tmp = clearContent[i];
- clearContent[i] = clearContent[i+1];
- clearContent[i+1] = tmp;
- }
-
- }
-
- string ret = "";
- unsigned char *buffer = nullptr;
- unsigned char *deflated = nullptr;
- int dataLen = strlen(clearContent.c_str());
- int decodeLen = base64Decode((unsigned char*)clearContent.c_str(), (unsigned int)dataLen, &buffer);
- if (buffer != nullptr) {
- ssize_t deflatedLen = ZipUtils::inflateMemory(buffer, decodeLen, &deflated);
- if (deflated != nullptr) {
- ret = string((char*)deflated, (char*)deflated + deflatedLen);
- }
- }
- free(buffer);
- free(deflated);
- return ret;
- }
- int BulldogTool::randomInt(int start, int end){
- if (end < start) {
- // ----------ERROR:随机值范围不对
- return 0;
- }
- int len = end - start + 1;
- int value = rand() % len;
- return value + start;
- }
- string BulldogTool::randomString(int len){
- char charPool[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r',
- 's','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N',
- 'O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9'};
- char retTmp[len+1];
- memset(&retTmp[0], 0, sizeof(retTmp) / sizeof(char));
- for (int i = 0 ; i < len; i++) {
- int randIndex = randomInt(0, (sizeof(charPool) / sizeof(char)) - 1);
- retTmp[i] = charPool[randIndex];
- }
- string ret = string(retTmp);
- return ret;
- }
|