C++ Base64 编码 解码
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 或程序缺陷, 请留言反馈, 谢谢。
C++ Base64 编码 解码的更多相关文章
- OpenSSL 使用 base64 编码/解码
简述 关于 OpenSSL 的介绍及安装请参见:Windows下编译OpenSSL 下面主要介绍有关 OpenSSL 使用 base64 编码/解码. 简述 编码解码 更多参考 编码/解码 #incl ...
- 利用openssl进行BASE64编码解码、md5/sha1摘要、AES/DES3加密解密
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- Javascript中Base64编码解码的使用实例
Javascript为我们提供了一个简单的方法来实现字符串的Base64编码和解码,分别是window.btoa()函数和window.atob()函数. 1 var encodedStr = win ...
- Atitit. 二进制数据ascii表示法,与base64编码解码api 设计标准化总结java php c#.net
Atitit. 二进制数据ascii表示法,与base64编码解码api 设计标准化总结java php c#.net 1. Base64编码, 1 1.1. 子模式 urlsafe Or url ...
- Atitit. 二进制数据ascii表示法,与base64编码解码api 设计标准化总结java php c#.net
Atitit. 二进制数据ascii表示法,与base64编码解码api 设计标准化总结java php c#.net 1. Base64编码,1 1.1. 子模式 urlsafe Or url u ...
- delphi Base64编码/解码及数据压缩/解压知识
一.Base64编码/解码 一般用到的是Delphi自带的单元EncdDecd,当然还有第三方提供的单元或控件,其中我所接触到的认为比较好的有Indy的TIdMimeEncode / TIdMimeD ...
- Code:Base64 编码/解码
ylbtech-Code:Base64 编码/解码 1. C#返回顶部 1.编码 byte[] inArray = new byte[msgTxt.Length]; int x; ; x < m ...
- 原来浏览器原生支持JS Base64编码解码 outside of the Latin1 range
原来浏览器原生支持JS Base64编码解码 « 张鑫旭-鑫空间-鑫生活 https://www.zhangxinxu.com/wordpress/2018/08/js-base64-atob-bto ...
- 王小胖之 Base64编码/解码
使用场景:编码网址作为URL参数,简单编码或加密数据,下载地址生成或解析. 实现功能:BASE64在线编码和解码. 数据实例:王小胖好啊,王小胖顶呱呱!! ~~ english 123 !@#$%^& ...
- Delphi Base64编码/解码及ZLib压缩/解压
最近在写的程序与SOAP相关,所以用到了一些Base64编码/解码及数据压缩/解压方面的知识. 在这里来作一些总结: 一.Base64编码/解码 一般用到的是Delphi自带的单元EncdDe ...
随机推荐
- androidstudio 查看源码
handler.postDelayed(myrunable,10000); ctrl+鼠标左键,点击postDelayed显示(不可以查看源码) ctrl+鼠标左键,点击postDelayed显示(可 ...
- Oracle Database 11G R2 标准版 企业版 下载地址(转)
转自:http://blog.itpub.net/628922/viewspace-759245/ 不需要注册,直接复制到迅雷或其他下载软件中即可下载. oracle 11.2.0.3 下载地址: L ...
- 第八十七天请假 PHP smarty模板配置以及简单的调用方式
smarty模板的配置文件 <?php define("ROOT",str_replace("\\","/",dirname(__FI ...
- Error : Must specify a primary resource (JAR or python or R file)
spark-submit 报错:must specify resource 取消关注 | 1 ... 我的submit.sh内容: /bin/spark-submit \ --class abc.pa ...
- ios block和函数的区别
block是封装了一段代码的OC对象,可以被设为Property, 在调用block的地方block都会被替换成相应的代码,相当于内联函数. 函数可以使代码更加整洁易读,使用block会使代码可读性变 ...
- TortoiseSVN文件夹及文件状态图标不显示解决方法
win8 64位系统,原本svn是好用的,安装了klive金山快盘后,svn图标都不显示了.最后通过修改注册表解决: win+R调出运行框,输入regedit,打开注册表编辑器. HKEY_LOCAL ...
- keyup keydown keypress 区别
测试的浏览器环境: chrome 版本 43.0.2357.134 mfirefox 版本 24.0IE6(绿色版 IE.exe)IE7IE8IE9 搜狗拼音输入法3.5(3.5.0.1089)网吧专 ...
- isa指针
转载自 http://www.cnblogs.com/zhangdashao/p/4438540.html 可以去这里看详细的. 每个Objective-C对象都有一个隐藏的数据结构,这个数据结构是O ...
- 深入理解js——继承
JavaScript中继承是通过原型链来体现的. function Foo(){} var f1=new Foo(); f1.a=10; Foo.prototype.a=100; Foo.protot ...
- perl中的运算符
字符计算的运算符