一、代码
1.1 头文件
//
//Descript: base64 encode and decode.
// Author: guowenyan
// Date: 2013.12.09
//
#pragma once
#include <string>
class base64
{
public:
bool Encode(const char* data, unsigned int len, std::string& str_result);
bool Decode(const char* data, unsigned int len, std::string& str_result);
};
1.2 实现文件
//
//Descript: base64 encode and decode.
// Author: guowenyan
// Date: 2013.12.09
//
#include "base64.h"
using namespace std;
bool base64::encode(const char* data, unsigned int len, string& str_result)
{
if ( NULL == data || len <= 0)
{
return false;
}
char encode_table[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
char a, b, c;
int line_len = 0;
for ( int i=0; i<len/3; ++i )
{
a = *data++;
b = *data++;
c = *data++;
str_result += encode_table[a >> 2];
str_result += encode_table[((a << 4) | (b >> 4)) & 0x3F];
str_result += encode_table[((b << 2) | (c >> 6)) & 0x3F];
str_result += encode_table[c & 0x3F];
if (line_len += 4, 76 == line_len)
{
str_result += "\r\n";
line_len = 0;
}
}
int mod = len % 3;
if ( 1 == mod )
{
a = *data++;
str_result += encode_table[(a & 0xFC) >> 2];
str_result += encode_table[(a & 0x03) << 4];
str_result += "==";
}
else if ( 2 == mod )
{
a = *data++;
b = *data++;
str_result += encode_table[(a & 0xFC) >> 2];
str_result += encode_table[((a & 0x03) << 4) | ((b & 0xF0) >> 4)];
str_result += encode_table[(b & 0x0F) << 2];;
str_result += "=";
}
return true;
}
bool base64::decode(const char* data, unsigned int len, string& str_result)
{
if ( NULL == data || len <= 0)
{
return false;
}
const char decode_table[] =
{
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
62, // '+'
-1, -1, -1,
63, // '/'
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // '0'-'9'
-1, -1, -1, -1, -1, -1, -1,
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, // 'A'-'Z'
-1, -1, -1, -1, -1, -1,
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, // 'a'-'z'
};
char a, b, c, d;
int i=0;
while ( i < len )
{
if ( '\r' == *data || '\n' == *data )
{
data++;
i++;
continue;
}
a = decode_table[*data++];
b = decode_table[*data++];
c = decode_table[*data++];
d = decode_table[*data++];
str_result += ( (a << 2) | ((b >> 4) & 0x03) );
if ( -1 != c ) str_result += ( (b << 4) | ((c >> 2) & 0x0F) );
if ( -1 != d ) str_result += ( (c << 6) | (d & 0x3F) );
i += 4;
}
return true;
}
1.3 主函数
#include <iostream>
using namespace std;
#include "base64.h"
int main()
{
//
//char ssz[] = "abcdef1234567 abcdef1234567 abcdef1234567 abcdef1234567 abcdef1234567 abcdef1234567";
char sz[] = "123456adbde";
unsigned int in_len = strlen(sz);
cout<<"sz:"<<sz<<","<<in_len<<endl;
base64 test;
//encode
string str_encode;
test.Encode(sz, in_len, str_encode);
cout<<"str_encode:"<<str_encode.c_str()<<","<<str_encode.length()<<endl;
//decode
string str_decode;
test.Decode(str_encode.c_str(), str_encode.length(), str_decode);
cout<<"str_decode:"<<str_decode.c_str()<<","<<str_decode.length()<<endl;
return 0;
}
二、结果