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)"是计算机中最小的数据单位.每一位 ...
随机推荐
- CMS收集器
CMS收集周期 CMS并非没有暂停,而是用两次短暂停来替代串行标记整理算法的长暂停,它的收集周期是这样:初始标记(CMS-initial-mark) -> 并发标记(CMS-concurrent ...
- C —— 零碎笔记
1.字节对齐和结构体大小 链接 2.共同体union 的作用 链接 3.文件夹和文件操作 windows: http://blog.csdn.net/gneveek/article/details/6 ...
- AIX操作oracle
oracle:出现下述错误,无法连接用户. ORA-01034: ORACLE not available ORA-27101: shared memory realm does not exist ...
- python google play
#!/usr/env python #-*- coding: utf-8 -*- import urllib import urllib2 import random import requests ...
- Android Layout布局文件里的android:layout_height等属性不起作用
有的时候,我们配置好的布局文件,在加载完成添加到我们的Activity中后发现,并没有安装我们设置的属性 来布局,比为我们设置了android:layout_marginTop="100di ...
- 使用WinINet和WinHTTP实现Http訪问
使用WinINet和WinHTTP实现Http訪问 飘飘白云 l_zhaohui@163.com 2007-11-30 Http訪问有两种方式,GET和POST,就编程来说GET方式相对简单点,它不用 ...
- 基于NPOI开源框架写的ExcelHelper【转载】
namespace ExcelTest { using System; using System.Collections.Generic; using System.Data; using Syste ...
- docker 服务注册
docker 服务注册 etcd docker run -d --name etcd -p 4001:4001 -p 7001:7001 elcolio/etcd
- viewController启动方法分析
viewController启动方法分析 转载:http://blog.csdn.net/dizzthxl/article/details/14170047 首先理清楚一个概念: 创建一个类和实例化一 ...
- Javascript 函数和模块定义
匿名函数 // calculator.js(function(root) { var calculator = { sum: function(a, b) { return a + b; } ...