c++base64編解碼使用示例
#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語言
下一篇:c語言網(wǎng)絡(luò)編程-標(biāo)準(zhǔn)步驟(改進(jìn)版)
本文標(biāo)題:c++base64編解碼使用示例
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/3783.html
您可能感興趣的文章
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
- 01-10深入理解C/C++混合編程
- 01-10深入sizeof的使用詳解
- 01-10探討編寫int strlen(char *strDest);不允許定義變量的問題
- 01-10解析四則表達(dá)式的編譯過程及生成匯編代碼
- 01-10淺析C語言中的sizeof
- 01-10如何將C語言代碼轉(zhuǎn)換為應(yīng)用程序(也就是編譯)
- 01-10C語言編程時(shí)常犯十八個(gè)錯(cuò)誤小結(jié)
- 01-10VC6.0常見編譯錯(cuò)誤提示附解決方法
- 01-10編譯錯(cuò)誤error: stray ‘\343’in program的解決方法


閱讀排行
- 1C語言 while語句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 04-02jquery與jsp,用jquery
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子