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

📄 base64.cpp

📁 几个封装完好的常用算法,md5,des,base64,crc32
💻 CPP
字号:
//---------------------------------------------------------------------------

#pragma hdrstop

#include "Base64.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)
static const unsigned char vec[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const unsigned char padding = '=';

//---------------------------------------------------------------------------
void TBase64Buffer::Encode(unsigned char * in,int len,char* out)
{
        int d = len / 3;
        int m = len % 3;
        if(m != 0) d++;
        int buflen = d * 3;
        unsigned char * buf = new char[buflen];
        memset(buf,0,buflen);
        memcpy(buf,in,len);

        int j = 0;
        for(int i = 0,k = 0;i < d;i++,k += 3)
        {
	        unsigned char ch;

	        ch = (unsigned char)((buf[k] & 0xFC) >> 2);
	        out[j] = vec[ch];
                j++;
        	ch = (unsigned char)(((buf[k] & 0x03) << 4) | (buf[k + 1] >> 4));
	        out[j] = vec[ch];
                j++;
        	ch = (unsigned char)(((buf[k + 1] & 0x0F) << 2) | (buf[k + 2] >> 6));
	        out[j] = vec[ch];
                j++;
	        ch = (unsigned char)(buf[k + 2] & 0x3F);
	        out[j] = vec[ch];
                j++;
        }
        out[j] = 0;
        delete[] buf;
}
//---------------------------------------------------------------------------
void TBase64Buffer::Decode(unsigned char* in,int len,unsigned char* out)
{
        int loop = len / 4;
        unsigned char index[4];
        for(int i = 0,j = 0,k = 0;i < loop;i++,k += 4)
        {
                index[0] = ConvToNumber(in[k]);
                index[1] = ConvToNumber(in[k + 1]);
                index[2] = ConvToNumber(in[k + 2]);
                index[3] = ConvToNumber(in[k + 3]);

	        unsigned char ch;
	        ch = (unsigned char)((index[0] << 2) | (index[1] >> 4));
	        out[j]  = ch;
                j++;
	        ch = (unsigned char)((index[1] << 4) | (index[2] >> 2));
	        out[j]  = ch;
                j++;
	        ch = (unsigned char)((index[2] << 6) | index[3]);
	        out[j]  = ch;
                j++;
        }
}
//---------------------------------------------------------------------------
int TBase64Buffer::GetEncodeOutLen(int inLen)
{
        int d = inLen / 3;
        int m = inLen % 3;
        if(m != 0) d++;
        return d * 4 + 1;
}
//---------------------------------------------------------------------------
int TBase64Buffer::GetDecodeOutLen(int inLen)
{
        return (inLen / 4) * 3; 
}
//---------------------------------------------------------------------------
int TBase64Buffer::ConvToNumber(unsigned char inByte)
{
	if (inByte >= 'A' && inByte <= 'Z')
		return (inByte - 'A');

	if (inByte >= 'a' && inByte <= 'z')
		return (inByte - 'a' + 26);

	if (inByte >= '0' && inByte <= '9')
		return (inByte - '0' + 52);

	if (inByte == '+')
		return (62);

	if (inByte == '/')
		return (63);

	return (-1);
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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