自己实现一个小巧的base64编码解码


int Base64Encode(BYTE* input_data, DWORD cbData, TCHAR* OUT buffer)
{
	if(!input_data || !cbData || !buffer)
		return 0;

	static char* table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
	DWORD group_count = cbData / 3;
	DWORD remain_count = cbData - group_count * 3;
	DWORD dwTemp = 0;
	BYTE* pStore = (BYTE*)(&dwTemp);
	int r = 0;
	int w = 0;

	for(DWORD i = 0; i < group_count; i++)
	{
		pStore[2] = input_data[r++];
		pStore[1] = input_data[r++];
		pStore[0] = input_data[r++];

		for(int j = 0; j < 4; j++)
		{
			dwTemp <<= 6;
			buffer[w++] = table[pStore[3] & 0x3F];
		}
	}

	if(remain_count)
	{
		int k = 2;

		for(DWORD i = 0; i < remain_count; i++)
			pStore[k--] = input_data[r++];

		int rcnt = remain_count == 1 ? 2 : 3;
		int pcnt = 4 - rcnt;

		for(int j = 0; j < rcnt; j++)
		{
			dwTemp <<= 6;
			buffer[w++] = table[pStore[3] & 0x3F];
		}

		for(int j = 0; j < pcnt; j++)
			buffer[w++] = _T('=');
	}

	buffer[w] = 0;
	return w;
}

int Base64Decode(TCHAR* pBase64, DWORD cchChar, BYTE* pBuffer)
{
	if(cchChar < 4 || !pBase64 || !pBuffer)
		return 0;

	static BYTE rtable[256] =
	{
		64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
		64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
		52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64, 64, 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, 64, 64, 64, 64, 64, 64, 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,
		64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
		64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
		64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
		64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
		64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
		64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
	};
	int group_cnt = cchChar / 4 - 1;
	DWORD dwTemp = 0;
	BYTE* pStore = (BYTE*)(&dwTemp);
	int r = 0;
	int w = 0;

	for(int i = 0; i < group_cnt; i++)
	{
		dwTemp = 0;

		for(int j = 0; j < 4; j++)
		{
			dwTemp <<= 6;
			BYTE ch = rtable[ pBase64[r++] ];

			if(ch > 63)
				return -1;

			dwTemp |= ch ;
		}

		pBuffer[w++] = pStore[2];
		pBuffer[w++] = pStore[1];
		pBuffer[w++] = pStore[0];
	}

	int cc = 3;

	if(pBase64[cchChar - 1] == '=')
		cc--;

	if(pBase64[cchChar - 2] == '=')
		cc--;

	dwTemp = 0;

	for(int j = 0; j < 4; j++)
	{
		dwTemp <<= 6;
		dwTemp |= (rtable[ pBase64[r++]] & 0x3F);
	}

	int k = 2;

	for(int j = 0; j < cc; j++)
		pBuffer[w++] = pStore[k--];

	return w;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C语言实现base64编码解码的示例代码: ```c #include <stdio.h> #include <string.h> #include <stdlib.h> // Base64编码表 static const char base64_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; // Base64解码表 static const char base64_decode_table[] = { 62, -1, -1, -1, 63, // '+', '/', and 0x3D are special 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // '0'-'9', '=', and 'A'-'Z' -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, -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 }; // Base64编码函数 char* base64_encode(const unsigned char *data, size_t input_length, size_t *output_length) { *output_length = 4 * ((input_length + 2) / 3); // 计算编码后字符串长度 char *encoded_data = malloc(*output_length + 1); // 分配内存 if (encoded_data == NULL) { return NULL; } // 编码 for (size_t i = 0, j = 0; i < input_length;) { uint32_t octet_a = i < input_length ? (unsigned char)data[i++] : 0; uint32_t octet_b = i < input_length ? (unsigned char)data[i++] : 0; uint32_t octet_c = i < input_length ? (unsigned char)data[i++] : 0; uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c; encoded_data[j++] = base64_table[(triple >> 3 * 6) & 0x3F]; encoded_data[j++] = base64_table[(triple >> 2 * 6) & 0x3F]; encoded_data[j++] = base64_table[(triple >> 1 * 6) & 0x3F]; encoded_data[j++] = base64_table[(triple >> 0 * 6) & 0x3F]; } // 添加填充字符 for (size_t i = 0; i < (*output_length - input_length % 3) % 3; i++) { encoded_data[*output_length - 1 - i] = '='; } encoded_data[*output_length] = '\0'; return encoded_data; } // Base64解码函数 unsigned char* base64_decode(const char *data, size_t input_length, size_t *output_length) { if (input_length % 4 != 0) { return NULL; } *output_length = input_length / 4 * 3; // 计算解码后字节数 if (data[input_length - 1] == '=') { (*output_length)--; } if (data[input_length - 2] == '=') { (*output_length)--; } unsigned char *decoded_data = malloc(*output_length + 1); // 分配内存 if (decoded_data == NULL) { return NULL; } // 解码 for (size_t i = 0, j = 0; i < input_length;) { uint32_t sextet_a = data[i] == '=' ? 0 & i++ : base64_decode_table[(int)data[i++]]; uint32_t sextet_b = data[i] == '=' ? 0 & i++ : base64_decode_table[(int)data[i++]]; uint32_t sextet_c = data[i] == '=' ? 0 & i++ : base64_decode_table[(int)data[i++]]; uint32_t sextet_d = data[i] == '=' ? 0 & i++ : base64_decode_table[(int)data[i++]]; uint32_t triple = (sextet_a << 3 * 6) + (sextet_b << 2 * 6) + (sextet_c << 1 * 6) + (sextet_d << 0 * 6); if (j < *output_length) { decoded_data[j++] = (triple >> 2 * 8) & 0xFF; } if (j < *output_length) { decoded_data[j++] = (triple >> 1 * 8) & 0xFF; } if (j < *output_length) { decoded_data[j++] = (triple >> 0 * 8) & 0xFF; } } decoded_data[*output_length] = '\0'; return decoded_data; } int main() { char *data = "Hello, world!"; size_t data_length = strlen(data); // 编码 size_t encoded_length; char *encoded_data = base64_encode((const unsigned char*)data, data_length, &encoded_length); printf("Base64 encoded data: %s\n", encoded_data); // 解码 size_t decoded_length; unsigned char *decoded_data = base64_decode(encoded_data, encoded_length, &decoded_length); printf("Base64 decoded data: %s\n", decoded_data); free(encoded_data); free(decoded_data); return 0; } ``` 上述代码中,`base64_encode`函数用于将二进制数据编码Base64字符串,`base64_decode`函数用于将Base64字符串解码为原始的二进制数据。在编码时,函数会将每3个字节的二进制数据转换为4个Base64字符,如果遇到不足3个字节的末尾数据,则会用`=`字符进行填充。在解码时,函数会将每4个Base64字符转换为3个字节的二进制数据,如果遇到末尾的`=`字符,则会忽略它们。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值