C++实现 base64 字符串编码解码(GCC编译)。
/**
* @brief C++ base64 编解码
* @author wid
* @date 2013-20-25
*
* @note 若代码存在 bug 或程序缺陷, 请留言反馈, 谢谢!
*/ #include <iostream>
#include <string>
#include <ctime> //base64 编解码函数声明
std::string b64encodestring(const std::string &strString); //对 ASCII 字符串进行 base64 编码
std::string b64decodestring(const std::string &strString); //对 base64 编码后的字符串进行解码 //base64 编解码函数实现
/**
* @brief 对 ASCII 字符串进行 base64 编码
*
* @param strString 待编码的字符串
*
* @return srs::string 返回编码后的字符串
*
* @note 对于字符串中含有非 ASCII 字符串型的字符, 代码将抛出 std::string 型异常, 请捕获
*/
std::string b64encodestring(const std::string &strString)
{
int nByteSrc = strString.length();
std::string pszSource = strString; int i = ;
for(i; i < nByteSrc; i++)
if( pszSource[i] < || pszSource[i] > )
throw "can not encode Non-ASCII characters"; const char *enkey = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
std::string pszEncode(nByteSrc*/ + , '\0');
int nLoop = nByteSrc % == ? nByteSrc : nByteSrc - ;
int n = ;
for(i=; i < nLoop; i+= )
{
pszEncode[n] = enkey[pszSource[i]>>];
pszEncode[n+] = enkey[((pszSource[i]&)<<) | ((pszSource[i+] & 0xF0)>>)];
pszEncode[n+] = enkey[((pszSource[i+] & 0x0f)<<) | ((pszSource[i+] & 0xc0 )>>)];
pszEncode[n+] = enkey[pszSource[i+] & 0x3F];
n += ;
} switch(nByteSrc%)
{
case :
pszEncode[n] = '\0';
break; case :
pszEncode[n] = enkey[pszSource[i]>>];
pszEncode[n+] = enkey[((pszSource[i]&)<<) | ((&0xf0)>>)];
pszEncode[n+] = '=';
pszEncode[n+] = '=';
pszEncode[n+] = '\0';
break; case :
pszEncode[n] = enkey[pszSource[i]>>];
pszEncode[n+] = enkey[((pszSource[i]&)<<) | ((pszSource[i+]&0xf0)>>)];
pszEncode[n+] = enkey[(( pszSource[i+]&0xf)<< ) | ((&0xc0)>>)];
pszEncode[n+] = '=';
pszEncode[n+] = '\0';
break;
} return pszEncode.c_str();
} /**
* @brief 对 base64 编码后的字符串进行解码
*
* @param strString 待解码的字符串
*
* @return std::string 返回解码后的字符串
*
* @note 对于非base64编码的字符串或已损坏的base64字符串进行解码会抛出 std::string 型异常, 请捕获
*/
std::string b64decodestring(const std::string &strString)
{
int nByteSrc = strString.length();
std::string pszSource = strString; const int dekey[] = {
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
, // '+'
-, -, -,
, // '/'
, , , , , , , , , , // '0'-'9'
-, -, -, -, -, -, -,
, , , , , , , , , , , , ,
, , , , , , , , , , , , , // 'A'-'Z'
-, -, -, -, -, -,
, , , , , , , , , , , , ,
, , , , , , , , , , , , , // 'a'-'z'
}; if(nByteSrc% != )
throw "bad base64 string"; std::string pszDecode(nByteSrc*/+, '\0');
int nLoop = pszSource[nByteSrc-] == '=' ? nByteSrc - : nByteSrc;
int b[];
int i = , n = ;
for(i = ; i < nLoop; i += )
{
b[] = dekey[pszSource[i]]; b[] = dekey[pszSource[i+]];
b[] = dekey[pszSource[i+]]; b[] = dekey[pszSource[i+]];
if(b[] == - || b[] == - || b[] == - || b[] == -)
throw "bad base64 string"; pszDecode[n] = (b[] << ) | ((b[] & 0x30) >> );
pszDecode[n+] = ((b[] & 0xf) << ) | ((b[] & 0x3c) >> );
pszDecode[n+] = ((b[] & 0x3) << ) | b[]; n+=;
} if( pszSource[nByteSrc-] == '=' && pszSource[nByteSrc-] == '=' )
{
b[] = dekey[pszSource[i]]; b[] = dekey[pszSource[i+]];
if(b[] == - || b[] == -)
throw "bad base64 string"; pszDecode[n] = (b[] << ) | ((b[] & 0x30) >> );
pszDecode[n+] = '\0';
} if( pszSource[nByteSrc-] == '=' && pszSource[nByteSrc-] != '=' )
{
b[] = dekey[pszSource[i]]; b[] = dekey[pszSource[i+]];
b[] = dekey[pszSource[i+]];
if(b[] == - || b[] == - || b[] == -)
throw "bad base64 string"; pszDecode[n] = (b[] << ) | ((b[] & 0x30) >> );
pszDecode[n+] = ((b[] & 0xf) << ) | ((b[] & 0x3c) >> );
pszDecode[n+] = '\0';
} if( pszSource[nByteSrc-] != '=' && pszSource[nByteSrc-] != '=' )
pszDecode[n] = '\0'; return pszDecode;
} //测试
int main()
{
///编码测试
std::string str1 = "Hello, world!";
std::cout << "对Hello, world!进行base64编码: " << b64encodestring(str1) << std::endl; ///解码测试
std::string str2 = "SGVsbG8sIHdvcmxkIQ==";
std::cout << "对SGVsbG8sIHdvcmxkIQ==进行base64解码: " << b64decodestring(str2) << std::endl; ///编码耗时测试
std::string str3(, 'A'); //生成 10000000 长的字符串
std::cout << std::endl << "对 10000000 长的字符串进行编码耗时测试.." << std::endl;
size_t t0 = clock(); //编码计时开始
b64encodestring(str3);
std::cout << "测试结束, 耗时 " << clock() - t0 << "ms" << std::endl; ///解码耗时测试
std::string str4 = b64encodestring(str3); //得到长度为 10000000 的字符串base64编码后的字符串
std::cout << std::endl << "对 " << str4.length() << " 长的base64字符串进行解码耗时测试.." << std::endl;
size_t t1 = clock(); //解码计时开始
b64decodestring(str3);
std::cout << "测试结束, 耗时 " << clock() - t1 << "ms" << std::endl; return ;
}
运行测试结果:
若代码存在 bug 或程序缺陷, 请留言反馈, 谢谢。