base64编码解码函数

base64位编解码函数

void encode(byte* input ,byte*& output)
{
	char arr[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
	int len = strlen((char*)input);
	int remain = len % 3;
	if (remain == 0)
	{
		output = new byte[len / 3 * 4 + 1]; memset(output, 0, len / 3 * 4 + 1);
		for (int i = 0, j = 0; i<len; i += 3, j += 4)
		{
			byte a1 = (input[i] >> 2) & 0x3f;
			byte a2 = (((input[i] << 6) | (input[i + 1] >> 4 << 2)) >> 2) & 0x3f;
			byte a3 = (((input[i + 1] << 4) | (input[i + 2] >> 6 << 2)) >> 2) & 0x3f;
			byte a4 = (input[i + 2] << 2 >> 2) & 0x3f;
			output[j] = arr[a1];
			output[j + 1] = arr[a2];
			output[j + 2] = arr[a3];
			output[j + 3] = arr[a4];
		}
	}
	else if (remain == 1)
	{
		output = new byte[len / 3 * 4 + 4 + 1]; memset(output, 0, len / 3 * 4 + 4 + 1);
		int i = 0, j = 0;
		for (; i<len - 3; i += 3, j += 4)
		{
			byte a1 = (input[i] >> 2) & 0x3f;
			byte a2 = (((input[i] << 6) | (input[i + 1] >> 4 << 2)) >> 2) & 0x3f;
			byte a3 = (((input[i + 1] << 4) | (input[i + 2] >> 6 << 2)) >> 2) & 0x3f;
			byte a4 = (input[i + 2] << 2 >> 2) & 0x3f;
			output[j] = arr[a1];
			output[j + 1] = arr[a2];
			output[j + 2] = arr[a3];
			output[j + 3] = arr[a4];
		}
		byte a1 = (input[i] >> 2) & 0x3f;
		byte a2 = (((input[i] << 6) | 0x00) >> 2) & 0x3f;
		output[j] = arr[a1];
		output[j + 1] = arr[a2];
		output[j + 2] = '=';
		output[j + 3] = '=';
	}
	else if (remain == 2)
	{
		output = new byte[len / 3 * 4 + 4 + 1]; memset(output, 0, len / 3 * 4 + 4 + 1);
		int i = 0, j = 0;
		for (; i<len - 3; i += 3, j += 4)
		{
			byte a1 = (input[i] >> 2) & 0x3f;
			byte a2 = (((input[i] << 6) | (input[i + 1] >> 4 << 2)) >> 2) & 0x3f;
			byte a3 = (((input[i + 1] << 4) | (input[i + 2] >> 6 << 2)) >> 2) & 0x3f;
			byte a4 = (input[i + 2] << 2 >> 2) & 0x3f;
			output[j] = arr[a1];
			output[j + 1] = arr[a2];
			output[j + 2] = arr[a3];
			output[j + 3] = arr[a4];
		}
		byte a1 = (input[i] >> 2) & 0x3f;
		byte a2 = (((input[i] << 6) | input[i + 1] >> 4 << 2) >> 2) & 0x3f;
		byte a3 = (((input[i + 1] << 4) | 0x00) >> 2) & 0x3f;
		output[j] = arr[a1];
		output[j + 1] = arr[a2];
		output[j + 2] = arr[a3];
		output[j + 3] = '=';
	}
}
byte find(byte c)
{
	char arr[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
	for (byte i = 0; i<64; i++)
	{
		if (arr[i] == c)
		{
			return i;
		}
	}
}
void decode(byte* input, byte*& output)
{
	int len = strlen((char*)input);
	if (input[len - 1] != '=')
	{
		output = new byte[len / 4 * 3 + 1];
		memset(output, 0, len / 4 * 3 + 1);
		for (int i = 0, j = 0; i<len; i += 4, j += 3)
		{
			byte a1 = (find(input[i]) << 2) | (find(input[i + 1]) >> 4);
			byte a2 = (find(input[i + 1]) << 4) | (find(input[i + 2]) >> 2);
			byte a3 = (find(input[i + 2]) << 6) | find(input[i + 3]);
			output[j] = a1;
			output[j + 1] = a2;
			output[j + 2] = a3;
		}
	}
	else if (input[len - 1] == '='&&input[len - 2] == '=')
	{
		output = new byte[len / 4 * 3 - 2 + 1];
		memset(output, 0, len / 4 * 3 - 2 + 1);
		int i = 0, j = 0;
		for (; i<len - 4; i += 4, j += 3)
		{
			byte a1 = (find(input[i]) << 2) | (find(input[i + 1]) >> 4);
			byte a2 = (find(input[i + 1]) << 4) | (find(input[i + 2]) >> 2);
			byte a3 = (find(input[i + 2]) << 6) | find(input[i + 3]);
			output[j] = a1;
			output[j + 1] = a2;
			output[j + 2] = a3;
		}
		byte a1 = (find(input[i]) << 2) | (find(input[i + 1]) >> 4);
		output[j] = a1;
	}
	else if (input[len - 1] == '='&&input[len - 2] != '=')
	{
		output = new byte[len / 4 * 3 - 1 + 1];
		memset(output, 0, len / 4 * 3 - 1 + 1);
		int i = 0, j = 0;
		for (; i<len - 4; i += 4, j += 3)
		{
			byte a1 = (find(input[i]) << 2) | (find(input[i + 1]) >> 4);
			byte a2 = (find(input[i + 1]) << 4) | (find(input[i + 2]) >> 2);
			byte a3 = (find(input[i + 2]) << 6) | find(input[i + 3]);
			output[j] = a1;
			output[j + 1] = a2;
			output[j + 2] = a3;
		}
		byte a1 = (find(input[i]) << 2) | (find(input[i + 1]) >> 4);
		byte a2 = (find(input[i + 1]) << 4) | (find(input[i + 2]) >> 2);
		output[j] = a1;
		output[j + 1] = a2;
	}
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值