/*
* base64.h
*
* Created on: 2013-1-15
* Author: hcmfys@163.com
*/
#ifndef _BASE_64_H_
#define _BASE_64_H_
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
using namespace std;
#ifndef OUT
#define OUT
#endif
#ifndef IN
#define IN
#endif
class Base64
{
public:
Base64()
{
base_table ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
}
int base64_decode(IN const char*code,OUT string &result)
{
int len=strlen(code);
int size=len /4;
if(len % 4 !=0)
{
cout<<"error data format\n";
return -1;
}
int alloc_size=len /4 *3;
char *buffer=new char[alloc_size+1];
memset(buffer,0,alloc_size+1);
int q=0;
for(int i=0; i<size; i++)
{
unsigned char p0=(unsigned char)code[4*i];
unsigned char p1=(unsigned char)code[4*i+1];
unsigned char p2=(unsigned char)code[4*i+2];
unsigned char p3=(unsigned char)code[4*i+3];
int s1=get_index(p0);
if(s1==-1)
{
cout<<"error data format\n";
return -1;
}
int s2=get_index(p1);
if(s2==-1)
{
cout<<"error data format\n";
return -1;
}
int s3=-2;
int s4=-2;
if(p2 =='=')
{
if(p3 != '=')
{
cout<<"error data format\n";
return -1;
}
s3=-2;
}
else
{
s3=get_index(p2);
if(s3==-1)
{
cout<<"error data format\n";
return -1;
}
}
if(p3=='=')
{
s4=-2;
}
else
{
s4=get_index(p3);
if(s4==-1)
{
cout<<"error data format\n";
return -1;
}
}
if(s3==-2)
{
s2=s2 >>4;
buffer[q]=(s1 << 2) | s2;
}
else
{
//前面前6位和中间2位
buffer[q]=( s1<< 2 ) | ( s2 >> 4 );
q++;
if(s4==-2)
{
s3=s3 >>2;
int low= (s2 & 0xf ) << 4;
buffer[q]= low | s3;
}
else
{
//前面前4位和中间4位
int low=(s2 & 0xf)<< 4 ;
int high=s3 >> 2;
buffer[q]= low | high;
q++;
//中间后2位和最后6位
low=(s3 & 0x03) << 6;
buffer[q]=low | s4;
q++;
}
}
}
result=buffer;
alloc_size=strlen(buffer);
delete[] buffer;
return alloc_size;
}
int base64_encode(IN const char* code, OUT string &result)
{
int len = strlen(code);
int size = len / 3;
int allocat_size=size*4 ;
int index = 0;
for (int i = 0; i < size; i++)
{
//前6位
unsigned char p0=(unsigned char)code[index];
unsigned char p1=(unsigned char)code[index+1];
unsigned char p2=(unsigned char)code[index+2];
int cur_index = p0>> 2;
result += base_table[cur_index];
//前2位中间4位
cur_index = ((p0 & 0x03) << 4) | (p1>> 4);
result += base_table[cur_index];
//中间后4位和最后前2位
cur_index = ((p1 & 0xf) << 2) | (p2 >> 6);
result += base_table[cur_index];
//最后6位
cur_index = (p2 & 0x3f);
result += base_table[cur_index];
index += 3;
}
size = 3-len * 4 % 3 ;
switch(size)
{
case 1:
{
unsigned char p0=(unsigned char)code[index];
unsigned char p1=(unsigned char)code[index+1];
//前6位
int cur_index = (p0 >> 2 & 0x3f);
result += base_table[cur_index];
//前后2位和中间4位
int high = (p0 & 0x03) << 4;
int low = p1 >> 4 & 0x0f;
cur_index = high | low;
result += base_table[cur_index];
//最后4位补1个 00 位
high = p1<< 4 & 0xf0;
high=high>>2;
cur_index=high;
result += base_table[cur_index];
//填充一个 = 号
result += "=";
allocat_size+=4;
}
break;
case 2:
{
unsigned char p0=(unsigned char)code[index];
//前6位
int cur_index = (p0>> 2 & 0x3f);
result += base_table[cur_index];
//前4位补充 2个 00 位
cur_index = p0 & 0x03;
cur_index = cur_index << 4;
result += base_table[cur_index];
result += "=";
result += "=";
allocat_size+=4;
}
break;
default:
break;
}
return allocat_size;
}
private:
char *base_table ;
int get_index(char l)
{
for(int i=0; i<64; i++)
{
if(base_table[i]==l)
return i;
}
return -1;
}
};
#endif
分享到:
相关推荐
C/C++ Base64编解码开源库,第三方开源库,亲测可用;
c++ base64 编码
c++ base64模块
C++ Base 64 编码解码Vs2008 程序
Base64编解码库(源码)--C++,一个老外写的。
c++实现base64算法加密解密,不需要修改,直接运行即可
C++封装的BASE64编码工具类。用于网络通讯中解码和编码。
Base64.dll 适用VC的BASE64 编码解码
图片转为base64编码,base64编码保存为图片。代码内容借鉴了网上的转换类。图片转为base64编码,base64编码保存为图片。代码内容借鉴了网上的转换类。
C++ STL编写的BASE64编解码源码 代码简洁易读 非常好用
base64编码解码,vc6,主要用了base64编码解码的类
c++图片与base64互转 代码直观可用,已经测试,可直接调用,需要用到OpenCV读取图片
base64编码和解码c++代码,从live555源码中移植并修改过来的
C++base64编码源代码+2小时教学视频百度云下载地址.
在前人的基础上增加了json解析封装得到base64或反转成文件。包括案例,源代码。 调用简单,不错的学习案例。 std::unique_ptr*>p(new const char*(readfile64("{\"filename\":\"D:\\\\job\\\\greatwall\\\\test\\\\1...
base64编码和解码,c\c++类 编译环境vs2008 希望对大家有用!!
在网上找了很久没有合适的,自己写了一个,留给有需要的人用。
Base64编码解码
C语言版的实现HMAC-SHA1和base64编码,已经对C++做了兼容处理,在VS下运行main.c代码,可以得到经过HMAC-SHA1处理后的结果,并且可以运行里面的base64编码函数得到想要的结果,可以用于连接阿里云MQTT
Base64编解码 C++实现 已经测试过可以用 Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,Base64就是一种基于64个可打印字符来表示二进制数据的方法