Base64编码与解码的C++实现

#include <iostream>
#include <string>
using namespace std;

class Base64
{
public:
	int Encode(string &s);
	int Decode(string &s);
	int EncodeWeb(string &s);
	int DecodeWeb(string &s);
private:
	const unsigned char Base64EncodeMap[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', '+', '/'
	};
	const unsigned char Base64DecodeMap[256] =
	{
		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
		0xFF, 0xFF, 0xFF, 0x3E, 0xFF, 0xFF, 0xFF, 0x3F,
		0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B,
		0x3C, 0x3D, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0xFF,
		0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
		0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
		0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
		0x17, 0x18, 0x19, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
		0xFF, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20,
		0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
		0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30,
		0x31, 0x32, 0x33, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
	};
	string EncodeEach(char s[3]);
	string s_processed;
	int DecodeCheck(const string &s, bool isWebSafe);
};
int Base64::Encode(string &s)
{
	char str_to_operate[3] = { 0 };
	for (size_t i = 0; s.size() >= 3; ++i)
	{
		for (size_t j = 0; j != 3; ++j)
			str_to_operate[j] = s[j];
		s = s.substr(3);
		s_processed += EncodeEach(str_to_operate);
	}
	switch (s.size())
	{
	case 0:break;
	case 1:
		str_to_operate[0] = s[0];
		str_to_operate[1] = 0;
		str_to_operate[2] = 0;
		break;
	case 2:
		str_to_operate[0] = s[0];
		str_to_operate[1] = s[1];
		str_to_operate[2] = 0;
		break;
	}
	s_processed += EncodeEach(str_to_operate);
	s = s_processed;
	return 0;
}

string Base64::EncodeEach(char s[3])
{
	string tmp;
	tmp.clear();
	if (s[0] != 0)
	{
		tmp += Base64EncodeMap[s[0] >> 2];
		if (s[1] != 0)
		{
			tmp += Base64EncodeMap[((s[0] & 0x03) << 4) | (s[1] >> 4)];
			if (s[2] != 0)
			{
				tmp += Base64EncodeMap[((s[1] & 0x0F) << 2) | (s[2] >> 6)];
				tmp += Base64EncodeMap[s[2] & 0x3F];
			}
			else
			{
				tmp += Base64EncodeMap[(s[1] & 0x0F) << 2];
				tmp += "=";
			}
		}
		else
		{
			tmp += Base64EncodeMap[((s[0] & 0x03) << 4)];
			tmp += "==";
		}
	}
	return tmp;
}

int Base64::EncodeWeb(string &s)
{
	Encode(s);
	for (string::size_type i = 0; i != s.size(); ++i)
	{
		if (s[i] == '+')
			s[i] = '-';
		else if (s[i] == '/')
			s[i] = '_';
	}
	return 0;
}

int Base64::DecodeCheck(const string &s, bool isWebSafe)  \\检查待解码的字符串是否是Base64编码而来
{
	if (isWebSafe == true)
	{
		for (string::const_iterator it = s.begin(); it != s.end(); ++it)
			if (!isalnum(*it) && *it != '+' && *it != '/')
				return -1;
	}
	else
	{
		for (string::const_iterator it = s.begin(); it != s.end(); ++it)
			if (!isalnum(*it) && *it != '-' && *it != '_')
				return -1;
	}
	return 0;
}

int Base64::Decode(string &s)
{
	if (DecodeCheck(s, false) != -1)
	{
		char str_to_operate[4];
		for (size_t i = 0; s.size() >= 4; ++i)
		{
			for (size_t j = 0; j != 4; ++j)
			{
				str_to_operate[j] = Base64DecodeMap[s[j]];
			}
			s = s.substr(4);
			s_processed += (str_to_operate[0] << 2) | (str_to_operate[1] >> 4);
			s_processed += (str_to_operate[1] << 4) | (str_to_operate[2] >> 2);
			s_processed += (str_to_operate[2] << 6) | str_to_operate[3];
		}
		s = s_processed;
	}
	else return -1;
	return 0;
}
int Base64::DecodeWeb(string &s)
{
	if (s.size() % 4 == 0)
	{
		if (!DecodeCheck(s, true))
		{
			for (string::size_type i = 0; i != s.size(); ++i)
			{
				if (s[i] == '-')
					s[i] = '+';
				else if (s[i] == '_')
					s[i] = '/';
			}
			Decode(s);
			return 0;
		}
		else return -1;
	}
	else return -1;

}

int main()
{
	cout << "1.编码\n2.解码\n选择操作(1 or 2):" << ends;
	char input;
	string s;
	Base64 a;
	cin >> input;
	if (input == '1')
	{
		cout << "输入待加密字符串:" << endl;
		cin >> s;
		a.Encode(s);
		cout << "编码结果:\n" << s << endl;
	}
	else if (input == '2')
	{
		cout << "输入待解密字符串:" << endl;
		cin >> s;
		a.Decode(s);
		cout << "解码结果:\n" << s << endl;
	}
	else
		cout << "输入错误" << endl;
	system("pause");
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值