in_str是输入缓冲区的地址
in_len 要编码的数据的长度
out_str 输出的地址
需要在外面分配内存
unsigned char *out_str = (unsigned char *) malloc (in_len * 4 / 3 + 1);
然后再调用函数编码。
void Base64Encode(const unsigned char *in_str, int in_len, unsigned char *out_str)
{
static unsigned char base64[] ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int curr_out_len = 0;
int i = 0;
unsigned char a, b, c;
out_str[0] = '\0';
if (in_len > 0)
{
while (i < in_len)
{
a = in_str[i];
b = (i + 1 >= in_len) ? 0 : in_str[i + 1];
c = (i + 2 >= in_len) ? 0 : in_str[i + 2];
if (i + 2 < in_len)
{
out_str[curr_out_len++] = (base64[(a >> 2) & 0x3F]);
out_str[curr_out_len++] = (base64[((a << 4) & 0x30) + ((b >> 4) & 0xf)]);
out_str[curr_out_len++] = (base64[((b << 2) & 0x3c) + ((c >> 6) & 0x3)]);
out_str[curr_out_len++] = (base64[c & 0x3F]);
}
else if (i + 1 < in_len)
{
out_str[curr_out_len++] = (base64[(a >> 2) & 0x3F]);
out_str[curr_out_len++] = (base64[((a << 4) & 0x30) + ((b >> 4) & 0xf)]);
out_str[curr_out_len++] = (base64[((b << 2) & 0x3c) + ((c >> 6) & 0x3)]);
out_str[curr_out_len++] = '=';
}
else
{
out_str[curr_out_len++] = (base64[(a >> 2) & 0x3F]);
out_str[curr_out_len++] = (base64[((a << 4) & 0x30) + ((b >> 4) & 0xf)]);
out_str[curr_out_len++] = '=';
out_str[curr_out_len++] = '=';
}
i += 3;
}
out_str[curr_out_len] = '\0';
}
return;
}
网上的解码函数,都是解码成字符串的,如果解码并保存到文件中,那么往往会多保存几个字节,主要是对base64字符串最后面的=号的判断不够,所以只要自行实现了一下:
void __stdcall Base64DecodeToFile(unsigned char *input, const unsigned char *dst_filename)
{
static int map[256] = {0};
static unsigned char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
size_t inplen = strlen(input);
int words = (inplen+3)/4;
size_t i=0, j=0;
int word = 0;
char *p = input;
int padnum = 0;
FILE *fd = NULL;
for (i = 0; i < 64; ++i)
{
map[(int)b64[i]]=i;
}
if(input[inplen - 1] == '=') padnum = 1;
if(input[inplen - 1] == '=' && input[inplen - 2] == '=') padnum = 2;
fd = fopen(dst_filename,"wb");
if (fd == NULL) return;
for(i=0; i
{
word = 0;
word |= map[(int)*p++];
word <<= 6;
word |= map[(int)*p++];
word <<= 6;
word |= map[(int)*p++];
word <<= 6;
word |= map[(int)*p++];
fprintf(fd, "%c", word >> 16 & 0xFF);
if (i + 1 == words && padnum == 2)
break;
fprintf(fd, "%c", word >> 8 & 0xFF);
if (i + 1 == words && padnum == 1)
break;
fprintf(fd, "%c", word & 0xFF);
}
fclose(fd);
return;
}