c++ base64编解码使用示例

所属分类: 软件编程 / C 语言 阅读数: 86
收藏 0 赞 0 分享

base64码简介

Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,大家可以查看RFC2045~RFC2049,上面有MIME的详细规范。Base64编码可用于在HTTP环境下传递较长的标识信息。例如,在Java Persistence系统Hibernate中,就采用了Base64来将一个较长的唯一标识符(一般为128-bit的UUID)编码为一个字符串,用作HTTP表单和HTTP GET URL中的参数。在其他应用程序中,也常常需要把二进制数据编码为适合放在URL(包括隐藏表单域)中的形式。此时,采用Base64编码具有不可读性,即所编码的数据不会被人用肉眼所直接看到。

0. 源数据都是8位位宽的数据;
1. 相当于分组码,将源数据分为3个一组,每一组共24bits,采用每6位对应一个编码码字,那么3*8bits = 4*6its, 将3个数据映射成4个数据,由于编码的码字都是6位长度,换位10进制就是0-63,总共有64中可能性,这也是base64名字的来历;
2. 6bits对应10进制数对应的码字如最后的表;

C代码编码

#include <stdio.h>
#include <string.h>

// 全局常量定义
const char * base64char = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const char padding_char = '=';

/*编码代码
* const unsigned char * sourcedata, 源数组
* char * base64 ,码字保存
*/
int base64_encode(const unsigned char * sourcedata, char * base64)
{
 int i=0, j=0;
 unsigned char trans_index=0; // 索引是8位,但是高两位都为0
 const int datalength = strlen((const char*)sourcedata);
 for (; i < datalength; i += 3){
  // 每三个一组,进行编码
  // 要编码的数字的第一个
  trans_index = ((sourcedata[i] >> 2) & 0x3f);
  base64[j++] = base64char[(int)trans_index];
  // 第二个
  trans_index = ((sourcedata[i] << 4) & 0x30);
  if (i + 1 < datalength){
   trans_index |= ((sourcedata[i + 1] >> 4) & 0x0f);
   base64[j++] = base64char[(int)trans_index];
  }else{
   base64[j++] = base64char[(int)trans_index];

   base64[j++] = padding_char;

   base64[j++] = padding_char;

   break; // 超出总长度,可以直接break
  }
  // 第三个
  trans_index = ((sourcedata[i + 1] << 2) & 0x3c);
  if (i + 2 < datalength){ // 有的话需要编码2个
   trans_index |= ((sourcedata[i + 2] >> 6) & 0x03);
   base64[j++] = base64char[(int)trans_index];

   trans_index = sourcedata[i + 2] & 0x3f;
   base64[j++] = base64char[(int)trans_index];
  }
  else{
   base64[j++] = base64char[(int)trans_index];

   base64[j++] = padding_char;

   break;
  }
 }

 base64[j] = '\0'; 

 return 0;
}

解码

包括两个函数:

/** 在字符串中查询特定字符位置索引
* const char *str ,字符串
* char c,要查找的字符
*/
inline int num_strchr(const char *str, char c) // 
{
 const char *pindex = strchr(str, c);
 if (NULL == pindex){
  return -1;
 }
 return pindex - str;
}
/* 解码
* const char * base64 码字
* unsigned char * dedata, 解码恢复的数据
*/
int base64_decode(const char * base64, unsigned char * dedata)
{
 int i = 0, j=0;
 int trans[4] = {0,0,0,0};
 for (;base64[i]!='\0';i+=4){
  // 每四个一组,译码成三个字符
  trans[0] = num_strchr(base64char, base64[i]);
  trans[1] = num_strchr(base64char, base64[i+1]);
  // 1/3
  dedata[j++] = ((trans[0] << 2) & 0xfc) | ((trans[1]>>4) & 0x03);

  if (base64[i+2] == '='){
   continue;
  }
  else{
   trans[2] = num_strchr(base64char, base64[i + 2]);
  }
  // 2/3
  dedata[j++] = ((trans[1] << 4) & 0xf0) | ((trans[2] >> 2) & 0x0f);

  if (base64[i + 3] == '='){
   continue;
  }
  else{
   trans[3] = num_strchr(base64char, base64[i + 3]);
  }

  // 3/3
  dedata[j++] = ((trans[2] << 6) & 0xc0) | (trans[3] & 0x3f);
 }

 dedata[j] = '\0';

 return 0;
}

下面是其他网友的补充可以参考一下

核心代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
static const char b64_table[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const char reverse_table[128] =
{
 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
 64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64
};

unsigned char *base64_encode(unsigned char *bindata,size_t inlen,unsigned char **out,size_t *outlen)
{
 size_t _outlen = *outlen;
 unsigned char *_out = NULL;
 size_t out_pos = 0;

 if(NULL == *out)
 {
  _outlen = (inlen / 3 + (inlen%3 != 0)) * 4 + 1;
  _out = malloc(_outlen);
 }
 else
 {
  _outlen = *outlen;
  _out = *out;
 }

 memset(_out,'=',_outlen);
 _out[_outlen-1] = 0;

 unsigned int bits_collected = 0;
 unsigned int accumulator = 0;
 for(int i = 0; i < inlen; i++)
 {
  accumulator = (accumulator << 8) | (bindata[i] & 0xffu);
  bits_collected += 8;
  while (bits_collected >= 6)
  {
   bits_collected -= 6;
   _out[out_pos++] = b64_table[(accumulator >> bits_collected) & 0x3fu];
  }
 }

 if(bits_collected >= 6)
 {
  if(NULL == *out)
  {
   free(_out);
  }
  return NULL;
 }

 if (bits_collected > 0)
 {
  // Any trailing bits that are missing.
  accumulator <<= 6 - bits_collected;
  _out[out_pos++] = b64_table[accumulator & 0x3fu];
 }

 *outlen = _outlen;
 *out = _out;
 return _out;
}

unsigned char *base64_decode(unsigned char *bindata,size_t inlen,unsigned char **out,size_t *outlen)
{
 size_t _outlen = *outlen;
 unsigned char *_out = NULL;
 int bits_collected = 0;
 unsigned int accumulator = 0;
 size_t out_pos = 0;

 if(NULL == *out)
 {
  _outlen = inlen;
  _out = malloc(_outlen);
 }
 else
 {
  _outlen = *outlen;
  _out = *out;
 }

 int c = 0;
 for(int i = 0; i < inlen; i++)
 {
  c = bindata[i];
  if (isspace(c) || c == '=')
  {
   // Skip whitespace and padding. Be liberal in what you accept.
   continue;
  }
  if ((c > 127) || (c < 0) || (reverse_table[c] > 63))
  {
   return NULL;
  }
  accumulator = (accumulator << 6) | reverse_table[c];
  bits_collected += 6;
  if (bits_collected >= 8)
  {
   bits_collected -= 8;
   _out[out_pos++] = (char)((accumulator >> bits_collected) & 0xffu);
  }
 }

 *outlen = _outlen;
 *out = _out;
 return _out;
}

int main(int argc,char *argv[])
{
 unsigned char *str = argv[1];
 unsigned char *out = 0;
 size_t len = 0;
 printf("%s\n",base64_encode(str,strlen(str),&out,&len));
 unsigned char *_out = 0;
 size_t _len = 0;
 printf("%s\n",base64_decode(out,strlen(out),&_out,&_len));
 return 0;
}

这篇C语言base64编解码的文章就介绍到这了,希望大家以后多多支持脚本之家。

更多精彩内容其他人还在看

C++中四种对象生存期和作用域以及static的用法总结分析

以下是对C++中四种对象生存期和作用域以及static的用法进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++嵌套类与局部类详细解析

从作用域的角度看,嵌套类被隐藏在外围类之中,该类名只能在外围类中使用。如果在外围类之外的作用域使用该类名时,需要加名字限定
收藏 0 赞 0 分享

C++空类详解

以下是对C++中的空类进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++之友元:友元函数和友元类详解

友元是一种允许非类成员函数访问类的非公有成员的一种机制。可以把一个函数指定为类的友元,也可以把整个类指定为另一个类的友元
收藏 0 赞 0 分享

C++中返回指向函数的指针示例

int (*ff(int)) (int *,int);表示:ff(int)是一个函数,带有一个int型的形参,该函数返回int (*) (int *,int),它是一个指向函数的指针,所指向的函数返回int型并带有两个分别是Int*和int型的形参
收藏 0 赞 0 分享

C数据结构之单链表详细示例分析

以下是对C语言中的单链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C数据结构之双链表详细示例分析

以下是对c语言中的双链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅析如何在c语言中调用Linux脚本

如何在c语言中调用Linux脚本呢?下面小编就为大家详细的介绍一下吧!需要的朋友可以过来参考下
收藏 0 赞 0 分享

深入解析unsigned int 和 int

以下是对unsigned int和int进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅谈C++中的string 类型占几个字节

本篇文章小编并不是为大家讲解string类型的用法,而是讲解我个人比较好奇的问题,就是string 类型占几个字节
收藏 0 赞 0 分享
查看更多