手机
当前位置:查字典教程网 >编程开发 >C语言 >c++base64编解码使用示例
c++base64编解码使用示例
摘要:复制代码代码如下:#include#include#include#includestaticconstcharb64_table[65]=...

复制代码 代码如下:

#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("%sn",base64_encode(str,strlen(str),&out,&len));

unsigned char *_out = 0;

size_t _len = 0;

printf("%sn",base64_decode(out,strlen(out),&_out,&_len));

return 0;

}

【c++base64编解码使用示例】相关文章:

从汇编看c++中extern关键字的使用

getdate()函数的用法实例

c语言中static的用法详细示例分析

C++概念重载、覆盖、隐藏的使用说明

c++ dynamic_cast与static_cast使用方法示例

C++卸载程序功能示例

浅析c++ 宏 #val 在unicode下的使用

深入分析C++中deque的使用

c++ const引用与非const引用介绍

深入sizeof的使用详解

精品推荐
分类导航