Base64概述:
- Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,Base64就是一种基于64个可打印字符来表示二进制数据的方法
- Base64一般用于在HTTP协议下传输二进制数据,由于HTTP协议是文本协议,所以在HTTP协议下传输二进制数据需要将二进制数据转换为字符数据
- 而且网络传输只能传输可打印字符
- ASCII码中规定,0~31、128这33个字符属于控制字符,32~127这95个字符属于可打印字符
Base64 编码表:
转换原理:
- 将索引转换为对应的二进制数据的话需要至多6个Bit
- 而ASCII码需要8个Bit来表示
- 4*6个Bit可以存储3*8个Bit的数据,也就是说3个ASCII字符刚好转换成对应的4个Base64字符
- Base64规定,当需要转换的字符不是3的倍数时,一律采用补0的方式凑足3的倍数,即用 "=" 填充
相关命令:
base64 # 加密
base64 -d # 解码
eg. echo "tongyishu" | base64 # 结果为 dG9uZ3lpc2h1Cg==
echo "dG9uZ3lpc2h1Cg==" | base64 -d # 结果为 tongyishu
以下是我的base64加密实现:
#include <stdio.h>
#include <math.h>
#include <string.h>
static char g_Base64CodingTable[64] = {
'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', '+', '/'
};
/* covert 3 bytes to 4 bytes, input is 3 bytes, output is 4 bytes */
void CovertBy24Bits(char input[3], char output[4])
{
int bits[24];
int shift;
bits[0] = (input[0] & 0x80) == 0 ? 0 : 1;
bits[1] = (input[0] & 0x40) == 0 ? 0 : 1;
bits[2] = (input[0] & 0x20) == 0 ? 0 : 1;
bits[3] = (input[0] & 0x10) == 0 ? 0 : 1;
bits[4] = (input[0] & 0x08) == 0 ? 0 : 1;
bits[5] = (input[0] & 0x04) == 0 ? 0 : 1;
bits[6] = (input[0] & 0x02) == 0 ? 0 : 1;
bits[7] = (input[0] & 0x01) == 0 ? 0 : 1;
bits[8] = (input[1] & 0x80) == 0 ? 0 : 1;
bits[9] = (input[1] & 0x40) == 0 ? 0 : 1;
bits[10] = (input[1] & 0x20) == 0 ? 0 : 1;
bits[11] = (input[1] & 0x10) == 0 ? 0 : 1;
bits[12] = (input[1] & 0x08) == 0 ? 0 : 1;
bits[13] = (input[1] & 0x04) == 0 ? 0 : 1;
bits[14] = (input[1] & 0x02) == 0 ? 0 : 1;
bits[15] = (input[1] & 0x01) == 0 ? 0 : 1;
bits[16] = (input[2] & 0x80) == 0 ? 0 : 1;
bits[17] = (input[2] & 0x40) == 0 ? 0 : 1;
bits[18] = (input[2] & 0x20) == 0 ? 0 : 1;
bits[19] = (input[2] & 0x10) == 0 ? 0 : 1;
bits[20] = (input[2] & 0x08) == 0 ? 0 : 1;
bits[21] = (input[2] & 0x04) == 0 ? 0 : 1;
bits[22] = (input[2] & 0x02) == 0 ? 0 : 1;
bits[23] = (input[2] & 0x01) == 0 ? 0 : 1;
shift = 0;
shift += (bits[0] << 5);
shift += (bits[1] << 4);
shift += (bits[2] << 3);
shift += (bits[3] << 2);
shift += (bits[4] << 1);
shift += (bits[5] << 0);
output[0] = g_Base64CodingTable[shift];
shift = 0;
shift += (bits[6] << 5);
shift += (bits[7] << 4);
shift += (bits[8] << 3);
shift += (bits[9] << 2);
shift += (bits[10] << 1);
shift += (bits[11] << 0);
output[1] = g_Base64CodingTable[shift];
shift = 0;
shift += (bits[12] << 5);
shift += (bits[13] << 4);
shift += (bits[14] << 3);
shift += (bits[15] << 2);
shift += (bits[16] << 1);
shift += (bits[17] << 0);
output[2] = g_Base64CodingTable[shift];
shift = 0;
shift += (bits[18] << 5);
shift += (bits[19] << 4);
shift += (bits[20] << 3);
shift += (bits[21] << 2);
shift += (bits[22] << 1);
shift += (bits[23] << 0);
output[3] = g_Base64CodingTable[shift];
}
void Base64(char* input, char* output)
{
char inputByte[3];
char outputByte[4];
int inputLength = strlen(input);
for (int i = 0; i + 3 <= inputLength; i += 3) {
strncpy(inputByte, input + i, 3);
CovertBy24Bits(inputByte, outputByte);
strncat(output, outputByte, 4);
}
/* inputLength % 3 == 0 follow the normal process */
if (inputLength % 3 == 1) {
inputByte[0] = input[inputLength - 1];
inputByte[1] = 0;
CovertBy24Bits(inputByte, outputByte);
outputByte[2] = '=';
outputByte[3] = '=';
strncat(output, outputByte, 4);
} else if (inputLength % 3 == 2) {
inputByte[0] = input[inputLength - 2];
inputByte[1] = input[inputLength - 1];
inputByte[2] = 0;
CovertBy24Bits(inputByte, outputByte);
outputByte[3] = '=';
strncat(output, outputByte, 4);
}
}
int main()
{
const unsigned int MAX_LENGTH = 1024;
int count;
char input[MAX_LENGTH];
char output[MAX_LENGTH * 2];
scanf("%d
", &count);
for (int i = 0; i < count; i++) {
fgets(input, sizeof(input), stdin);
/* 这里需要去掉fgets尾部的
*/
int inputLength = strlen(input);
input[inputLength - 1] = '