base64.cpp
#include "Base64.h" const std::string CBase64::base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" "0123456789+/"; int CBase64::find_utf_8_bit_head(const unsigned char *src_content,int src_size ){ int i_ret = -1; int byteNum = 0; //字符数 if( src_content ) { for(int i = src_size-1; i >= 0; i--) { if( 0 == (src_content[i] >> 7 ) ) { byteNum = 1; i_ret = src_size - i; break; } if( 0x06 == (src_content[i]>> 5) ) { byteNum = 2; i_ret = src_size - i; break; } if( 0x0E == (src_content[i] >> 4) ) { byteNum = 3; i_ret = src_size - i; break; } if( 0x1E == (src_content[i] >> 3) ) { byteNum = 4; i_ret = src_size - i; break; } if( 0x3E == (src_content[i] >> 2) ) { byteNum = 5; i_ret = src_size - i; break; } if( 0x7E == (src_content[i] >> 1) ) { byteNum = 6; i_ret = src_size - i; break; } } if( i_ret == byteNum ) i_ret = -1; } return i_ret;} std::string CBase64::base64_encode(const unsigned char* bytes_to_encode, unsigned int in_len) { std::string ret; int i = 0; int j = 0; unsigned char char_array_3[3]; unsigned char char_array_4[4]; while (in_len--) { char_array_3[i++] = *(bytes_to_encode++); if (i == 3) { char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); char_array_4[3] = char_array_3[2] & 0x3f; for(i = 0; (i <4) ; i++) ret += base64_chars[char_array_4[i]]; i = 0; } } if (i) { for(j = i; j < 3; j++) char_array_3[j] = ''; char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); char_array_4[3] = char_array_3[2] & 0x3f; for (j = 0; (j < i + 1); j++) ret += base64_chars[char_array_4[j]]; while((i++ < 3)) ret += '='; } return ret; } //CString CBase64::Base64Encode(LPCTSTR lpszSrc)//{// char* strSrc = new char[_tcslen(lpszSrc)+1];// ZeroMemory(strSrc, _tcslen(lpszSrc)+1);// strcpy(strSrc, (char*)(_bstr_t(lpszSrc)));// std::string str = base64_encode((unsigned char*)strSrc, (int)strlen(strSrc));// CString strDst = str.c_str();// return strDst;//} std::string CBase64::Base64Encode(char* lpszSrc){ int str_len = strlen(lpszSrc); int find_index = find_utf_8_bit_head((unsigned char*)lpszSrc, str_len); if(find_index > -1) { memset(lpszSrc+(str_len-find_index), 0, find_index); } str_len = strlen(lpszSrc); return base64_encode((unsigned char*)lpszSrc, str_len);} std::string CBase64::base64_decode(const unsigned char* bytes_to_encode, unsigned int in_len) { int i = 0; int j = 0; int in_ = 0; unsigned char char_array_4[4], char_array_3[3]; std::string ret; while (in_len-- && ( bytes_to_encode[in_] != '=') /*&& is_base64(bytes_to_encode[in_])*/) { char_array_4[i++] = bytes_to_encode[in_]; in_++; if (i ==4) { for (i = 0; i <4; i++) char_array_4[i] = base64_chars.find(char_array_4[i]); char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; for (i = 0; (i < 3); i++) ret += char_array_3[i]; i = 0; } } if (i) { for (j = i; j <4; j++) char_array_4[j] = 0; for (j = 0; j <4; j++) char_array_4[j] = base64_chars.find(char_array_4[j]); char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; for (j = 0; (j < i - 1); j++) ret += char_array_3[j]; } return ret; } //CString CBase64::Base64Decode(LPCTSTR lpszSrc)//{// char* strSrc = new char[_tcslen(lpszSrc)+1];// ZeroMemory(strSrc, _tcslen(lpszSrc)+1);// strcpy(strSrc, (char*)(_bstr_t(lpszSrc)));// std::string str = base64_decode((unsigned char*)strSrc, (int)strlen(strSrc));// CString strDst = str.c_str();// return strDst;//} std::string CBase64::Base64Decode(char* lpszSrc){ return base64_decode((unsigned char*)lpszSrc, (int)strlen(lpszSrc));}
推荐阅读
- 为什么称曹操为老板,曹操为啥是魏国
- 李世民为啥要发动玄武门之变,李世民带领十人发动玄武门之变
- 什么样的人一定会成功?成功的人说要做有把握的事
- 为什么古代人都是近亲婚姻?古代近亲结婚为啥很少有毛病
- 翡翠|翡翠为啥用黑色的挂绳
- 兔子有必要做绝育吗?兔子做绝育手术好还是不做好
- 为什么历代帝王都要去泰山封禅?封禅为啥在泰山
- 二十四桥又叫什么桥?二十四桥为啥叫二十四桥
- 什么样的教师更容易获得幸福?幸福的教师要做到
- 网站上线前要做好哪些SEO优化操作?