⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 base64.cpp

📁 对字符串进行base64编码和解码; 在http传输中
💻 CPP
字号:

//////////////////////////////////////////////////////////////////////
#include "Base64.h"
#include <windows.h>

// 用于编码的字符
const CString CBase64::_base64_encode_chars = 
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

// 用于解码的字符
const char CBase64::_base64_decode_chars[] = 
{
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
	52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
	-1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
	15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
	-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
	41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1
};


//////////////////////////////////////////////////////////////////////////
// 函数:    CString encode()
// 功能:    将ANSI字符串转成Base64字符串
// 参数:    in_str ANSI字符串
// 返回值:  CString Base64字符串
// 日期:    [6/23/2005]
//////////////////////////////////////////////////////////////////////////
CString CBase64::encode(const CString in_str)
{
	CString out_str;
	unsigned char c1, c2, c3;
	int i = 0;
	int len = in_str.GetLength();

	while ( i<len )
	{
		// read the first byte
		c1 = in_str[i++];
		if ( i==len )       // pad with "="
		{
			out_str += _base64_encode_chars[ c1>>2 ];
			out_str += _base64_encode_chars[ (c1&0x3)<<4 ];
			out_str += "==";
			break;
		}

		// read the second byte
		c2 = in_str[i++];
		if ( i==len )       // pad with "="
		{
			out_str += _base64_encode_chars[ c1>>2 ];
			out_str += _base64_encode_chars[ ((c1&0x3)<<4) | ((c2&0xF0)>>4) ];
			out_str += _base64_encode_chars[ (c2&0xF)<<2 ];
			out_str += "=";
			break;
		}

		// read the third byte
		c3 = in_str[i++];
		// convert into four bytes string
		out_str += _base64_encode_chars[ c1>>2 ];
		out_str += _base64_encode_chars[ ((c1&0x3)<<4) | ((c2&0xF0)>>4) ];
		out_str += _base64_encode_chars[ ((c2&0xF)<<2) | ((c3&0xC0)>>6) ];
		out_str += _base64_encode_chars[ c3&0x3F ];
	}

	return out_str;
}


//////////////////////////////////////////////////////////////////////////
// 函数:    CString decode()
// 功能:    将Base64字符串转成ANSI字符串
// 参数:    in_str Base64字符串
// 返回值:  CString ANSI字符串
// 日期:    [6/23/2005]
//////////////////////////////////////////////////////////////////////////
CString CBase64::decode(const CString in_str)
{
	CString out_str;
	char c1, c2, c3, c4;
	int i = 0;
	int len = in_str.GetLength();

	while ( i<len)
	{
		// read the first byte
		do {
			c1 = _base64_decode_chars[ in_str[i++] ];
		} while ( i<len && c1==-1);

		if ( c1==-1)
			break;

		// read the second byte
		do {
			c2 = _base64_decode_chars[ in_str[i++] ];
		} while ( i<len && c2==-1);

		if ( c2==-1 )
			break;

		// assamble the first byte
		out_str += char( (c1<<2) | ((c2&0x30)>>4) );

		// read the third byte
		do {
			c3 = in_str[i++];
			if ( c3==61 )       // meet with "=", break
				return out_str;
			c3 = _base64_decode_chars[ c3 ];
		} while ( i<len && c3==-1);

		if ( c3==-1 )
			break;

		// assamble the second byte
		out_str += char( ((c2&0XF)<<4) | ((c3&0x3C)>>2) );

		// read the fourth byte
		do {
			c4 = in_str[i++];
			if ( c4==61 )       // meet with "=", break
				return out_str;
			c4 = _base64_decode_chars[ c4 ];
		} while ( i<len && c4==-1 );

		if ( c4==-1 )
			break;

		// assamble the third byte
		out_str += char( ((c3&0x03)<<6) | c4 );
	}

	return out_str;
}


//////////////////////////////////////////////////////////////////////////
// 函数:    void encode()
// 功能:    将ANSI字符串转成Base64字符串
// 参数:    pIn  ANSI字符串
//   dwInLen ANSI字符串的长度
//   pOut 放Base64字符串的内存
//   pdwOutLen Base64字符串的长度
// 返回值:  void
// 日期:    [6/24/2005]
//////////////////////////////////////////////////////////////////////////
void CBase64::encode(LPSTR pIn, DWORD dwInLen, LPSTR pOut, LPDWORD pdwOutLen)
{
	unsigned char c1, c2, c3;
	int i = 0, n = 0;
	int len = dwInLen;

	while ( i<len )
	{
		// read the first byte
		c1 = pIn[i++];
		if ( i==len )       // pad with "="
		{
			pOut[n++] = _base64_encode_chars[ c1>>2 ];
			pOut[n++] = _base64_encode_chars[ (c1&0x3)<<4 ];
			pOut[n++] = '=';
			pOut[n++] = '=';
			break;
		}

		// read the second byte
		c2 = pIn[i++];
		if ( i==len )       // pad with "="
		{
			pOut[n++] = _base64_encode_chars[ c1>>2 ];
			pOut[n++] = _base64_encode_chars[ ((c1&0x3)<<4) | ((c2&0xF0)>>4) ];
			pOut[n++] = _base64_encode_chars[ (c2&0xF)<<2 ];
			pOut[n++] = '=';
			break;
		}

		// read the third byte
		c3 = pIn[i++];
		// convert into four bytes string
		pOut[n++] = _base64_encode_chars[ c1>>2 ];
		pOut[n++] = _base64_encode_chars[ ((c1&0x3)<<4) | ((c2&0xF0)>>4) ];
		pOut[n++] = _base64_encode_chars[ ((c2&0xF)<<2) | ((c3&0xC0)>>6) ];
		pOut[n++] = _base64_encode_chars[ c3&0x3F ];
	}
	*pdwOutLen = n;
}


//////////////////////////////////////////////////////////////////////////
// 函数:    void decode()
// 功能:    将Base64字符串转成ANSI字符串
// 参数:    pIn  Base64字符串
//   dwInLen Base64字符串的长度
//   pOut 放ANSI字符串的内存
//   pdwOutLen ANSI字符串的长度
// 返回值:  void
// 日期:    [6/24/2005]
//////////////////////////////////////////////////////////////////////////
void CBase64::decode(LPSTR pIn, DWORD dwInLen, LPSTR pOut, LPDWORD pdwOutLen)
{
	char c1, c2, c3, c4;
	int i = 0, n = 0;
	int len = dwInLen;

	while ( i<len)
	{
		// read the first byte
		do {
			c1 = _base64_decode_chars[ pIn[i++] ];
		} while ( i<len && c1==-1);

		if ( c1==-1)
			break;

		// read the second byte
		do {
			c2 = _base64_decode_chars[ pIn[i++] ];
		} while ( i<len && c2==-1);

		if ( c2==-1 )
			break;

		// assamble the first byte
		pOut[n++] = char( (c1<<2) | ((c2&0x30)>>4) );

		// read the third byte
		do {
			c3 = pIn[i++];
			if ( c3==61 )       // meet with "=", break
				goto end; //return;
			c3 = _base64_decode_chars[ c3 ];
		} while ( i<len && c3==-1);

		if ( c3==-1 )
			break;

		// assamble the second byte
		pOut[n++] = char( ((c2&0XF)<<4) | ((c3&0x3C)>>2) );

		// read the fourth byte
		do {
			c4 = pIn[i++];
			if ( c4==61 )       // meet with "=", break
				goto end; //return;
			c4 = _base64_decode_chars[ c4 ];
		} while ( i<len && c4==-1 );

		if ( c4==-1 )
			break;

		// assamble the third byte
		pOut[n++] = char( ((c3&0x03)<<6) | c4 );
	}
end:
	*pdwOutLen = n;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -