Base64加解密C++
阿新 • • 發佈:2018-12-12
BASE64.CPP
#include "BASE64.h" static const CHAR* DATA_BIN2ASCII = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; INT BASE64_Encode( const BYTE* inputBuffer, INT inputCount, TCHAR* outputBuffer ) { INT i; BYTE b0, b1, b2; if( (inputBuffer == NULL) || (inputCount < 0) ) { return -1; // 引數錯誤 } if( outputBuffer != NULL ) { for( i = inputCount; i > 0; i -= 3 ) { if( i >= 3 ) { // 將3位元組資料轉換成4個ASCII字元 b0 = *inputBuffer++; b1 = *inputBuffer++; b2 = *inputBuffer++; *outputBuffer++ = DATA_BIN2ASCII[b0 >> 2]; *outputBuffer++ = DATA_BIN2ASCII[((b0 << 4) | (b1 >> 4)) & 0x3F]; *outputBuffer++ = DATA_BIN2ASCII[((b1 << 2) | (b2 >> 6)) & 0x3F]; *outputBuffer++ = DATA_BIN2ASCII[b2 & 0x3F]; } else { b0 = *inputBuffer++; if( i == 2 )b1 = *inputBuffer++; else b1 = 0; *outputBuffer++ = DATA_BIN2ASCII[b0 >> 2]; *outputBuffer++ = DATA_BIN2ASCII[((b0 << 4) | (b1 >> 4)) & 0x3F]; *outputBuffer++ = (i == 1) ? TEXT('=') : DATA_BIN2ASCII[(b1 << 2) & 0x3F]; *outputBuffer++ = TEXT('='); } } // End for i *outputBuffer++ = TEXT('/0'); // 新增字串結束標記 } return ((inputCount + 2) / 3) * 4; // 返回有效字元個數 } #define B64_EOLN 0xF0 // 換行/n #define B64_CR 0xF1 // 回車/r #define B64_EOF 0xF2 // 連字元- #define B64_WS 0xE0 // 跳格或者空格(/t、space) #define B64_ERROR 0xFF // 錯誤字元 #define B64_NOT_BASE64(a) (((a)|0x13) == 0xF3) static const BYTE DATA_ASCII2BIN[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 }; INT BASE64_Decode( const TCHAR* inputBuffer, INT inputCount, BYTE* outputBuffer ) { INT i, j; BYTE b[4]; TCHAR ch; if( (inputBuffer == NULL) || (inputCount < 0) ) { return -1; // 引數錯誤 } // 去除頭部空白字元 while( inputCount > 0 ) { ch = *inputBuffer; if( (ch < 0) || (ch >= 0x80) ) //base64碼錶只有0-64,怎麼到80了,多了回車換行? { return -2; // 資料錯誤,不在ASCII字元編碼範圍內 } else { if( DATA_ASCII2BIN[ch] == B64_WS ) { inputBuffer++; inputCount--; } else { break; } } } // 去除尾部的空白字元、回車換行字元、連字元 while( inputCount >= 4 ) { ch = inputBuffer[inputCount - 1]; if( (ch < 0) || (ch >= 0x80) ) { return -2; // 資料錯誤,不在ASCII字元編碼範圍內 } else { if( B64_NOT_BASE64(DATA_ASCII2BIN[ch]) ) { inputCount--; } else { break; } } } // 字串長度必須為4的倍數 if( (inputCount % 4) != 0 ) { return -2; // 資料錯誤 } if( outputBuffer != NULL ) { for( i = 0; i < inputCount; i += 4 ) { for( j = 0; j < 4; j++ ) { ch = *inputBuffer++; if( (ch < 0) || (ch >= 0x80) ) { return -2; // 資料錯誤,不在ASCII字元編碼範圍內 } else { if( ch == '=' ) // 發現BASE64編碼中的填充字元 { break; } else { b[j] = DATA_ASCII2BIN[ch]; if( b[j] & 0x80 ) { return -2; // 資料錯誤,無效的Base64編碼字元 } } } } // End for j if( j == 4 ) { *outputBuffer++ = (b[0] << 2) | (b[1] >> 4); *outputBuffer++ = (b[1] << 4) | (b[2] >> 2 ); *outputBuffer++ = (b[2] << 6) | b[3]; } else if( j == 3 ) { // 有1個填充位元組 *outputBuffer++ = (b[0] << 2) | (b[1] >> 4); *outputBuffer++ = (b[1] << 4) | (b[2] >> 2 ); return (i >> 2) * 3 + 2; } else if( j == 2 ) { // 有2個填充位元組 *outputBuffer++ = (b[0] << 2) | (b[1] >> 4); return (i >> 2) * 3 + 1; } else { return -2; // 資料錯誤,無效的Base64編碼字元 } } // End for i } return (inputCount >> 2) * 3; }
BASE64.h
#pragma once #include <windows.h> #ifdef __cplusplus extern "C" { #endif /* 功能:將二進位制資料轉換成BASE64編碼字串 引數說明: inputBuffer:要編碼的二進位制資料 inputCount:資料長度 outputBuffer:儲存轉換後的BASE64編碼字串 返回值: -1:引數錯誤 >=0:有效編碼長度(字元數),不包括字串結束符。 備註: 等效於openssl中EVP_EncodeBlock函式 */ INT BASE64_Encode( const BYTE* inputBuffer, INT inputCount, TCHAR* outputBuffer ); /* 功能:將BASE64編碼字串轉換為二進位制資料 引數說明: inputBuffer:BASE64編碼字串 inputCount:編碼長度(字元數),應該為4的倍數。 outputBuffer:儲存轉換後的二進位制資料 返回值: -1:引數錯誤 -2:資料錯誤 >=0:轉換後的位元組數 備註: 等效於openssl中EVP_DecodeBlock函式 */ INT BASE64_Decode( const TCHAR* inputBuffer, INT inputCount, BYTE* outputBuffer ); #ifdef __cplusplus } #endif
加密呼叫:
BOOL FieldEncrypt( CString strFieldName,CString &strResult) { CHAR* chTemp = WideChartoUTF8(strFieldName.GetBuffer()); string strRet(chTemp); delete [] chTemp; int nCount = strlen(strRet.c_str()); int nDes = 0; if (nCount%3 == 0) { nDes = nCount/3*4; } else { nDes = (nCount/3 +1)*4; } TCHAR* chData = new TCHAR[nDes+2]; ZeroMemory(chData,(nDes+2)*sizeof(TCHAR)); BASE64_Encode((BYTE*)strRet.c_str(),nCount,chData); strResult = chData; if (NULL != chData) { delete chData; chData = NULL; } return TRUE; } CHAR* WideChartoUTF8(const wchar_t* pwzData) { if (NULL == pwzData) { return NULL; } INT nLen = WideCharToMultiByte(CP_UTF8, 0, pwzData, -1, NULL, 0, NULL, NULL ); if (nLen <= 0) { return NULL; } CHAR* pUtf8Rtn = new CHAR[nLen + 1]; if (NULL == pUtf8Rtn) { return NULL; } memset(pUtf8Rtn, 0, nLen + 1); WideCharToMultiByte( CP_UTF8, 0, pwzData, -1, pUtf8Rtn, nLen, NULL, NULL ); return pUtf8Rtn; }