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 ...
随机推荐
- 第一零二天上课 PHP TP框架 引入文件路径问题和调用验证码的方式
外部文件引入到视图模板的方式 1,将外部文件放在Public文件夹下,用load标签引入 2,在模板出书写引入代码(方法有很多,只有以下方法不容易出问题) <load h ...
- avalon2学习教程15指令总结
avalon的指令在上一节已经全部介绍完毕,当然有的语焉不详,如ms-js.本节主要总结我对这方面的思考与探索. MVVM的成功很大一语分是来自于其指令,或叫绑定.让操作视图的功能交由形形式式的指令来 ...
- UVM Top Testbench
top testbench在top_tb中包含进所有的文件,将DUT放在top_tb中(例化DUT),连接好各个端口,提供clk时钟和rst_n复位信号.最主要的是要给组件中的虚接口设置接口,一般是给 ...
- 如何用expdp、impdp按表空间导出、导入?
参考:http://blog.csdn.net/zftang/article/details/6387325 A数据库: 表空间:ylcois 用户名:ylcois 密码:ylcois B数据库: 表 ...
- php 万能加密
function fue($hash,$times) { // Execute the encryption(s) as many times as the user wants for($i=$ti ...
- 【BZOJ1226】学校食堂Dining(状压DP)
题意:见题面 思路:设dp[i,sta,k]为前i个人已经吃完,从第i人到第i+b[i]人的吃饭状况是sta,前一个吃完的人离i的距离是k(可能为负)的最小值 \[ dp[i+1,sta>> ...
- count distinct 多个字段 或者 count(*) 统计group by 结果
SELECT COUNT(*) FROM( SELECT 列名 FROM 表名 where ( 条件 )GROUP BY 多字段)临时表名 例如: SELECT COUNT(*) FROM(SELEC ...
- android笔记:ViewPager实现界面的滑动
最近在学习ViewPager实现界面的滑动,拜读了郭神的博客文章,并抽取归纳了自己对ViewPager的理解. ViewPager实现界面滑动的步骤如下: 1.在xml布局内加入控件android.s ...
- java代码性能优化总结(转载)
原文链接:http://developer.51cto.com/art/201511/496263.htm 前言 代码优化,一个很重要的课题.可能有些人觉得没用,一些细小的地方有什么好修改的,改与不改 ...
- sql server 2000通过机器名可以连,通过ip连不上的问题
客户那边两台服务器A和B,之前一直都是好好的,今天因为换了网络环境,结果数据库之间不能相互访问了. 目前只能A访问B,B访问不了A,在服务器A上面试了,通过ip连本机,也是连接超时. 开始想着是服务器 ...