#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;
}