C++实现base64编码(1)
下面的代码是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)的更多相关文章
- URL安全的Base64编码
Base64编码可用于在HTTP环境下传递较长的标识信息.在其他应用程序中,也常常需要把二进制数据编码为适合放在URL(包括隐藏表单域)中的形式.此时,采用Base64编码不仅比较简短,同时也具有不可 ...
- Base64编码
Base64编码 写在前面 今天在做一个Android app时遇到了一个问题:Android端采用ASE对称加密的数据在JavaWeb(jre1.8.0_7)后台解密时,居然解密失败了!经过测试后发 ...
- Android数据加密之Base64编码算法
前言: 前面学习总结了平时开发中遇见的各种数据加密方式,最终都会对加密后的二进制数据进行Base64编码,起到一种二次加密的效果,其实呢Base64从严格意义上来说的话不是一种加密算法,而是一种编码算 ...
- 网络安全——Base64编码、MD5、SHA1-SHA512、HMAC(SHA1-SHA512)哈希
据说今天520是个好日子,为什么我想起的是502.500.404这些?还好服务器没事! 一.Base64编码 Base64编码要求把3个8位字节(3*8=24)转化为4个6位的字节(4*6=24),之 ...
- Base64编码【转】
转http://www.cnblogs.com/luguo3000/p/3940197.html 开发者对Base64编码肯定很熟悉,是否对它有很清晰的认识就不一定了.实际上Base64已经简单到不能 ...
- 【前端攻略】:玩转图片Base64编码
引言 图片处理在前端工作中可谓占据了很重要的一壁江山.而图片的 base64 编码可能相对一些人而言比较陌生,本文不是从纯技术的角度去讨论图片的 base64 编码.标题略大,不过只是希望通过一些浅显 ...
- 为什么要用base64编码
1.需求 了解为什么要使用base64对数据编码 2.理由 因为传输二进制数据的时候,网络中间的有些路由会把ascii码中的不可见字符删了,导致数据不一致.一般也会对url进行base64编码 Whe ...
- 图片Base64编码
我们经常在做Jquery插件的时候需要插入一些自定义的控件,比如说按钮,而我们自己又觉着button标签很丑,而且不同浏览器显示的效果还不一样,这个时候我们需要用到图片,当然,我们可以通过img标签添 ...
- 浅析用Base64编码的图片优化网页加载速度
想必大家都知道网页加载的过程,从开始请求,到加载页面,开始解析和显示网页,遇到图片就再次向服务器发送请求,加载图片.如果图片很多的话,就会产生大量的http请求,从而影响页面的加载速度.所以现在有一种 ...
- Base64编码原理分析
Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,在了解Base64编码之前,先了解几个基本概念:位.字节. 位:"位(bit)"是计算机中最小的数据单位.每一位 ...
随机推荐
- hdoj 2647 Reward【反向拓扑排序】
Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- C# 调试
1.监视窗口
- K倍动态减法游戏
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2580 #include <iostream> #include <string.h> ...
- [RxJS] What RxJS operators are
We have covered the basics of what is Observable.create, and other creation functions. Now lets fina ...
- 执行游戏时出现0xc000007b错误的解决方法
如图,这个错误使无数玩家烦恼. 出现这个错误,可能是硬件的问题,也可能是软件的问题.可是,因为硬件引起该问题的概率非常小,而且除了更换硬件之外没有更好的解决方法,因此本文将具体介绍怎样通过软件解决此问 ...
- WCF - 契约
契约就是双方或多方就某个问题达成的一种的共识 服务提供者通过契约的形式将服务公布出来 服务消费者根据契约来消费 这样通过契约这个中间者就可以规范服务提供的内容 而服务消费者也可以根据契约来使用服务端 ...
- 运行yum报错:Error: Cannot retrieve metalink for repository: epel. Please verify its path
Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again 当我们安装第三方扩 ...
- int? 参数是这个的时候 是可以传入null的 而int的就不行
such as pager.CurrentPageIndex = (page != null ? (int)page : 1);
- python 学习笔记(二)两种方式实现第一个python程序
在交互模式下: 如果要让Python打印出指定的文字,可以用print语句,然后把希望打印的文字用单引号或者双引号括起来,但不能混用单引号和双引号: >>> print 'hello ...
- 关于WINDOWS命令
1. Windows netstat 查看端口.进程占用 netstat -aon — 显示全部进程 2. 查看进程命令 tasklist — 显示全部进程 taskkill — 关闭至少一个系统进 ...