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)"是计算机中最小的数据单位.每一位 ...
随机推荐
- angularjs post 跨域
web api搞好了:用Ajax妥妥的:但是前端用的AngulagJS,也懒得再换为Ajax了: 但是问题来了:提示: 已拦截跨源请求:同源策略禁止读取位于 http://x.x.x.x:port/a ...
- 第一贱-UILabel
UILabel *label = [[UILabel alloc]init]; label.frame = CGRectMake(100, 100, 100, 100); label.text = @ ...
- COMPACT 行记录格式
CREATE TABLE `mytest` ( `t1` varchar() DEFAULT NULL, `t2` varchar() DEFAULT NULL, `t3` ) DEFAULT NUL ...
- (转载)linux那点事儿(中)
原文地址:http://www.cnblogs.com/fnng/archive/2012/03/19/2407162.html 本文只是转载供自己学习之用 2012-03-22 13:31 by 虫 ...
- javascript中的简单三角函数
- Android 开发实践 ViewGroup 实现左右滑出窗口(一)
利用假期把以前做的东西总结整理一下,先从简单的开始吧.实现的效果是这样的: 做了个截屏动画,比例有点不对了,凑合着看吧. 整个窗口有3部分组成,中间的主界面是个列表,左边的滑出界面是个菜单,右边的 ...
- Linux chmod
在Linux中要修改一个文件夹或文件的权限我们需要用到linux chmod命令来做. 语法如下: chmod [who] [+ | - | =] [mode] 文件名 命令中各选项的含义为 u 表示 ...
- oracle中的层级递归查询操作
oracle中的层级操作非常方便,在使用之后爱不释手,以前要实现该种数据查询操作,需要非常复杂的实现过程.在oracle中通过connect by可以实现前面的目的,通常情况下层级查询基本都能实现递归 ...
- oracle周数计算方法
从星期天开始起算 Function Fn_GetWeekbyDate(P_Date Varchar2) Return Varchar2 Is Begin Return To_char(To_Date( ...
- HTML特效代码大全
1)贴图:<img src="图片地址">2)加入连接:<a href="所要连接的相关地址">写上你想写的字</a>1)贴 ...