BASE64编码C/C++实现

//base64.h
#ifndef base64_h
#define base64_h

#include <stdio.h>

#if _cplusplus
extern "C"{
#endif
	void enBASE64code( char *charBuf,	 int charBufLen,    char *base64Char,int &base64CharLen);		//BASE64编码
	void deBASE64code( char *base64Char,  int base64CharLen, char *outStr,	 int &outStrLen);			//BASE64解码
#ifdef _cplusplus
}
#endif


#endif
//base64.cpp
// BASE64.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "base64.h"
#include "string.h"

static const char *ALPHA_BASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; //BASE64  64个字符

int main()
{
	char str[]="sdfsdfsdfsd5沈雪冰151546……*(U&*(@&";
	int strLen=strlen(str);
	char str_base64[1024]={0};
	int base64Len;
	enBASE64code(str,strLen,str_base64,base64Len);
	printf("BASE64:%s\n",str_base64);
	printf("len=%d\n",base64Len);
	char de_str[1024]={0};
	int de_strLen=1024;
	deBASE64code(str_base64,base64Len,de_str,de_strLen);
	printf("解析后:%s\n",de_str);
	printf("len=%d\n",de_strLen);
	getchar();
}




void enBASE64code( char *charBuf,	 int charBufLen,    char *base64Char,int &base64CharLen) 
{
	int a = 0;
	int i = 0;
	while (i < charBufLen) 
	{
		char b0 = charBuf[i++];
		char b1 = (i < charBufLen) ? charBuf[i++] : 0;
		char b2 = (i < charBufLen) ? charBuf[i++] : 0;

		int int63 = 0x3F;	//  00111111 意图去掉8bit的两高位
		int int255 = 0xFF;	// 11111111
		base64Char[a++] = ALPHA_BASE[(b0 >> 2) & int63];	//b0字符的前6bit   00+6bit 组成一个十进制数 即BASE编码表号
		base64Char[a++] = ALPHA_BASE[((b0 << 4) | ((b1 & int255) >> 4)) & int63];	//b0 要想取最后两位,所以左移四位 得到最后两位 再和b1字符的 前四个字符做  或运算 组成6bit  00+6bit
		base64Char[a++] = ALPHA_BASE[((b1 << 2) | ((b2 & int255) >> 6)) & int63];	//b1 要想得到后四位  需要右移2位 再和 b2的前4 位做或运算 组成6bit   00+bit 租组成BASE64编码
		base64Char[a++] = ALPHA_BASE[b2 & int63];  //b2的后6位   00+6bit 组成BASE编码号
	}
	//最后不够4个字符  补=,最多补两个 ==
	switch (charBufLen % 3) 
	{
	case 1:
		base64Char[--a] = '=';
	case 2:
		base64Char[--a] = '=';
	}
	base64CharLen=strlen(base64Char); //输出的base64长度

}

void deBASE64code( char *base64Char,  int base64CharLen, char *outStr,	 int &outStrLen) 
{
	int toInt[128] = {-1};
	for (int i = 0; i < 64; i++) //将ANSI 码找到对应的BASE64码
	{
		int pos=ALPHA_BASE[i];
		toInt[pos] = i;
	}
	int int255 = 0xFF;  //11111111
	int index = 0;
	for (int i = 0; i < base64CharLen; i += 4) 
	{
		int c0 = toInt[base64Char[i]]; // 获取一组中第一个BASE64编码号
		int c1 = toInt[base64Char[i + 1]];// 获取一组中第二个BASE64编码号
		outStr[index++] = (((c0 << 2) | (c1 >> 4)) & int255);  //c0后6位  和c1去完前2位的  的前2位  组成8bit 再转换成字符  
		if (index >= base64CharLen)  //如果长度小于输入进来的长度 意图去掉=
		{
			return ;
		}
		int c2 = toInt[base64Char[i + 2]];// 获取一组中第三个BASE64编码号
		outStr[index++] = (((c1 << 4) | (c2 >> 2)) & int255);  //c1的后6位和c2去完前2位的  的前2位 组成8bit  转换成字符
		if (index >= outStrLen)  //如果长度小于输入进来的长度  意图去掉=
		{
			return ;
		}
		int c3 = toInt[base64Char[i + 3]];// 获取一组中第四个BASE64编码号
		outStr[index++] = (((c2 << 6) | c3) & int255); //c2的后2位和c3的后6位  组成8bit 转换成字符
	}
	outStrLen=strlen(outStr);

}

运行结果:
这里写图片描述

以下是一个简单的Base64编码和解码实现的C程序: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> const char BASE64_TABLE[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; char* base64_encode(const unsigned char* data, size_t input_length) { size_t 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 ? 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++] = 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 < input_length % 3; i++) { encoded_data[output_length - 1 - i] = '='; } encoded_data[output_length] = '\0'; return encoded_data; } 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); 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_TABLE[data[i++]]; uint32_t sextet_b = data[i] == '=' ? 0 & i++ : BASE64_TABLE[data[i++]]; uint32_t sextet_c = data[i] == '=' ? 0 & i++ : BASE64_TABLE[data[i++]]; uint32_t sextet_d = data[i] == '=' ? 0 & i++ : BASE64_TABLE[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; } return decoded_data; } int main() { const char* input = "Hello, world!"; size_t input_length = strlen(input); char* encoded_data = base64_encode((const unsigned char*)input, input_length); printf("Encoded data: %s\n", encoded_data); size_t output_length; unsigned char* decoded_data = base64_decode(encoded_data, strlen(encoded_data), &output_length); printf("Decoded data: %s\n", decoded_data); free(encoded_data); free(decoded_data); return 0; } ``` 程序首先定义了一个Base64字符表,包含了所有可能的Base64字符。然后实现了两个函数,分别是Base64编码和解码函数。 Base64编码函数将输入数据按照每3个字节一组进行处理,将每组字节转换成4个Base64字符。如果输入数据不是3的倍数,则在末尾加上相应数量的0字节,以使其能够被3整除。最后,函数将转换后的字符串末尾补上相应数量的=字符。 Base64解码函数将输入数据按照每4个字符一组进行处理,将每组字符转换成3个字节。如果末尾有=字符,则说明输入数据被填充了,需要在解码后去掉填充的0字节。 在程序中,我们将一个字符串进行Base64编码,然后再将编码后的字符串解码回原始字符串,最后输出结果。在输出解码后的数据时,我们将其当作一个字符串输出,可能会出现乱码,这是因为转换后的数据可能包含了0字节,而字符串输出函数会在遇到0字节时停止输出。如果需要输出二进制数据,应该使用fwrite函数来输出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

离水的鱼儿

一分也是爱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值