程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> VC中BASE64編碼息爭碼應用詳解

VC中BASE64編碼息爭碼應用詳解

編輯:關於C++

VC中BASE64編碼息爭碼應用詳解。本站提示廣大學習愛好者:(VC中BASE64編碼息爭碼應用詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是VC中BASE64編碼息爭碼應用詳解正文


BASE64可以用來將binary的字節序列數據編碼成ASCII字符序列組成的文本。完全的BASE64界說可見 RFC1421和 RFC2045。編碼後的數據比原始數據略長,為本來的4/3。在電子郵件中,依據RFC822劃定,每76個字符,還須要加上一個回車換行。

轉換的時刻,將三個byte的數據,前後放入一個24bit的緩沖區中,先來的byte占高位。數據缺乏3byte的話,於緩沖區中剩下的Bit用0補足。然後,每次掏出6個bit,依照其值選擇ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/中的字符作為編碼後的輸入。赓續停止,直到全體輸出數據轉換完成。假如最初剩下兩個輸出數據,在編碼成果後加1個“=”;假如最初剩下一個輸出數據,編碼成果後加2個“=”;假如沒有剩下任何數據,就甚麼都不要加,如許才可以包管材料復原的准確性。

BASE64_API.h  文件內容

/* ----------------------------------------------------------
文件稱號:BASE64_API.h

作者:秦建輝

MSN:[email protected]

以後版本:V1.1

汗青版本:
  V1.1  2010年05月11日
      修改BASE64解碼的Bug。

  V1.0  2010年05月07日
      完成正式版本。

功效描寫:
  BASE64編碼息爭碼

接口函數:
  Base64_Encode
  Base64_Decode

解釋:
  1.  參考openssl-1.0.0。
  2.  改良接口,以使其順應TCHAR字符串。
  3.  修改EVP_DecodeBlock函數解碼時未去失落填充字節的缺點。
 ------------------------------------------------------------ */
#pragma once
#include "stdafx.h"

#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

BASE64_API.cpp 文件內容

#pragma once

#include "stdafx.h"

#include "BASE64_API.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) )
    {
      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;
}

采取以上辦法便可以將二進制數據轉換成可見字符停止傳遞便可以了.

那末若何應用呢?舉以下兩個例子

第一個:將一個圖片轉換成 txt 文本 並保留起來

//選擇一個圖象文件,將它轉為 文本保留至 _T("D:\\2.txt"
void CTextPicDlg::OnBnClickedButton2()
{
  // TODO: 在此添加控件告訴處置法式代碼
  CFileDialog file(TRUE,".jpg","");
  if (file.DoModal() == IDOK)
  {
    CFile data(file.GetPathName(), CFile::modeReadWrite);
    int len = data.GetLength();
    BYTE *dv;
    dv = (BYTE *)malloc(len*sizeof(BYTE));
    data.Read(dv, len);
    data.Close();
    int slen = (len / 3) * 4;
    slen += 10;
    TCHAR * tc;
    tc = (TCHAR *)malloc(slen);
    slen = BASE64_Encode(dv, len, tc);
    CFile save(_T("D:\\2.txt"), CFile::modeCreate | CFile::modeWrite);
    save.Write(tc, slen);
    save.Close();
    free(tc);
    free(dv);
  }
}

第二個例子,將一個文本文件復原為一個圖象

void CTextPicDlg::OnBnClickedButton3()
{
  // TODO: 在此添加控件告訴處置法式代碼
  CFileDialog file(TRUE, ".txt", "");
  if (file.DoModal() == IDOK)
  {
    CFile data(file.GetPathName(), CFile::modeReadWrite);
    int len = data.GetLength();
    TCHAR *dv;
    dv = (TCHAR *)malloc(len*sizeof(TCHAR));
    data.Read(dv, len);
    data.Close();
    int slen = (len / 4) * 3;
    slen += 10;
    BYTE * tc;
    tc = (BYTE *)malloc(slen);
    BASE64_Decode(dv, len, tc);
    //直接在內存外面構建CIMAGE,須要應用IStream接口,若何應用
    //構建內存情況    
    HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, slen);
    void * pData = GlobalLock(hGlobal);
    memcpy(pData, tc, slen); // 拷貝位圖數據出來
    GlobalUnlock(hGlobal);
    // 創立IStream
    IStream * pStream = NULL;
    if (CreateStreamOnHGlobal(hGlobal, TRUE, &pStream) != S_OK)
      return ;
    //  應用CImage加載位圖內存
    CImage img;
    if (SUCCEEDED(img.Load(pStream)) )
    {
      CClientDC dc(this);
       //應用內涵中結構的圖象 直接在對話框上畫圖
       img.Draw(dc.m_hDC, 0, 0, 500, 300);
    }
     //釋放內存
    pStream->Release();
    GlobalFree(hGlobal);
    //假如要保留圖象文件的話,那就應用上面的代碼
    //CFileDialog savefile(FALSE, ".jpg", "");
    //if (savefile.DoModal()==IDOK)
    //{
    //  CFile save(savefile.GetPathName(), CFile::modeCreate | CFile::modeWrite);
    //  save.Write(tc, slen);
    //  save.Close();
    //}
    free(tc);
    free(dv);
  }
}

至此,應用Base64轉碼的方法,來顯示保留顯示圖片的辦法,就算是勝利了!

我們再來看一個base64編碼解碼的例子

起首是編碼

const BYTE Base64ValTab[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

#define AVal(x) Base64ValTab[x]
int CSeeBase64Dlg::EncodeBase64(char * pInput, char * pOutput)
{
    int i = 0;
    int loop = 0;
    int remain = 0;
    int iDstLen = 0;
    int iSrcLen = (int)strlen(pInput);

    loop = iSrcLen/3;
    remain = iSrcLen%3;

    // also can encode native char one by one as decode method
    // but because all of char in native string is to be encoded so encode 3-chars one time is easier.

    for (i=0; i < loop; i++)
    {
        BYTE a1 = (pInput[i*3] >> 2);
        BYTE a2 = ( ((pInput[i*3] & 0x03) << 4) | (pInput[i*3+1] >> 4) );
        BYTE a3 = ( ((pInput[i*3+1] & 0x0F) << 2) | ((pInput[i*3+2] & 0xC0) >> 6) );
        BYTE a4 = (pInput[i*3+2] & 0x3F);

        pOutput[i*4] = AVal(a1);
        pOutput[i*4+1] = AVal(a2);
        pOutput[i*4+2] = AVal(a3);
        pOutput[i*4+3] = AVal(a4);
    }

    iDstLen = i*4;

    if (remain == 1)
    {
        // should pad two equal sign
        i = iSrcLen-1;
        BYTE a1 = (pInput[i] >> 2);
        BYTE a2 = ((pInput[i] & 0x03) << 4);
        
        pOutput[iDstLen++] = AVal(a1);
        pOutput[iDstLen++] = AVal(a2);
        pOutput[iDstLen++] = '=';
        pOutput[iDstLen++] = '=';
        pOutput[iDstLen] = 0x00;
    }
    else if (remain == 2)
    {
        // should pad one equal sign
        i = iSrcLen-2;
        BYTE a1 = (pInput[i] >> 2);
        BYTE a2 = ( ((pInput[i] & 0x03) << 4) | (pInput[i+1] >> 4));
        BYTE a3 = ( (pInput[i+1] & 0x0F) << 2);

        pOutput[iDstLen++] = AVal(a1);
        pOutput[iDstLen++] = AVal(a2);
        pOutput[iDstLen++] = AVal(a3);
        pOutput[iDstLen++] = '=';
        pOutput[iDstLen] = 0x00;
    }
    else
    {
        // just division by 3
        pOutput[iDstLen] = 0x00;
    }

    return iDstLen;
}

上面是解碼

const BYTE Base64IdxTab[128] =
{
255,255,255,255, 255,255,255,255, 255,255,255,255, 255,255,255,255,
255,255,255,255, 255,255,255,255, 255,255,255,255, 255,255,255,255,
255,255,255,255, 255,255,255,255, 255,255,255,62,  255,255,255,63,
52,53,54,55,   56,57,58,59,   60,61,255,255,  255,255,255,255,
255,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,255,   255,255,255,255,
255,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,255,   255,255,255,255
};

#define BVal(x) Base64IdxTab[x]

int CSeeBase64Dlg::DecodeBase64(char * pInput, char * pOutput)
{
    int i = 0;
    int iCnt = 0;
    int iSrcLen = (int)strlen(pInput);

    char * p = pOutput;

    for (i=0; i < iSrcLen; i++)
    {
        if (pInput[i] > 127) continue;
        if (pInput[i] == '=') return p-pOutput+1;

        BYTE a = BVal(pInput[i]);
        if (a == 255) continue;
        
        switch (iCnt)
        {
        case 0:
            {
                *p = a << 2;
                iCnt++;
            }
            break;

        case 1:
            {
                *p++ |= a >> 4;
                *p = a << 4;
                iCnt++;
            }
            break;

        case 2:
            {
                *p++ |= a >> 2;
                *p = a << 6;
                iCnt++;
            }
            break;
        case 3:
            {
                *p++ |= a;
                iCnt = 0;
            }
            break;
        } 
    }

    *p = 0x00;
    return p-pOutput;
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved