MD5加密算法及BASE64编码

1、MD5头文件(使用时只需include该文件即可)

// MD5.h
//
#ifndef _MD5_H_
#define _MD5_H_
 class CMD5 { public: CMD5(); /* * 功 能:计算输入数据的MD5哈希值 * 参 数:pbInputBuffer:输入数据 * uiInputCount:输入数据长度(字节数) * pbOutputBuffer:输入数据的哈希值 * 返回值:哈希值的有效长度(字节数) */ int MD5_Hash(IN const BYTE* pbInputBuffer, IN UINT uiInputCount, OUT BYTE* pbOutputBuffer); /* * 功 能:计算输入数据的HMAC-MD5哈希值 * 参 数:pbInputBuffer:输入数据 * uiInputCount:输入数据长度(字节数) * pbUserKey:用户密钥 * uiUserKeyLen:用户密钥长度 * pbOutputBuffer:输入数据的哈希值 * 返回值:哈希值的有效长度(字节数) */ int HMAC_MD5_Hash(IN const BYTE* pbInputBuffer, IN UINT uiInputCount, IN const BYTE* pbUserKey, IN UINT uiUserKeyLen, OUT BYTE* pbOutputBuffer); /* * 功 能:计算输入数据的MD5哈希值,并转换为BASE64编码字符串输出。 * 参 数:pbInputBuffer:输入数据 * uiInputCount:输入数据长度(字节数) * pbOutputBuffer:MD5哈希值的BASE64编码字符串 * 返回值:BASE64编码字符串长度(字符数),不包括字符串结束符 */ int MD5_BASE64(IN const BYTE* pbInputBuffer, IN UINT uiInputCount, OUT TCHAR* pbOutputBuffer); /* * 功 能:计算输入数据的HMAC-MD5哈希值,并转换为BASE64编码字符串输出。 * 参 数:pbInputBuffer:输入数据 * uiInputCount:输入数据长度(字节数) * pbUserKey:用户密钥 * uiUserKeyLen:用户密钥长度 * pbOutputBuffer:HMAC-MD5哈希值的BASE64编码字符串 * 返回值:BASE64编码字符串长度(字符数),不包括字符串结束符 */ int HMAC_MD5_BASE64(IN const BYTE* pbInputBuffer, IN UINT uiInputCount, IN const BYTE* pbUserKey, IN UINT uiUserKeyLen, OUT TCHAR* pbOutputBuffer); private: struct st_Md5Data_t { DWORD dwaP[4]; DWORD dwaQ[4][16]; }; struct st_MD5CTX_t { DWORD dwState[4]; // 记录数据的变化状态 DWORD dwCount[2]; // 记录数据的原始长度(以bit为单位) BYTE baBuffer[64]; // 原始数据 }; private: void _MD5_Init(st_MD5CTX_t& pstruContext); void _MD5_Transform(PDWORD pdwState, PDWORD pdwX); void _MD5_Update(st_MD5CTX_t& stContext, const BYTE* pbInput, DWORD dwInputLen); void _MD5_Final(st_MD5CTX_t& stContext); private: // MD5关键参数,修改即可形成不同的变体 st_Md5Data_t m_stMd5Arguments; BYTE m_bMd5ShiftTable[4][4]; // 变换操作移位表 BYTE m_bMd5Padding[64]; // 填充数据 }; #endif // !_MD5_H_

2、BASE64头文件

// Base64.h
// 
#ifndef _BASE64_H_
#define _BASE64_H_

#include <windows.h>

class CBase64 {
public:
    CBase64();
    /* * 功 能:将二进制数据转换成BASE64编码字符串 * 参 数:pbInputBuffer:要编码的二进制数据 * iInputCount:数据长度 * ptsOutputBuffer:存储转换后的BASE64编码字符串 * 返回值:-1:参数错误 * >=0:有效编码长度(字符数),不包括字符串结束符。 */
    int Encode(IN const BYTE* pbInputBuffer,
        IN int iInputCount, OUT TCHAR* ptsOutputBuffer);

    /* * 功 能:将BASE64编码字符串转换为二进制数据 * 参 数:pbInputBuffer:BASE64编码字符串 * iInputCount:编码长度(字符数),应该为4的倍数。 * ptsOutputBuffer:存储转换后的二进制数据 * 返回值:-1:参数错误 * -2:数据错误 * >=0:转换后的字节数 */
    int Decode(IN const TCHAR* pbInputBuffer,
        IN int iInputCount, OUT BYTE* ptsOutputBuffer );

private:
    char m_szBIN2ASCII[65];
    BYTE m_bASCII2BIN[128];
};

#endif //!_BASE64_H_

3、MD5与BASE64相关的宏定义文件Md5Base64Macro.h

// Md5Base64Macro.h
#ifndef _MD5_BASE64_MACRO_H_
#define _MD5_BASE64_MACRO_H_

//////////////////////////////////////////////////////////////////////////
// Base64 相关宏定义
//////////////////////////////////////////////////////////////////////////
#define __Base64_EOLN 0xF0 // 换行/n 
#define __Base64_CR 0xF1 // 回车/r 
#define __Base64_EOF 0xF2 // 连字符- 
#define __Base64_WS 0xE0 // 跳格或者空格(/t、space) 
#define __Base64_ERROR 0xFF // 错误字符 
#define __Base64_NOT_BASE64(a) (((a)|0x13) == 0xF3)
#define __Base64_BIN2ASCIIString "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

//////////////////////////////////////////////////////////////////////////
// MD5 相关宏定义
//////////////////////////////////////////////////////////////////////////
// MD5基本位操作函数
#define __MD5_F(x, y, z)(((x) & (y)) | ((~x) & (z)))
#define __MD5_G(x, y, z)(((x) & (z)) | ((y) & (~z)))
#define __MD5_H(x, y, z)((x) ^ (y) ^ (z))
#define __MD5_I(x, y, z)((y) ^ ((x) | (~z)))
// 位循环左移位操作
#define __MD5_ROL(x, n) (((x) << (n)) | ((x) >> (32-(n))))
// ---------变换操作----------
// 第一轮变换基本操作
#define __MD5_FF(a, b, c, d, x, s, t) { \
    (a) += __MD5_F((b), (c), (d)) + (x) + (t); \
    (a) = __MD5_ROL((a), (s)); \
    (a) += (b); \
}
// 第二轮变换基本操作
#define __MD5_GG(a, b, c, d, x, s, t) { \
    (a) += __MD5_G((b), (c), (d)) + (x) + (t); \
    (a) = __MD5_ROL((a), (s)); \
    (a) += (b); \
}
// 第三轮变换基本操作
#define __MD5_HH(a, b, c, d, x, s, t) { \
    (a) += __MD5_H((b), (c), (d)) + (x) + (t); \
    (a) = __MD5_ROL((a), (s)); \
    (a) += (b); \
}
// 第四轮变换基本操作
#define __MD5_II(a, b, c, d, x, s, t) { \
    (a) += __MD5_I((b), (c), (d)) + (x) + (t); \
    (a) = __MD5_ROL((a), (s)); \
    (a) += (b); \
}

#endif // !_MD5_BASE64_MACRO_H_

4、MD5源文件

// MD5.cpp
// 
#include "MD5.h"
#include "Base64.h"
#include "Md5Base64Macro.h"

int CMD5::MD5_Hash(const BYTE* pbInputBuffer,
    UINT uiInputCount, BYTE* pbOutputBuffer )
{
    st_MD5CTX_t stContext;

    if( pbInputBuffer == NULL )
    {
        uiInputCount = 0;
    }
    // 进行MD5变换  
    _MD5_Init(stContext);
    _MD5_Update(stContext, pbInputBuffer, uiInputCount);
    _MD5_Final(stContext);
    // 获取哈希值  
    if( pbOutputBuffer != NULL )
    {
        memcpy( pbOutputBuffer, stContext.dwState, 16 );
    }   
    return 16;
}

int CMD5::HMAC_MD5_Hash(const BYTE* pbInputBuffer,
    UINT uiInputCount, const BYTE* pbUserKey,
    UINT uiUserKeyLen, BYTE* pbOutputBuffer)
{   
    BYTE baHmacKey[64] = {0};
    BYTE baKipad[64];
    BYTE baKopad[64];
    st_MD5CTX_t stContext;

    if( pbInputBuffer == NULL )
    {
        uiInputCount = 0;
    }

    if( pbUserKey == NULL )
    {
        uiUserKeyLen = 0;
    }
    // 保证密钥长度不超过64字节  
    if( uiUserKeyLen > 64 )
    {
        MD5_Hash(pbUserKey, uiUserKeyLen, baHmacKey);
    }
    else
    {
        memcpy(baHmacKey, pbUserKey, uiUserKeyLen);
    }

    for( UINT i = 0; i < 64; i++ )
    {
        baKipad[i] = baHmacKey[i] ^ 0x36;
        baKopad[i] = baHmacKey[i] ^ 0x5C;
    }
    // 内圈MD5运算  
    _MD5_Init(stContext);
    _MD5_Update(stContext, baKipad, 64);
    _MD5_Update(stContext, pbInputBuffer, uiInputCount);
    _MD5_Final(stContext);
    memcpy(baHmacKey, stContext.dwState, 16);
    // 外圈MD5运算  
    _MD5_Init(stContext);
    _MD5_Update(stContext, baKopad, 64);
    _MD5_Update(stContext, baHmacKey, 16);
    _MD5_Final(stContext);
    // 获取哈希值  
    if( pbOutputBuffer != NULL )
    {
        memcpy(pbOutputBuffer, stContext.dwState, 16);
    }

    return 16;
}

int CMD5::MD5_BASE64(const BYTE* pbInputBuffer,
    UINT uiInputCount, TCHAR* pbOutputBuffer)
{
    BYTE baHash[16];
    int iByteNum;
    // 计算输入串MD5的哈希值
    iByteNum = MD5_Hash(pbInputBuffer, uiInputCount, baHash);
    // 将哈希值转换成BASE64编码
    CBase64 base64;
    return base64.Encode(baHash, iByteNum, pbOutputBuffer);
}

int CMD5::HMAC_MD5_BASE64(const BYTE* pbInputBuffer,
    UINT uiInputCount, const BYTE* pbUserKey,
    UINT uiUserKeyLen, TCHAR* pbOutputBuffer)
{
    BYTE baHash[16];
    int iByteNum;
    // 计算输入串HMAC-MD5的哈希值
    iByteNum = HMAC_MD5_Hash(pbInputBuffer, uiInputCount,
        pbUserKey, uiUserKeyLen, baHash );
    // 将哈希值转换成BASE64编码
    CBase64 base64;
    return base64.Encode(baHash, iByteNum, pbOutputBuffer);
}

CMD5::CMD5()
{
    st_Md5Data_t stMd5Data = {
        {
            0x67452301,0xEFCDAB89,0x98BADCFE,0x10325476
        },  
        {// 变换操作偏移量表
            0xD76AA478,0xE8C7B756,0x242070DB,0xC1BDCEEE,  
            0xF57C0FAF,0x4787C62A,0xA8304613,0xFD469501,  
            0x698098D8,0x8B44F7AF,0xFFFF5BB1,0x895CD7BE,  
            0x6B901122,0xFD987193,0xA679438E,0x49B40821,  

            0xF61E2562,0xC040B340,0x265E5A51,0xE9B6C7AA,  
            0xD62F105D,0x02441453,0xD8A1E681,0xE7D3FBC8,  
            0x21E1CDE6,0xC33707D6,0xF4D50D87,0x455A14ED,  
            0xA9E3E905,0xFCEFA3F8,0x676F02D9,0x8D2A4C8A,  

            0xFFFA3942,0x8771F681,0x6D9D6122,0xFDE5380C,  
            0xA4BEEA44,0x4BDECFA9,0xF6BB4B60,0xBEBFBC70,  
            0x289B7EC6,0xEAA127FA,0xD4EF3085,0x04881D05,  
            0xD9D4D039,0xE6DB99E5,0x1FA27CF8,0xC4AC5665,  

            0xF4292244,0x432AFF97,0xAB9423A7,0xFC93A039,  
            0x655B59C3,0x8F0CCC92,0xFFEFF47D,0x85845DD1,  
            0x6FA87E4F,0xFE2CE6E0,0xA3014314,0x4E0811A1,  
            0xF7537E82,0xBD3AF235,0x2AD7D2BB,0xEB86D391
            }
    };
    memcpy(&m_stMd5Arguments, &stMd5Data, sizeof(st_Md5Data_t));

    BYTE bMd5ShiftTable[][4] = {
        {7,12,17,22},{5,9,14,20},
        {4,11,16,23},{6,10,15,21}
    };
    memcpy(m_bMd5ShiftTable, bMd5ShiftTable, sizeof(m_bMd5ShiftTable));

    BYTE bMd5Padding[] = {
        0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    };
    memcpy(m_bMd5Padding, bMd5Padding, sizeof(m_bMd5Padding));
}

void CMD5::_MD5_Init( st_MD5CTX_t& pstruContext )
{
    const DWORD *pdwOriginState = m_stMd5Arguments.dwaP;
    pstruContext.dwState[0] = pdwOriginState[0];
    pstruContext.dwState[1] = pdwOriginState[1];
    pstruContext.dwState[2] = pdwOriginState[2];
    pstruContext.dwState[3] = pdwOriginState[3];
    pstruContext.dwCount[0] = pstruContext.dwCount[1] = 0;
}

void CMD5::_MD5_Transform(PDWORD pState, PDWORD px)
{
    const DWORD (*MDOffTable)[16];
    DWORD a,b,c,d;

    MDOffTable = m_stMd5Arguments.dwaQ;
    a = pState[0], b = pState[1], c = pState[2], d = pState[3];

    // 第一轮变换  
    __MD5_FF(a, b, c, d, px[ 0], m_bMd5ShiftTable[0][0], MDOffTable[0][ 0]);
    __MD5_FF(d, a, b, c, px[ 1], m_bMd5ShiftTable[0][1], MDOffTable[0][ 1]);
    __MD5_FF(c, d, a, b, px[ 2], m_bMd5ShiftTable[0][2], MDOffTable[0][ 2]);
    __MD5_FF(b, c, d, a, px[ 3], m_bMd5ShiftTable[0][3], MDOffTable[0][ 3]);
    __MD5_FF(a, b, c, d, px[ 4], m_bMd5ShiftTable[0][0], MDOffTable[0][ 4]);
    __MD5_FF(d, a, b, c, px[ 5], m_bMd5ShiftTable[0][1], MDOffTable[0][ 5]);
    __MD5_FF(c, d, a, b, px[ 6], m_bMd5ShiftTable[0][2], MDOffTable[0][ 6]);
    __MD5_FF(b, c, d, a, px[ 7], m_bMd5ShiftTable[0][3], MDOffTable[0][ 7]);
    __MD5_FF(a, b, c, d, px[ 8], m_bMd5ShiftTable[0][0], MDOffTable[0][ 8]);
    __MD5_FF(d, a, b, c, px[ 9], m_bMd5ShiftTable[0][1], MDOffTable[0][ 9]);
    __MD5_FF(c, d, a, b, px[10], m_bMd5ShiftTable[0][2], MDOffTable[0][10]);
    __MD5_FF(b, c, d, a, px[11], m_bMd5ShiftTable[0][3], MDOffTable[0][11]);
    __MD5_FF(a, b, c, d, px[12], m_bMd5ShiftTable[0][0], MDOffTable[0][12]);
    __MD5_FF(d, a, b, c, px[13], m_bMd5ShiftTable[0][1], MDOffTable[0][13]);
    __MD5_FF(c, d, a, b, px[14], m_bMd5ShiftTable[0][2], MDOffTable[0][14]);
    __MD5_FF(b, c, d, a, px[15], m_bMd5ShiftTable[0][3], MDOffTable[0][15]);

    // 第二轮变换  
    __MD5_GG(a, b, c, d, px[ 1], m_bMd5ShiftTable[1][0], MDOffTable[1][ 0]);
    __MD5_GG(d, a, b, c, px[ 6], m_bMd5ShiftTable[1][1], MDOffTable[1][ 1]);
    __MD5_GG(c, d, a, b, px[11], m_bMd5ShiftTable[1][2], MDOffTable[1][ 2]);
    __MD5_GG(b, c, d, a, px[ 0], m_bMd5ShiftTable[1][3], MDOffTable[1][ 3]);
    __MD5_GG(a, b, c, d, px[ 5], m_bMd5ShiftTable[1][0], MDOffTable[1][ 4]);
    __MD5_GG(d, a, b, c, px[10], m_bMd5ShiftTable[1][1], MDOffTable[1][ 5]);
    __MD5_GG(c, d, a, b, px[15], m_bMd5ShiftTable[1][2], MDOffTable[1][ 6]);
    __MD5_GG(b, c, d, a, px[ 4], m_bMd5ShiftTable[1][3], MDOffTable[1][ 7]);
    __MD5_GG(a, b, c, d, px[ 9], m_bMd5ShiftTable[1][0], MDOffTable[1][ 8]);
    __MD5_GG(d, a, b, c, px[14], m_bMd5ShiftTable[1][1], MDOffTable[1][ 9]);
    __MD5_GG(c, d, a, b, px[ 3], m_bMd5ShiftTable[1][2], MDOffTable[1][10]);
    __MD5_GG(b, c, d, a, px[ 8], m_bMd5ShiftTable[1][3], MDOffTable[1][11]);
    __MD5_GG(a, b, c, d, px[13], m_bMd5ShiftTable[1][0], MDOffTable[1][12]);
    __MD5_GG(d, a, b, c, px[ 2], m_bMd5ShiftTable[1][1], MDOffTable[1][13]);
    __MD5_GG(c, d, a, b, px[ 7], m_bMd5ShiftTable[1][2], MDOffTable[1][14]);
    __MD5_GG(b, c, d, a, px[12], m_bMd5ShiftTable[1][3], MDOffTable[1][15]);

    // 第三轮变换      
    __MD5_HH(a, b, c, d, px[ 5], m_bMd5ShiftTable[2][0], MDOffTable[2][ 0]);
    __MD5_HH(d, a, b, c, px[ 8], m_bMd5ShiftTable[2][1], MDOffTable[2][ 1]);
    __MD5_HH(c, d, a, b, px[11], m_bMd5ShiftTable[2][2], MDOffTable[2][ 2]);
    __MD5_HH(b, c, d, a, px[14], m_bMd5ShiftTable[2][3], MDOffTable[2][ 3]);
    __MD5_HH(a, b, c, d, px[ 1], m_bMd5ShiftTable[2][0], MDOffTable[2][ 4]);
    __MD5_HH(d, a, b, c, px[ 4], m_bMd5ShiftTable[2][1], MDOffTable[2][ 5]);
    __MD5_HH(c, d, a, b, px[ 7], m_bMd5ShiftTable[2][2], MDOffTable[2][ 6]);
    __MD5_HH(b, c, d, a, px[10], m_bMd5ShiftTable[2][3], MDOffTable[2][ 7]);
    __MD5_HH(a, b, c, d, px[13], m_bMd5ShiftTable[2][0], MDOffTable[2][ 8]);
    __MD5_HH(d, a, b, c, px[ 0], m_bMd5ShiftTable[2][1], MDOffTable[2][ 9]);
    __MD5_HH(c, d, a, b, px[ 3], m_bMd5ShiftTable[2][2], MDOffTable[2][10]);
    __MD5_HH(b, c, d, a, px[ 6], m_bMd5ShiftTable[2][3], MDOffTable[2][11]);
    __MD5_HH(a, b, c, d, px[ 9], m_bMd5ShiftTable[2][0], MDOffTable[2][12]);
    __MD5_HH(d, a, b, c, px[12], m_bMd5ShiftTable[2][1], MDOffTable[2][13]);
    __MD5_HH(c, d, a, b, px[15], m_bMd5ShiftTable[2][2], MDOffTable[2][14]);
    __MD5_HH(b, c, d, a, px[ 2], m_bMd5ShiftTable[2][3], MDOffTable[2][15]);

    // 第四轮变换      
    __MD5_II(a, b, c, d, px[ 0], m_bMd5ShiftTable[3][0], MDOffTable[3][ 0]);
    __MD5_II(d, a, b, c, px[ 7], m_bMd5ShiftTable[3][1], MDOffTable[3][ 1]);
    __MD5_II(c, d, a, b, px[14], m_bMd5ShiftTable[3][2], MDOffTable[3][ 2]);
    __MD5_II(b, c, d, a, px[ 5], m_bMd5ShiftTable[3][3], MDOffTable[3][ 3]);
    __MD5_II(a, b, c, d, px[12], m_bMd5ShiftTable[3][0], MDOffTable[3][ 4]);
    __MD5_II(d, a, b, c, px[ 3], m_bMd5ShiftTable[3][1], MDOffTable[3][ 5]);
    __MD5_II(c, d, a, b, px[10], m_bMd5ShiftTable[3][2], MDOffTable[3][ 6]);
    __MD5_II(b, c, d, a, px[ 1], m_bMd5ShiftTable[3][3], MDOffTable[3][ 7]);
    __MD5_II(a, b, c, d, px[ 8], m_bMd5ShiftTable[3][0], MDOffTable[3][ 8]);
    __MD5_II(d, a, b, c, px[15], m_bMd5ShiftTable[3][1], MDOffTable[3][ 9]);
    __MD5_II(c, d, a, b, px[ 6], m_bMd5ShiftTable[3][2], MDOffTable[3][10]);
    __MD5_II(b, c, d, a, px[13], m_bMd5ShiftTable[3][3], MDOffTable[3][11]);
    __MD5_II(a, b, c, d, px[ 4], m_bMd5ShiftTable[3][0], MDOffTable[3][12]);
    __MD5_II(d, a, b, c, px[11], m_bMd5ShiftTable[3][1], MDOffTable[3][13]);
    __MD5_II(c, d, a, b, px[ 2], m_bMd5ShiftTable[3][2], MDOffTable[3][14]);
    __MD5_II(b, c, d, a, px[ 9], m_bMd5ShiftTable[3][3], MDOffTable[3][15]); 

    pState[0] += a;
    pState[1] += b;
    pState[2] += c;
    pState[3] += d;
}

void CMD5::_MD5_Update(st_MD5CTX_t& stContext,
    const BYTE* pbInput, DWORD dwInputLen )
{
    DWORD i, dwIndex, dwPartLen, dwBitsNum;
    // 计算 mod 64 的字节数  
    dwIndex = (stContext.dwCount[0] >> 3) & 0x3F;
    // 更新数据位数  
    dwBitsNum = dwInputLen << 3;
    stContext.dwCount[0] += dwBitsNum;

    if(stContext.dwCount[0] < dwBitsNum)
    {
        stContext.dwCount[1]++;
    }

    stContext.dwCount[1] += dwInputLen >> 29;

    dwPartLen = 64 - dwIndex;
    if( dwInputLen < dwPartLen )
    {
        i = 0;
    }
    else  
    {
        memcpy( stContext.baBuffer+dwIndex, pbInput, dwPartLen );
        _MD5_Transform( stContext.dwState, (DWORD*)stContext.baBuffer );

        for(i = dwPartLen; i + 63 < dwInputLen; i += 64 )
        {
            _MD5_Transform( stContext.dwState, (DWORD*)(pbInput + i) );
        }

        dwIndex = 0;
    }

    memcpy( stContext.baBuffer + dwIndex, pbInput + i, dwInputLen - i );
}

void CMD5::_MD5_Final(st_MD5CTX_t& stContext)
{
    DWORD dwIndex, dwPadLen;
    BYTE pBits[8];

    memcpy( pBits, stContext.dwCount, 8 );
    // 计算 mod 64 的字节数  
    dwIndex = (stContext.dwCount[0] >> 3) & 0x3F;
    // 使长度满足K*64+56个字节  
 dwPadLen = (dwIndex < 56) ? (56-dwIndex) : (120-dwIndex);
    _MD5_Update(stContext, m_bMd5Padding, dwPadLen);
    _MD5_Update(stContext, pBits, 8);
}

5、BASE64源文件

// Base64.cpp
//
#include "Base64.h"
#include "Md5Base64Macro.h"

CBase64::CBase64()
{
    strcpy_s(m_szBIN2ASCII, strlen(__Base64_BIN2ASCIIString)+1,
        __Base64_BIN2ASCIIString);

    BYTE bASCII2BIN[128] = {
        0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
        0xFF,0xE0,0xF0,0xFF,0xFF,0xF1,0xFF,0xFF,
        0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
        0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
        0xE0,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
        0xFF,0xFF,0xFF,0x3E,0xFF,0xF2,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  
    };
    memcpy(m_bASCII2BIN, bASCII2BIN, 128);
}

int CBase64::Encode(const BYTE* pbInputBuffer, int iInputCount,
    TCHAR* ptsOutputBuffer)
{
    int i;
    BYTE b0, b1, b2;

    if( (pbInputBuffer == NULL) || (iInputCount < 0) )
    {
        return -1;
    }

    if( ptsOutputBuffer == NULL )
    {// 返回有效字符个数
        return ((iInputCount + 2) / 3) * 4;
    }

    for( i = iInputCount; i > 0; i -= 3 )
    {
        if( i >= 3 )
        { // 将3字节数据转换成4个ASCII字符
            b0 = *pbInputBuffer++;
            b1 = *pbInputBuffer++;
            b2 = *pbInputBuffer++;

            *ptsOutputBuffer++ = m_szBIN2ASCII[b0 >> 2];
            *ptsOutputBuffer++ = m_szBIN2ASCII[
                ((b0 << 4) | (b1 >> 4)) & 0x3F];
            *ptsOutputBuffer++ = m_szBIN2ASCII[
                ((b1 << 2) | (b2 >> 6)) & 0x3F];
            *ptsOutputBuffer++ = m_szBIN2ASCII[b2 & 0x3F];
        }
        else  
        {
            b0 = *pbInputBuffer++;
            if( i == 2 )b1 = *pbInputBuffer++; else b1 = 0;

            *ptsOutputBuffer++ = m_szBIN2ASCII[b0 >> 2];
            *ptsOutputBuffer++ = m_szBIN2ASCII[((b0 << 4) | (b1 >> 4)) & 0x3F];
            *ptsOutputBuffer++ = (i == 1) ? TEXT('=') : m_szBIN2ASCII[(b1 << 2) & 0x3F];
            *ptsOutputBuffer++ = TEXT('=');
        }
    } // End for i 
    // 添加字符串结束标记
    *ptsOutputBuffer++ = TEXT('\0');

    return ((iInputCount + 2) / 3) * 4; // 返回有效字符个数
}

int CBase64::Decode(const TCHAR* pbInputBuffer,
    int iInputCount, BYTE* pbOutputBuffer )
{
    int i, j;
    BYTE b[4];
    TCHAR ch;

    if( (pbInputBuffer == NULL) || (iInputCount < 0) )
    {// 参数错误
        return -1; 
    }

    // 去除头部空白字符 
    while( iInputCount > 0 )
    {
        ch = *pbInputBuffer;
        if( (ch < 0) || (ch >= 0x80) )
        {// 数据错误,不在ASCII字符编码范围内
            return -2;
        }

        if( m_bASCII2BIN[ch] != __Base64_WS )
        {
            break;

        }

        pbInputBuffer++;
        iInputCount--;
    }

    // 去除尾部的空白字符、回车换行字符、连字符 
    while( iInputCount >= 4 )
    {
        ch = pbInputBuffer[iInputCount - 1];
        if( (ch < 0) || (ch >= 0x80) )
        {
            return -2;// 数据错误,不在ASCII字符编码范围内 
        }
        else  
        {
            if( __Base64_NOT_BASE64(m_bASCII2BIN[ch]) )
            {
                iInputCount--;
            }
            else  
            {
                break;
            }
        }
    }

    // 字符串长度必须为4的倍数 
    if( (iInputCount % 4) != 0 )
    {
        return -2;// 数据错误 
    }

    if( pbOutputBuffer == NULL )
    {
        return (iInputCount >> 2) * 3;
    }

    for( i = 0; i < iInputCount; i += 4 )
    {
        for( j = 0; j < 4; j++ )
        {
            ch = *pbInputBuffer++;
            if( (ch < 0) || (ch >= 0x80) )
            {// 数据错误,不在ASCII字符编码范围内
                return -2;
                continue;
            }
            // 发现BASE64编码中的填充字符
            if( ch == '=' )
            {
                break;
            }

            b[j] = m_bASCII2BIN[ch];
            if( b[j] & 0x80 )
            {// 数据错误,无效的Base64编码字符
                return -2;
            }
        } // End for j 

        if( j == 4 )
        {
            *pbOutputBuffer++ = (b[0] << 2) | (b[1] >> 4);
            *pbOutputBuffer++ = (b[1] << 4) | (b[2] >> 2 );
            *pbOutputBuffer++ = (b[2] << 6) | b[3];
        }
        else if( j == 3 )
        { // 有1个填充字节 
            *pbOutputBuffer++ = (b[0] << 2) | (b[1] >> 4);
            *pbOutputBuffer++ = (b[1] << 4) | (b[2] >> 2 );

            return (i >> 2) * 3 + 2;
        }
        else if( j == 2 )
        { // 有2个填充字节 
            *pbOutputBuffer++ = (b[0] << 2) | (b[1] >> 4);

            return (i >> 2) * 3 + 1;
        }
        else  
        {// 数据错误,无效的Base64编码字符
            return -2;
        }           
    } // End for i

    return (iInputCount >> 2) * 3;
}

你可能感兴趣的:(加密,MD5,base64)