将昨天的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. android 随手记 倒计时

    class CountDownUtils extends CountDownTimer { public CountDownUtils(long millisInFuture, long countD ...

  2. Doctor NiGONiGO’s multi-core CPU(最小费用最大流模板)

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=693 题意:有一个 k 核的处理器和 n 个工作,全部的工作都须要在一个核上处理一个单位的 ...

  3. Delphi Memo的记事本功能

    Delphi Memo的记事本功能           下载地址 : http://download.csdn.net/detail/teststudio/6412883 这个代码实现了Windows ...

  4. 基于XMPP实现的Openfire的配置安装+Android客户端的实现

    最近在整理一些这方面的资料,闲话少说,咱还是直奔主题吧 :) 一.基于xmpp实现的openfire的配置安装 1. 下载最新的openfire安装文件 官方下载站点: http://www.igni ...

  5. MVC ASPX(webForm)视图引擎 &lt;%:%&gt; 与&lt;%=%&gt;的差别

    控制器 using System; using System.Collections.Generic; using System.Linq; using System.Web; using Syste ...

  6. spring mvc DispatcherServlet详解之interceptor和filter的区别

    首先我们看一下spring mvc Interceptor的功能及实现: http://wenku.baidu.com/link?url=Mw3GaUhCRMhUFjU8iIDhObQpDcbmmRy ...

  7. Eclipse下使用Fat Jar插件对源代码进行打包

    这两天需要对一个项目进行打包,并在服务器上部署成后台服务模式进行执行,原来使用eclipse进行打包很难用,配置文件容易出错,生成的jar不能正常运行.后来发现Fat Jar Eclipse Plug ...

  8. Linux系统下Memcached的安装以及自启动

    一.准备工作: 1.下载libevent:http://monkey.org/~provos/libevent/ (由于memcached与客户端的通信是借助libevent来实现的,所以此动作必须在 ...

  9. 9.22 noip模拟试题

    水灾(sliker.cpp/c/pas) 1000MS  64MB 大雨应经下了几天雨,却还是没有停的样子.土豪CCY刚从外地赚完1e元回来,知道不久除了自己别墅,其他的地方都将会被洪水淹没. CCY ...

  10. ASP 调用dll(VB)及封装dll实例

    ASP调用dll及封装dll实例,封装为dll可以提供运行效率,加密代码. 打开VB6,新建ActiveX DLL 2.在工程引用中加入Microsoft Active Server Pages Ob ...