将昨天的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. weblogic 日志介绍

    1.server.log 该日志记录的是服务(包括admin server 和 app server)启动过程中和关闭过程中的日志,还包括部署在服务上面的应用,在运行过程中所产生的日志. server ...

  2. 什么是staging server

    原文链接:http://blog.csdn.net/blade2001/article/details/7194895 软件应用开发的经典模型有这样几个环境:开发环境(development).集成环 ...

  3. JAVA wait(), notify(),sleep详解

    转自: http://blog.csdn.net/zyplus 在JAVA中,是没有类似于PV操作.进程互斥等相关的方法的.JAVA的进程同步是通过synchronized()来实现的,需要说明的是, ...

  4. MFC——error LNK2005: "protected: static struct AFX_MSGMAP

    好久没弄VC程序了,今天弄了下,还会用公司给的窗口重绘作为基类来实现,竟然报了这个错误. 找了一下是这里: 有个窗口重绘类是基类: class CBaseDlg : public CDialog 新建 ...

  5. PNP8550(3.3V DC蜂鸣器) - 原理图系列

    一.截图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGlhb2Jpbl9ITEo4MA==/font/5a6L5L2T/fontsize/400/fi ...

  6. Android 颜色渲染(十) ComposeShader组合渲染

    版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] Android 颜色处理(十) ComposeShader组合渲染 public ComposeShader(Shader sh ...

  7. Android 颜色渲染(四) BitmapShader位图渲染

    版权声明:本文为博主原创文章,未经博主允许不得转载. Android 颜色处理(四) BitmapShader位图渲染 public   BitmapShader(Bitmap bitmap,Shad ...

  8. 写一个函数,参数为$n,生成一个数组,其元素为1~$n,各元素位置随机排列,不得重复

    function rand_array($n){ $array=range(1,$n); shuffle($array); return $array; }

  9. wpf提示背景,资源样式

    查找资源时多用element.TryFindResource() <TextBox FontSize="17" Height="26" Margin=&q ...

  10. NUll在oracle与sqlserver中使用相同与区别

    最近在使用Oracle进行开发,遇到很多与以前使用sqlserver的不同语法.今天遇到null在两种数据库上面操作上的差别,在此记录两种数据库上的差异. null 与字符串相加 1.在oracle中 ...