本文共 13473 字,大约阅读时间需要 44 分钟。
string NS_PUB_API::DesEncode(const string encodeSrc,const string encodeKey){ try{ //3des加密 string stData = TC_Des::encrypt3(encodeKey.c_str(),encodeSrc.c_str(), encodeSrc.size()); //base64编码 string stRet; Base64::Encode(stData,stRet); return stRet; } catch(TC_DES_Exception &ex){ ELOG("desEncode error.....!!!"); return ""; } } bool NS_PUB_API::DesDecode(const string decodeSrc,const string decodeKey,string &outResult){ try{ string base64Str; //base64解码 Base64::Decode(decodeSrc,base64Str); //3des解密 outResult = TC_Des::decrypt3(decodeKey.c_str(), base64Str.c_str(), base64Str.size()); return true; }catch(TC_DES_Exception &ex) { ELOG("catch TC_DES_Exception"); return false; }catch(exception &ex) { ELOG("catch exception[%s]", ex.what()); return false; }catch(...) { ELOG("catch NotTC_DES_Exception"); return false; } } bool NS_PUB_API::checkSign(map_s2s mapin,string appid,string md5Key){ //暂不校验签名 return true; string md5_whole_key = md5Key+appid; string md5_src = get_md5_src(mapin,md5_whole_key); string sign=MD5::sum(md5_src); DLOG("check sigin md5_key=[%s],md5_src=[%s] sign=[%s]",md5_whole_key.c_str(),md5_src.c_str(),sign.c_str()); if (strcasecmp(sign.c_str(), mapin["sign"].c_str()) != 0){ _throw_m(NS_PUB_DEF::ERROR_CHECK_SIGN, "check sign err"); } return true; } string NS_PUB_API::get_md5_src(map_s2s &map_in, const string &key) { map<string, string>::key_compare kcomp = map_in.key_comp(); map<string, string>::const_iterator iter = map_in.begin(); string md5_src = ""; do { /* 如果值为空或者是参数名为sign,则跳过 */ if (!iter->second.empty() && "sign" != iter->first ) { md5_src += iter->first + "=" + iter->second + "&"; } } while (kcomp((*iter++).first, map_in.rbegin()->first)); md5_src += string("key=") + key; return md5_src;}
///.h头文件
//vi: set tabstop=4 shiftwidth=4 expandtab:
#ifndef _PUB_BASE64_H__ #define _PUB_BASE64_H__ #include <string> using std::string; //------------------------------------------------------------------------------ class Base64 { public: //标准base64算法(非RFC822,即不会在每76字符后添加回车换行) static bool Encode(const string &src, string &dst); static bool Encode(const char* src, const size_t len, string &dst); static bool Decode(const char* src, const size_t len, string &dst); static bool Decode(const string &src, string &dst); //为方便http传输将标准base64中的 +/= 分别转义为-_= //@note 财付通middle中常用此算法 static bool WebSafeEncode(const string &src, string &dst); static bool WebSafeEncode(const char* src, const size_t len, string &dst); static bool WebSafeDecode(const char* src, const size_t len, string &dst); static bool WebSafeDecode(const string &src, string &dst); //用于cookie编码编码 +/= 分别转义为-_. static bool CookieEncode(const string &src, string &dst); static bool CookieEncode(const char* src, const size_t len, string &dst); static bool CookieDecode(const char* src, const size_t len, string &dst); static bool CookieDecode(const string &src, string &dst); private: Base64(); Base64(const Base64 &); Base64 & operator= (const Base64 &); }; //------------------------------------------------------------------------------ #endif///base 64 .cpp
#include "pub_base64.h"
//------------------------------------------------------------------------------ #define XX 127 //Table for decoding standard base64 // if (ch >= 'A' && ch <= 'Z') return ch - 'A'; // if (ch >= 'a' && ch <= 'z') return ch - 'a' + 26; // if (ch >= '0' && ch <= '9') return ch - '0' + 52; // if (ch == '+') return 62; // if (ch == '/') return 63; static const unsigned char index_64[256] = { XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,62, XX,XX,XX,63, 52,53,54,55, 56,57,58,59, 60,61,XX,XX, XX,XX,XX,XX, XX, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14, 15,16,17,18, 19,20,21,22, 23,24,25,XX, XX,XX,XX,XX, XX,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40, 41,42,43,44, 45,46,47,48, 49,50,51,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX }; #define CHAR64(c) (index_64[(unsigned char)(c)]) //Table for decoding websafe base64 // if (ch >= 'A' && ch <= 'Z') return ch - 'A'; // if (ch >= 'a' && ch <= 'z') return ch - 'a' + 26; // if (ch >= '0' && ch <= '9') return ch - '0' + 52; // if (ch == '-') return 62; // if (ch == '_') return 63; static const unsigned char websafe_index_64[256] = { XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,62,XX,XX, 52,53,54,55, 56,57,58,59, 60,61,XX,XX, XX,XX,XX,XX, XX, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14, 15,16,17,18, 19,20,21,22, 23,24,25,XX, XX,XX,XX,63, XX,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40, 41,42,43,44, 45,46,47,48, 49,50,51,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX }; #define WEBCHAR64(c) (websafe_index_64[(unsigned char)(c)]) static const char base64_char[64] = { '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','+','/' }; static const char websafe_base64_char[64] = { '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','-','_' }; //------------------------------------------------------------------------------ bool Base64::Encode(const string &src, string &dst) { return Base64::Encode(src.data(), src.size(), dst); } //------------------------------------------------------------------------------ bool Base64::Encode(const char* src, const size_t len, string &dst) { if (src == NULL) return false; const size_t grp = (len + 2) / 3; const char pad = '='; for (size_t i = 0; i < grp; ++i) { const unsigned char *input = (unsigned char*)src + i * 3; size_t glen = 3; if (i == grp - 1) glen = len - i * 3; if (glen == 3) { dst.push_back(base64_char[input[0]>>2]); dst.push_back(base64_char[((input[0]&0x03)<<4) + (input[1]>>4)]); dst.push_back(base64_char[((input[1]&0x0f)<<2) + (input[2]>>6)]); dst.push_back(base64_char[input[2]&0x3f]); } else if (glen == 2) { dst.push_back(base64_char[input[0]>>2]); dst.push_back(base64_char[((input[0]&0x03)<<4) + (input[1]>>4)]); dst.push_back(base64_char[((input[1]&0x0f)<<2)]); dst.push_back(pad); } else if (glen == 1) { dst.push_back(base64_char[input[0]>>2]); dst.push_back(base64_char[((input[0]&0x03)<<4)]); dst.push_back(pad); dst.push_back(pad); } } return true; } //------------------------------------------------------------------------------ bool Base64::Decode(const string &src, string &dst) { return Base64::Decode(src.data(), src.size(), dst); } //------------------------------------------------------------------------------ bool Base64::Decode(const char* src, const size_t len, string &dst) { if (src == NULL) return false; if ((len % 4) != 0) return false; //must be 4-bytes align. const char pad = '='; for (size_t i = 0; i < len; i += 4) { const unsigned char *input = (unsigned char *)src + i; size_t cnt = 0; unsigned char output[4] = {0}; if (input[2] == pad && input[3] == pad) { output[0] = CHAR64(input[0])<<2; output[0] |= CHAR64(input[1])>>4; cnt = 1; } else if (input[3] == pad) { output[0] = CHAR64(input[0])<<2; output[0] |= CHAR64(input[1])>>4; output[1] = CHAR64(input[1])<<4; output[1] |= CHAR64(input[2])>>2; cnt = 2; } else { output[0] = CHAR64(input[0])<<2; output[0] |= CHAR64(input[1])>>4; output[1] = CHAR64(input[1])<<4; output[1] |= CHAR64(input[2])>>2; output[2] = CHAR64(input[2])<<6; output[2] |= CHAR64(input[3]); cnt = 3; } dst.append((char*)output, cnt); } return true; } //------------------------------------------------------------------------------ bool Base64::WebSafeEncode(const string &src, string &dst) { return Base64::WebSafeEncode(src.data(), src.size(), dst); } //------------------------------------------------------------------------------ bool Base64::WebSafeEncode(const char* src, const size_t len, string &dst) { if (src == NULL) return false; const size_t grp = (len + 2) / 3; const char pad = '='; for (size_t i = 0; i < grp; ++i) { const unsigned char *input = (unsigned char*)src + i * 3; size_t glen = 3; if (i == grp - 1) glen = len - i * 3; if (glen == 3) { dst.push_back(websafe_base64_char[input[0]>>2]); dst.push_back(websafe_base64_char[((input[0]&0x03)<<4) + (input[1]>>4)]); dst.push_back(websafe_base64_char[((input[1]&0x0f)<<2) + (input[2]>>6)]); dst.push_back(websafe_base64_char[input[2]&0x3f]); } else if (glen == 2) { dst.push_back(websafe_base64_char[input[0]>>2]); dst.push_back(websafe_base64_char[((input[0]&0x03)<<4) + (input[1]>>4)]); dst.push_back(websafe_base64_char[((input[1]&0x0f)<<2)]); dst.push_back(pad); } else if (glen == 1) { dst.push_back(websafe_base64_char[input[0]>>2]); dst.push_back(websafe_base64_char[((input[0]&0x03)<<4)]); dst.push_back(pad); dst.push_back(pad); } } return true; } //------------------------------------------------------------------------------ bool Base64::WebSafeDecode(const string &src, string &dst) { return Base64::WebSafeDecode(src.data(), src.size(), dst); } //------------------------------------------------------------------------------ bool Base64::WebSafeDecode(const char* src, const size_t len, string &dst) { if (src == NULL) return false; if ((len % 4) != 0) return false; //must be 4-bytes align. const char pad = '='; for (size_t i = 0; i < len; i += 4) { const unsigned char *input = (unsigned char *)src + i; size_t cnt = 0; unsigned char output[4] = {0}; if (input[2] == pad && input[3] == pad) { output[0] = WEBCHAR64(input[0])<<2; output[0] |= WEBCHAR64(input[1])>>4; cnt = 1; } else if (input[3] == pad) { output[0] = WEBCHAR64(input[0])<<2; output[0] |= WEBCHAR64(input[1])>>4; output[1] = WEBCHAR64(input[1])<<4; output[1] |= WEBCHAR64(input[2])>>2; cnt = 2; } else { output[0] = WEBCHAR64(input[0])<<2; output[0] |= WEBCHAR64(input[1])>>4; output[1] = WEBCHAR64(input[1])<<4; output[1] |= WEBCHAR64(input[2])>>2; output[2] = WEBCHAR64(input[2])<<6; output[2] |= WEBCHAR64(input[3]); cnt = 3; } dst.append((char*)output, cnt); } return true; } //------------------------------------------------------------------------------ bool Base64::CookieEncode(const string &src, string &dst) { return Base64::CookieEncode(src.data(), src.size(), dst); } //------------------------------------------------------------------------------ bool Base64::CookieEncode(const char* src, const size_t len, string &dst) { if (src == NULL) return false; const size_t grp = (len + 2) / 3; const char pad = '.'; for (size_t i = 0; i < grp; ++i) { const unsigned char *input = (unsigned char*)src + i * 3; size_t glen = 3; if (i == grp - 1) glen = len - i * 3; if (glen == 3) { dst.push_back(websafe_base64_char[input[0]>>2]); dst.push_back(websafe_base64_char[((input[0]&0x03)<<4) + (input[1]>>4)]); dst.push_back(websafe_base64_char[((input[1]&0x0f)<<2) + (input[2]>>6)]); dst.push_back(websafe_base64_char[input[2]&0x3f]); } else if (glen == 2) { dst.push_back(websafe_base64_char[input[0]>>2]); dst.push_back(websafe_base64_char[((input[0]&0x03)<<4) + (input[1]>>4)]); dst.push_back(websafe_base64_char[((input[1]&0x0f)<<2)]); dst.push_back(pad); } else if (glen == 1) { dst.push_back(websafe_base64_char[input[0]>>2]); dst.push_back(websafe_base64_char[((input[0]&0x03)<<4)]); dst.push_back(pad); dst.push_back(pad); } } return true; } //------------------------------------------------------------------------------ bool Base64::CookieDecode(const string &src, string &dst) { return Base64::CookieDecode(src.data(), src.size(), dst); } //------------------------------------------------------------------------------ bool Base64::CookieDecode(const char* src, const size_t len, string &dst) { if (src == NULL) return false; if ((len % 4) != 0) return false; //must be 4-bytes align. const char pad = '.'; for (size_t i = 0; i < len; i += 4) { const unsigned char *input = (unsigned char *)src + i; size_t cnt = 0; unsigned char output[4] = {0}; if (input[2] == pad && input[3] == pad) { output[0] = WEBCHAR64(input[0])<<2; output[0] |= WEBCHAR64(input[1])>>4; cnt = 1; } else if (input[3] == pad) { output[0] = WEBCHAR64(input[0])<<2; output[0] |= WEBCHAR64(input[1])>>4; output[1] = WEBCHAR64(input[1])<<4; output[1] |= WEBCHAR64(input[2])>>2; cnt = 2; } else { output[0] = WEBCHAR64(input[0])<<2; output[0] |= WEBCHAR64(input[1])>>4; output[1] = WEBCHAR64(input[1])<<4; output[1] |= WEBCHAR64(input[2])>>2; output[2] = WEBCHAR64(input[2])<<6; output[2] |= WEBCHAR64(input[3]); cnt = 3; } dst.append((char*)output, cnt); } return true; }转载地址:http://pnrgi.baihongyu.com/