将昨天的php代码改造成C++

/*base_64.h文件*/
#ifndef BASE_64_H
#define BASE_64_H
/**
* Base64 编码/解码
* @author liruixing
*/
class Base64{
private:
std::string _base64_table;
static const char base64_pad = '=';public:
Base64()
{
_base64_table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; /*这是Base64编码使用的标准字典*/
}
/**
* 这里必须是unsigned类型,否则编码中文的时候出错
*/
std::string Encode(const unsigned char * str,int bytes);
std::string Decode(const char *str,int bytes);
void Debug(bool open = true);
};
#endif

上面定义了一个头文件,定义base64的类

/*base_64.cpp文件*/
#include <iostream>
#include <string>
#include <cstring>
#include "base_64.h" std::string Base64::Encode(const unsigned char * str,int bytes) {
int num = ,bin = ,i;
std::string _encode_result;
const unsigned char * current;
current = str;
while(bytes > ) {
_encode_result += _base64_table[current[] >> ];
_encode_result += _base64_table[((current[] & 0x03) << ) + (current[] >> )];
_encode_result += _base64_table[((current[] & 0x0f) << ) + (current[] >> )];
_encode_result += _base64_table[current[] & 0x3f]; current += ;
bytes -= ;
}
if(bytes > )
{
_encode_result += _base64_table[current[] >> ];
if(bytes% == ) {
_encode_result += _base64_table[(current[] & 0x03) << ];
_encode_result += "==";
} else if(bytes% == ) {
_encode_result += _base64_table[((current[] & 0x03) << ) + (current[] >> )];
_encode_result += _base64_table[(current[] & 0x0f) << ];
_encode_result += "=";
}
}
return _encode_result;
}
std::string Base64::Decode(const char *str,int length) {
//解码表
const char DecodeTable[] =
{
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, , -, -, -, ,
, , , , , , , , , , -, -, -, -, -, -,
-, , , , , , , , , , , , , , , ,
, , , , , , , , , , , -, -, -, -, -,
-, , , , , , , , , , , , , , , ,
, , , , , , , , , , , -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -
};
int bin = ,i=,pos=;
std::string _decode_result;
const char *current = str;
char ch;
while( (ch = *current++) != '\0' && length-- > )
{
if (ch == base64_pad) { // 当前一个字符是“=”号
/*
先说明一个概念:在解码时,4个字符为一组进行一轮字符匹配。
两个条件:
1、如果某一轮匹配的第二个是“=”且第三个字符不是“=”,说明这个带解析字符串不合法,直接返回空
2、如果当前“=”不是第二个字符,且后面的字符只包含空白符,则说明这个这个条件合法,可以继续。
*/
if (*current != '=' && (i % ) == ) {
return NULL;
}
continue;
}
ch = DecodeTable[ch];
//这个很重要,用来过滤所有不合法的字符
if (ch < ) { /* a space or some other separator character, we simply skip over */
continue;
}
switch(i % )
{
case :
bin = ch << ;
break;
case :
bin |= ch >> ;
_decode_result += bin;
bin = ( ch & 0x0f ) << ;
break;
case :
bin |= ch >> ;
_decode_result += bin;
bin = ( ch & 0x03 ) << ;
break;
case :
bin |= ch;
_decode_result += bin;
break;
}
i++;
}
return _decode_result;
}

base64类中方法的定义实际上是在base_64.cpp中进行的。

上面的两个文件用来生成一个静态链接库:libbase_64.a

g++ -c base_64.cpp
ar rs libbase_64.a base_64.o

下面来进行实际的测试:

/*main.cppe文件*/
#include <iostream>
#include "base_64.h" using namespace std;
int main()
{
unsigned char str[] = "这是一条测试数据:http://img.v.cmcm.com/7dd6e6d7-0c2d-5a58-a072-71f828b94cbc_crop_216x150.jpg";
string normal,encoded;
int i,len = sizeof(str)-;
Base64 *base = new Base64();
encoded = base->Encode(str,len);
cout << "base64 encode : " << encoded << endl;
len = encoded.length();
const char * str2 = encoded.c_str();
normal = base->Decode(str2,len);
cout << "base64 decode : " << normal <<endl;
return ;
}

编译代码并运行

g++ main.cpp -L. -lbase_64 -Ibase_64 -o main
./main

正常数据的encode和decode输出效果:

包含异常数据的decode输出效果:

在改造昨天的php代码过程中,还参考php源码base64.c中相关逻辑的实现。

C++实现base64编码的更多相关文章

  1. URL安全的Base64编码

    Base64编码可用于在HTTP环境下传递较长的标识信息.在其他应用程序中,也常常需要把二进制数据编码为适合放在URL(包括隐藏表单域)中的形式.此时,采用Base64编码不仅比较简短,同时也具有不可 ...

  2. Base64编码

    Base64编码 写在前面 今天在做一个Android app时遇到了一个问题:Android端采用ASE对称加密的数据在JavaWeb(jre1.8.0_7)后台解密时,居然解密失败了!经过测试后发 ...

  3. Android数据加密之Base64编码算法

    前言: 前面学习总结了平时开发中遇见的各种数据加密方式,最终都会对加密后的二进制数据进行Base64编码,起到一种二次加密的效果,其实呢Base64从严格意义上来说的话不是一种加密算法,而是一种编码算 ...

  4. 网络安全——Base64编码、MD5、SHA1-SHA512、HMAC(SHA1-SHA512)哈希

    据说今天520是个好日子,为什么我想起的是502.500.404这些?还好服务器没事! 一.Base64编码 Base64编码要求把3个8位字节(3*8=24)转化为4个6位的字节(4*6=24),之 ...

  5. Base64编码【转】

    转http://www.cnblogs.com/luguo3000/p/3940197.html 开发者对Base64编码肯定很熟悉,是否对它有很清晰的认识就不一定了.实际上Base64已经简单到不能 ...

  6. 【前端攻略】:玩转图片Base64编码

    引言 图片处理在前端工作中可谓占据了很重要的一壁江山.而图片的 base64 编码可能相对一些人而言比较陌生,本文不是从纯技术的角度去讨论图片的 base64 编码.标题略大,不过只是希望通过一些浅显 ...

  7. 为什么要用base64编码

    1.需求 了解为什么要使用base64对数据编码 2.理由 因为传输二进制数据的时候,网络中间的有些路由会把ascii码中的不可见字符删了,导致数据不一致.一般也会对url进行base64编码 Whe ...

  8. 图片Base64编码

    我们经常在做Jquery插件的时候需要插入一些自定义的控件,比如说按钮,而我们自己又觉着button标签很丑,而且不同浏览器显示的效果还不一样,这个时候我们需要用到图片,当然,我们可以通过img标签添 ...

  9. 浅析用Base64编码的图片优化网页加载速度

    想必大家都知道网页加载的过程,从开始请求,到加载页面,开始解析和显示网页,遇到图片就再次向服务器发送请求,加载图片.如果图片很多的话,就会产生大量的http请求,从而影响页面的加载速度.所以现在有一种 ...

  10. Base64编码原理分析

    Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,在了解Base64编码之前,先了解几个基本概念:位.字节. 位:"位(bit)"是计算机中最小的数据单位.每一位 ...

随机推荐

  1. firefox如何restart重启

    1.开发者工具栏可以对firefox进行重启,不是关闭firefox又打开那种重启

  2. web项目学习之spring-security

    转自<http://liukai.iteye.com/blog/982088> spring security功能点总结: 1. 登录控制 2. 权限控制(用户菜单的显示,功能点访问控制) ...

  3. winfrom 截屏、抓屏 分类: WinForm 2014-08-01 13:02 198人阅读 评论(0) 收藏

    截取全屏代码: try { this.Hide(); Rectangle bounds = Screen.GetBounds(Screen.GetBounds(Point.Empty)); Bitma ...

  4. Python获取当前时间 分类: python 2014-11-08 19:02 132人阅读 评论(0) 收藏

    Python有专门的time模块可以供调用. <span style="font-size:14px;">import time print time.time()&l ...

  5. 开源 免费 java CMS - FreeCMS2.1 会员站内信

    项目地址:http://www.freeteam.cn/ 站内信 1.1.1 写信 从左側管理菜单点击写信进入. 输入收信人.标题.内容后点击发送button. 1.1.2 收件箱 从左側管理菜单点击 ...

  6. java中调用js脚本

    JDK1.6加入了对Script(JSR223)的支持.这是一个脚本框架,提供了让脚本语言来访问Java内部的方法.你可以在运行的时候找到脚本引擎,然后调用这个引擎去执行脚本.这个脚本API允许你为脚 ...

  7. 使用SqlAlchemy时如何方便的取得dict数据、dumps成Json

    使用Sqlalchemy可以方便的从数据库读取出python对象形式的数据(吐槽:说实话对象形式也没多方便,还不如我之前从关系型数据库直接读取出dict形式的数据用起来方便,具体参见我以前的文章htt ...

  8. div宽度设置无效问题解决

    问题描述: 要设置两个div在同一行显示,都加入了display:inline样式,但是其中一个div的宽度设置无效,在浏览器显示它的宽度始终是1003px. 解决办法: 方法1/给div加入样式:f ...

  9. Java-strurs总结

    这里是自己对自学的struts2 的一个整体 的脉络进行的一个概括,需要学习哪些东西,注重哪些东西: struts2 是主流框架SSH 中的一个"S" ,准备MVC开发标准的一个框 ...

  10. IIS7.5 asp+access数据库连接失败处理 64位系统

    IIS7.5 asp+access数据库连接失败处理(SRV 2008R2 x64/win7 x64) IIS7.5不支持oledb4.0驱动?把IIS运行模式设置成32位就可以了,微软没有支持出64 ...