下面的代码是php里面的base64编码逻辑,确实比我之前的要美观很多,我只是简单的用C++的类进行了一下封装,删除了一些没用的逻辑,基本上还是原来PHP的代码:

#include <iostream>
#include <cstring>
#include <cctype>
using namespace std; class Base64{
private:
static const char base64_pad = '='; public:
unsigned char * Encode(const unsigned char *str, int length, int &ret_length);
unsigned char * Decode(const unsigned char *str, int length, int &ret_length);
}; unsigned char * Base64::Encode(const unsigned char *str, int length, int &ret_length) /* {{{ */
{
/* {{{ base64 tables */
const char base64_table[] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'', '', '', '', '', '', '', '', '', '', '+', '/', '\0'
};
const unsigned char *current = str;
unsigned char *p;
unsigned char * result; result = new unsigned char[(length+)/*];
p = result; if (length < ) {
ret_length = ;
return NULL;
} while (length > ) { /* keep going until we have less than 24 bits */
*p++ = base64_table[current[] >> ];
*p++= base64_table[((current[] & 0x03) << ) + (current[] >> )];
*p++= base64_table[((current[] & 0x0f) << ) + (current[] >> )];
*p++= base64_table[current[] & 0x3f]; current += ;
length -= ; /* we just handle 3 octets of data */
} /* now deal with the tail end of things */
if (length != ) {
*p++= base64_table[current[] >> ];
if (length > ) {
*p++= base64_table[((current[] & 0x03) << ) + (current[] >> )];
*p++= base64_table[(current[] & 0x0f) << ];
*p++= base64_pad;
} else {
*p++ = base64_table[(current[] & 0x03) << ];
*p++ = base64_pad;
*p++ = base64_pad;
}
}
ret_length = (int)(p - result);
*p = '\0';
return result;
} unsigned char * Base64::Decode(const unsigned char *str, int length, int &ret_length) /* {{{ */
{
const short base64_reverse_table[] = {
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, , -, -, -, ,
, , , , , , , , , , -, -, -, -, -, -,
-, , , , , , , , , , , , , , , ,
, , , , , , , , , , , -, -, -, -, -,
-, , , , , , , , , , , , , , , ,
, , , , , , , , , , , -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -
};
const unsigned char *current = str;
int ch, i = , j = , k;
/* this sucks for threaded environments */ unsigned char * result; result = new unsigned char[length]; /* run through the whole string, converting as we go */
while ((ch = *current++) != '\0' && length-- > ) {
if (ch == base64_pad) {
if (*current != '=' && (i % ) == ) {
return NULL;
}
continue;
}
ch = base64_reverse_table[ch];
if (ch < || ch == -) { /* a space or some other separator character, we simply skip over */
continue;
} else if (ch == -) {
return NULL;
} switch(i % ) {
case :
result[j] = ch << ;
break;
case :
result[j++] |= ch >> ;
result[j] = (ch & 0x0f) << ;
break;
case :
result[j++] |= ch >>;
result[j] = (ch & 0x03) << ;
break;
case :
result[j++] |= ch;
break;
}
i++;
cout << "result == " << result << endl;
} k = j;
/* mop things up if we ended on a boundary */
if (ch == base64_pad) {
switch(i % ) {
case :
return NULL;
case :
k++;
case :
result[k] = ;
}
}
if(ret_length) {
ret_length = j;
}
result[j] = '\0';
return result;
}
int main()
{
unsigned char str[] = " aHR0cDo vL2ltZy52LmNtY20uY29tLzdkNjZkNy0wYzJkLTVhNTgtYTA3Mi03MWY4MjhiOTRjYmNfY3JvcF8yMTZ{4MTUwLmpwZw= ======";
unsigned char *normal,*encoded;
int i,len = ,ret_len=; cout << "original string : " << str << endl;
len = sizeof(str)-;
Base64 * base64 = new Base64();
cout << "str = " << str << endl;
normal = base64->Decode(str,len,ret_len);
cout << "base64 decode : " << normal <<endl;
delete [] encoded;
delete [] normal;
return ;
}

上面的代码对php源码中的逻辑做了优化,删除了decode方法中判断结尾的“=”号时多余的逻辑,以免干扰视线。具体删除的代码参照php源码中ext/standard/base64.c

上一篇php实现base64的代码也进行了调整,提高了容错。

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

  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. dijkstra算法(迪杰斯特拉算法)

    dijkstra算法(迪杰斯特拉算法) 用途:有向图最短路径问题 定义:迪杰斯特拉算法是典型的算法,一般的表述通常有两种方式,这里均采用永久和临时标号的方式,该算法要求图中不存在负权边 用永久和临时标 ...

  2. SQL ID自增列从1开始重新排序 分类: SQL Server 2014-05-19 14:46 652人阅读 评论(0) 收藏

    数据库中把ID自增长重置成1: 一般做法:(太麻烦) 复制表数据->删除原表.新建一张表->粘贴: 新方法: 数据库中:新建查询->复制.粘贴一下代码->修改表名,执行即可(先 ...

  3. 3proxy 二级代理配置样例

    适应情况: 有时,我们的机器HOST-A只能通过代理服务器HOST-B才可以访问internet, 而与我们相连的机器HOST-C也需要访问internet, 但是HOST-C却不能直接访问HOST- ...

  4. HDU3336-Count the string(KMP)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  5. 怎样在delphi中实现控件的拖拽

    下面这2种方法都能实现对控件和窗体的拖拽 方法1 procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift ...

  6. InstallShield 12 制作安装包

    目  录 一. 二. 三. (一) 打开project... 2 (二) project助手页面... 3 1.Application Information:程序信息... 4 2.Installa ...

  7. innodb 页分配

    [root@localhost test]# python /root/soft/py_innodb_page_info.py -v t1.ibdpage offset 00000000, page ...

  8. java文件处理工具类

    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedRead ...

  9. 关于javascript dom扩展:Selector API

    众多javascript库中最常用的一项功能,就是根据css选择符选择与某个模式匹配的DOM元素.之前由于对javascript的认识较低,对javascript对DOM操作还停留在getElemen ...

  10. jquery-动态设置ul li a链接目标

    概述: 先上我的布局图: ul中的每一个li里面包含着一个'a'超链接,可以猜想到,我想没点击ul->li里面每个a,链接到不同的页面,在这里,我用iframe完成. html主要代码为: &l ...