Base64 编码原理及代码实现

Base64 编码原理及代码实现

所谓 base64 编码就是从 ASCII 码表中选取64个可打印字符(A-Za-z0-9+/)作为基本字符集对其它字符进行编码转换。加上作为填充的 “=” 实际上是 65 个字符。

Base64 产生的原因

要想了解 base64 就得先了解下 ASCII 码表, ASCII 码表是由以英语为母语的美国制定。英语用128个符号编码就够了,但是用来表示其他语言,128个符号是不够的。比如,在法语中,字母上方有注音符号,它就无法用 ASCII 码表示。于是,一些欧洲国家就决定,利用字节中闲置的最高位编入新的符号。比如,法语中的é的编码为130(二进制10000010)。这样一来,这些欧洲国家使用的编码体系,可以表示最多256个符号。

但是,这里又出现了新的问题。不同的国家有不同的字母,因此,哪怕它们都使用256个符号的编码方式,代表的字母却不一样。比如,130在法语编码中代表了é,在希伯来语编码中却代表了字母Gimel (ג),在俄语编码中又会代表另一个符号。但是不管怎样,所有这些编码方式中,0–127表示的符号是一样的,不一样的只是128–255的这一段。

至于亚洲国家的文字,使用的符号就更多了,汉字就多达10万左右。一个字节只能表示256种符号,肯定是不够的,就必须使用多个字节表达一个符号。比如,简体中文常见的编码方式是 GB2312,使用两个字节表示一个汉字,所以理论上最多可以表示 256 * 256 = 65536 个符号。

而在网络上交换数据时,比如说从A地传到B地,往往要经过多个路由设备,由于不同的设备对字符的处理方式有一些不同,这样那些不可见字符就有可能被处理错误,这是不利于传输的。所以就先把数据先做一个 Base64 编码,统统变成可见字符,这样出错的可能性就大降低了。

为什么是 Base64?

为什么是 base64 而不是 base128、base256 呢?其实原因很简单,因为在 ASCII 码表中的可打印字符只有 95 个,所以选取 64 个可打印字符是最为合理的。既然如此,那是不是也有 base32、base16 呢?对,当然可以有。只是目前大多用到的还是 base64 编码。

Base64 编码的理论实现

前面根据 A-Za-z0-9+/ 字符集可以得到一张索引表:

索引对应字符索引对应字符索引对应字符索引对应字符
0A17R34i51z
1B18S35j520
2C19T36k531
3D20U37l542
4E21V38m553
5F22W39n564
6G23X40o575
7H24Y41p586
8I25Z42q597
9J26a43r608
10K27b44s619
11L28c45t62+
12M29d46u63/
13N30e47v
14O31f48w
15P32g49x
16Q33h50y

Base64 具体编码转换步骤如下:

1.将待转换的字符串以每 3 个字节分为一组,1byte = 8bit,每一组正好 24 个二进制位。
2.将上面的 24 个二进制位划分为每 6 位一组,形成 4 组。
3.每组前面加两个 0,形成 8 位一组,即 4 个字节。
4.根据上面 Base64 对照表获取对应的值,形成 Base64 编码。

**举个例子:**下面表格是以字符串 “Man” 作为原始字符串进行 Base64 编码的过程。

文本Man
ASCII7797110
二进制010011010110000101101110
分组00 01001100 01011000 00010100 101110
索引1922546
Base64编码TWFu

那么你可能会问,如果我原始字符串少于 3 个字节怎么办呢?

如果输入原始字符串长度不能被 3 整除的话,我们需要用 “=” 对其 Base64 编码进行填充。为什么需要 “=” 填充呢?因为 Base64 解码是以 4 位字符一划分的,如果你不对其进行填充就会导致解码失败。

当原始字符串的二进制位不是 6 的倍数的时候,我们依然会将其划分为 6 位一组,然后将最后一组用 0 填充至 6 位(在末尾填充)。

**举个例子:**下面是对字符串 “AB” 的编码过程,其编码结果为 “QUI=”。

文本AB
ASCII6566
二进制0100000101000010
分组00 01000000 01010000 001000
索引16208
Base64编码QUI=

注意:中文字符有很多的编码,如 UTF-8、GBK、GB2312 等,不同的编码都会对 Base64 编码产生影响。

源代码

base64.h

/*base64.h*/
#ifndef _BASE64_H
#define _BASE64_H

#include <stdlib.h>
#include <string.h>
#include <math.h>

inline unsigned int BASE64_ENCODE_SIZE(unsigned int len) { // 计算字符串加密后的长度(不包括填充字符 '=')return ceil(len * 8 / 6);
}

unsigned char *base64encode(const unsigned char *str, unsigned int len);
unsigned char *base64decode(const unsigned char *str, unsigned int len);

#endif 

base64.c

#include <stdio.h>
#include "base64.h"

#define CHARPAD '='

extern inline unsigned int BASE64_ENCODE_SIZE(unsigned int);

/* Base64 编码表 */
static const unsigned char base64_table_encode[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H','I', 'J', 'K', 'L', 'M', 'N', 'O', 'P','Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X','Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f','g', 'h', 'i', 'j', 'k', 'l', 'm', 'n','o', 'p', 'q', 'r', 's', 't', 'u', 'v','w', 'x', 'y', 'z', '0', '1', '2', '3','4', '5', '6', '7', '8', '9', '+', '/',
};

unsigned char *base64encode(const unsigned char *str, unsigned int len)
{int i = 0, j = 0, k = 0;unsigned int encodeSize = BASE64_ENCODE_SIZE(len);unsigned char *result = (unsigned char *) malloc(sizeof(unsigned char) * encodeSize + 4);result[encodeSize] = '\0'; /* 构造字符串 */for(i = 0, j = 0; i < encodeSize; i += 4, j += 3){result[i] = base64_table_encode[(str[j] >> 2) & 0x3f];if (i + 1 >= encodeSize) break;result[i+1] = base64_table_encode[((str[j] & 0x3) << 4) | ((str[j+1] >> 4) & 0xf)];if (i + 2 >= encodeSize) break;result[i+2] = base64_table_encode[((str[j+1] & 0xf) << 2) | ((str[j+2] >> 6) & 0x3)];if (i + 3 >= encodeSize) break;result[i+3] = base64_table_encode[(str[j+2] & 0x3f)];}return result;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
base64 编码是将二进制数据编码为可传输的字符串的方法,它的原理是使用可打印的 ASCII 码表中的字符来表示二进制数据的六位二进制块。 以下是 C 语言中实现 base64 编码的示例代码: ``` #include <stdio.h> #include <stdlib.h> #include <string.h> char *base64_encode(const unsigned char *data, size_t input_length, size_t *output_length) { static const char encoding_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; *output_length = 4 * ((input_length + 2) / 3); char *encoded_data = malloc(*output_length); if (encoded_data == NULL) return NULL; for (size_t i = 0, j = 0; i < input_length;) { uint32_t octet_a = i < input_length ? data[i++] : 0; uint32_t octet_b = i < input_length ? data[i++] : 0; uint32_t octet_c = i < input_length ? data[i++] : 0; uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c; encoded_data[j++] = encoding_table[(triple >> 3 * 6) & 0x3F]; encoded_data[j++] = encoding_table[(triple >> 2 * 6) & 0x3F]; encoded_data[j++] = encoding_table[(triple >> 1 * 6) & 0x3F]; encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F]; } for (int i = 0; i < *output_length; i++) { if (i % 4 == 3) encoded_data[i] = '='; } return encoded_data; } int main(int argc, char *argv[]) { const char *data = "hello, world"; size_t input_length = strlen(data); size_t output_length = 0; char *encoded_data = base64_encode(data, input_length, &output_length); if (encoded_data == NULL) { fprintf(stderr, "Error encoding data\n"); return 1; } printf("Encoded data:\n%s\n", encoded_data); free(encoded_data); return 0; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值