1、Base64是一种编码规则,用于将二进制数据转换为可表示的文本字符串形式。它使用64个字符来表示所有可能的数值组合。
(1)可视字码表如下,由 A-Z、a-z、0-9、“+” 和 “/” 按如下的标准顺序组成。
const char base64_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
(2)输出的编码包含上面的字符,如果末尾有填充,还会包含1或2个 “=”字符。
(3)编码规则:将待编码的数据分成每3个字节一组,按照每组3个字节将其转换为4个6位的数字,将每个6位的数字映射为Base64字符集中的对应字符,如果待编码数据的字节数不是3的倍数,会采用0填充,编码后填充了几个零末尾就有几个=。
(4)比如一个字符"9",填充后为"900",二进制编码 001110 010000 000000 000000, 编码映射后为OQAA, 因为原始字符串填充了两个0,所有倒数两个字符要替换为两个“=”,最终编码结果为OQ==。
(5)注意:最后两个=不是追加,而是把填充位的非0值保留,0值用=号代替,所有基本上填充了几个0,末尾就会有几个=号。OQAA== 是错误的。
(6)编码前和编码后的长度关系:L = 4 * ceil(n / 3) 。n是原长度,L是编码后长度,ceil表示向上取整。
2、参考代码:
#include <string.h>
const char base64_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int sm_base64_encode(const unsigned char *input, const unsigned int input_length, unsigned char *out, const unsigned int out_men_len) {
if (input==NULL || input_length<=0 || out==NULL || out_men_len<=0)
{
return -1;
}
unsigned int output_length = 4 * ((input_length + 2) / 3);
unsigned int i = 0, j = 0;
if (output_length >= out_men_len )
{ //out 内存大小不够
return -2;
}
for (i = 0, j = 0; i < input_length;) {
unsigned char byte0 = i < input_length ? input[i++] : 0;
unsigned char byte1 = i < input_length ? input[i++] : 0;
unsigned char byte2 = i < input_length ? input[i++] : 0;
unsigned char encoded_byte0 = byte0 >> 2;
unsigned char encoded_byte1 = ((byte0 & 0x03) << 4) | (byte1 >> 4);
unsigned char encoded_byte2 = ((byte1 & 0x0F) << 2) | (byte2 >> 6);
unsigned char encoded_byte3 = byte2 & 0x3F;
out[j++] = base64_chars[encoded_byte0];
out[j++] = base64_chars[encoded_byte1];
out[j++] = base64_chars[encoded_byte2];
out[j++] = base64_chars[encoded_byte3];
}
int padding = input_length % 3;
if (padding > 0) {
unsigned int i = 0;
for (i = 0; i < 3 - padding; i++) {
out[output_length - 1 - i] = '=';
}
}
out[output_length] = '\0';
return output_length;
}
int sm_base64_decode(const unsigned char *input, unsigned int input_length, unsigned char *out, const unsigned int out_men_len) {
if (input==NULL || input_length<=0 || out==NULL || out_men_len<=0)
{
return -1;
}
int output_length = 0;
unsigned int i, j;
if (((input_length/4)*3)>=out_men_len)
{ // out 的内存大小不够
return -1;
}
int padding_num = 0;
while (input_length > 0 && input[input_length - 1] == '=') {
input_length--;
padding_num++;
}
for (i = 0, j = 0; i < input_length;) {
unsigned char encoded_byte0 = strchr(base64_chars, input[i++]) - base64_chars;
unsigned char encoded_byte1 = strchr(base64_chars, input[i++]) - base64_chars;
unsigned char encoded_byte2 = i < input_length ? strchr(base64_chars, input[i++]) - base64_chars : 0;
unsigned char encoded_byte3 = i < input_length ? strchr(base64_chars, input[i++]) - base64_chars : 0;
unsigned char byte0 = (encoded_byte0 << 2) | (encoded_byte1 >> 4);
unsigned char byte1 = ((encoded_byte1 & 0x0F) << 4) | (encoded_byte2 >> 2);
unsigned char byte2 = ((encoded_byte2 & 0x03) << 6) | encoded_byte3;
out[j++] = byte0;
out[j++] = byte1;
out[j++] = byte2;
output_length += 3;
}
output_length -= padding_num;
return output_length;
}