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 编码 解码的更多相关文章

  1. OpenSSL 使用 base64 编码/解码

    简述 关于 OpenSSL 的介绍及安装请参见:Windows下编译OpenSSL 下面主要介绍有关 OpenSSL 使用 base64 编码/解码. 简述 编码解码 更多参考 编码/解码 #incl ...

  2. 利用openssl进行BASE64编码解码、md5/sha1摘要、AES/DES3加密解密

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  3. Javascript中Base64编码解码的使用实例

    Javascript为我们提供了一个简单的方法来实现字符串的Base64编码和解码,分别是window.btoa()函数和window.atob()函数. 1 var encodedStr = win ...

  4. Atitit. 二进制数据ascii表示法,与base64编码解码api 设计标准化总结java php c#.net

    Atitit. 二进制数据ascii表示法,与base64编码解码api 设计标准化总结java php c#.net 1. Base64编码, 1 1.1. 子模式 urlsafe Or  url  ...

  5. 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 ...

  6. delphi Base64编码/解码及数据压缩/解压知识

    一.Base64编码/解码 一般用到的是Delphi自带的单元EncdDecd,当然还有第三方提供的单元或控件,其中我所接触到的认为比较好的有Indy的TIdMimeEncode / TIdMimeD ...

  7. Code:Base64 编码/解码

    ylbtech-Code:Base64 编码/解码 1. C#返回顶部 1.编码 byte[] inArray = new byte[msgTxt.Length]; int x; ; x < m ...

  8. 原来浏览器原生支持JS Base64编码解码 outside of the Latin1 range

    原来浏览器原生支持JS Base64编码解码 « 张鑫旭-鑫空间-鑫生活 https://www.zhangxinxu.com/wordpress/2018/08/js-base64-atob-bto ...

  9. 王小胖之 Base64编码/解码

    使用场景:编码网址作为URL参数,简单编码或加密数据,下载地址生成或解析. 实现功能:BASE64在线编码和解码. 数据实例:王小胖好啊,王小胖顶呱呱!! ~~ english 123 !@#$%^& ...

  10. Delphi Base64编码/解码及ZLib压缩/解压

    最近在写的程序与SOAP相关,所以用到了一些Base64编码/解码及数据压缩/解压方面的知识. 在这里来作一些总结:   一.Base64编码/解码   一般用到的是Delphi自带的单元EncdDe ...

随机推荐

  1. Mongodb数据导出工具mongoexport和导入工具mongoimport介绍

    一.导出工具mongoexport Mongodb中的mongoexport工具可以把一个collection导出成JSON格式或CSV格式的文件.可以通过参数指定导出的数据项,也可以根据指定的条件导 ...

  2. Python学习笔记(三)数据类型

    在内存中存储的数据可以有多种类型,在Python中,能够直接处理的数据类型有以下几种: 数字(Numbers) 字符串(String) 列表(List) 元组(Tuple) 字典(Dictionary ...

  3. MySQL5.7.10免安装版配置

     最新版的 Mysql 不提供图形界面的安装了, 下载下来是一个压缩包的形式, 那么我们如何来使用它呢, 让它为我们工作呢? 环境: mysql-5.7.10-winx64 + win7(64位) 一 ...

  4. gcc相关

    linux操作系统上面开发程序, 光有了gcc 是不行的 它还需要一个   build-essential软件包作用是提供编译程序必须软件包的列表信息 也就是说 编译程序有了这个软件包它才知道 头文件 ...

  5. js判断图片是否加载成功

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 用定时器令P0(或其它IO口)产生多路方波

    void Timer0_isr(void) interrupt 1 using 1{ static unsigned char i;  //重新赋值 12M晶振计算,指令周期1uS,500x2=1mS ...

  7. Nop源码分析一

    从Global.asax文件开始逐层分析Nop的架构. Application_Start()方法作为mvc启动的第一个方法. 1,首先初始化一个引擎上下文,如下面的代码: EngineContext ...

  8. Android自定义ImageView实现图片圆形 ,椭圆和矩形圆角显示

    Android中的ImageView只能显示矩形的图片,为了用户体验更多,Android实现圆角矩形,圆形或者椭圆等图形,一般通过自定义ImageView来实现,首先获取到图片的Bitmap,然后通过 ...

  9. 第1章 C#类型基础

    1.1值类型和引用类型 1.1.1 值类型 使用值类型之前需要对值类型的所有元素初始化(普通值类型和结构体). 结构还有一个特性:调用结构上的方法前,需要对其所有的字段进行赋值,为了避免对结构体中所有 ...

  10. mysql操作--高级

    1.视图 2.储存过程 3.SQL编程