什么是base64编码?

  • Base64是一种基于64个可打印字符来表示二进制数据的表示方法。由于 2^6=64 ,所以每6个比特为一个单元,对应某个可打印字符。3个字节有24个比特,对应于4个Base64单元,即3个字节可由4个可打印字符来表示。

  • base64编码简介_#include

  • base64编码简介_字节数_02

  • 如果要编码的字节数不能被3整除,最后会多出1个或2个字节,那么可以使用下面的方法进行处理:先使用0字节值在末尾补足,使其能够被3整除,然后再进行Base64的编码。在编码后的Base64文本后加上一个或两个=号,代表补足的字节数。也就是说,当最后剩余两个八位字节(2个byte)时,最后一个6位的Base64字节块有四位是0值,最后附加上两个等号;如果最后剩余一个八位字节(1个byte)时,最后一个6位的base字节块有两位是0值,最后附加一个等号。 参考下表:
  • base64编码简介_sed_03

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


#include <iostream>
#include <string>
#include <unistd.h>

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, 0x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 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.coding\n2.decoding\noperating(1 or 2):"<<ends;
char input;
string s;
Base64 a;
cin >> input;
if (input == '1')
{
cout << "Enter the string to be encrypted:" << endl;
cin >> s;
a.Encode(s);
cout << "Encoding result:\n" << s << endl;
}
else if (input == '2')
{
cout << "Enter the string to be decrypted:" << endl;
cin >> s;
a.Decode(s);
cout << "Decoded result:\n" << s << endl;
}
else
cout << "input error" << endl;

pause();
return 0;
}

base64编码简介_字节数_04