base64的C++實現

來源:互聯網
上載者:User
  1.  概述
    base64可以將不可顯示的編碼轉換成可顯示的編碼,以便於儲存。
    比如http一般傳輸的是文字格式設定的報文,如果要傳輸圖片,那麼就要先將圖片編碼成base64的報文傳輸,然後在客用戶端顯示的時候,將base64的報文解密,轉換成二進位的圖片資料顯示。
    下面給出原始碼,可以直接在linux,msvc環境下進行編譯。
  2. 代碼
    Base64.h代碼如下:
    /**<br />* (C) Copyright 2009, asiainfo<br />* @version v1.0<br />* @author chenli<br />* @brief base64編碼和解碼<br />*<br />* history:<br />* <p>chenli 2009-02-17 1.0 build this moudle</p><br />*/</p><p>#ifndef ___BASE64_H___<br />#define ___BASE64_H___</p><p>#include <string></p><p>using namespace std;</p><p>class CBase64<br />{<br />public:<br />CBase64();<br />~CBase64();</p><p>/*********************************************************<br />* 函數說明:將輸入資料進行base64編碼<br />* 參數說明:[in]pIn需要進行編碼的資料<br />[in]uInLen 輸入參數的位元組數<br />[out]strOut 輸出的進行base64編碼之後的字串<br />* 傳回值 :true處理成功,false失敗<br />* 作 者 :ChenLi<br />* 編寫時間:2009-02-17<br />**********************************************************/<br />bool static Encode(const unsigned char *pIn, unsigned long uInLen, string& strOut);</p><p>/*********************************************************<br />* 函數說明:將輸入資料進行base64編碼<br />* 參數說明:[in]pIn需要進行編碼的資料<br />[in]uInLen輸入參數的位元組數<br />[out]pOut輸出的進行base64編碼之後的字串<br />[out]uOutLen輸出的進行base64編碼之後的字串長度<br />* 傳回值 :true處理成功,false失敗<br />* 作 者 :ChenLi<br />* 編寫時間:2009-02-17<br />**********************************************************/<br />bool static Encode(const unsigned char *pIn, unsigned long uInLen, unsigned char *pOut, unsigned long *uOutLen);</p><p>/*********************************************************<br />* 函數說明:將輸入資料進行base64解碼<br />* 參數說明:[in]strIn需要進行解碼的資料<br />[out]pOut輸出解碼之後的節數資料<br />[out]uOutLen輸出的解碼之後的位元組數長度<br />* 傳回值 :true處理成功,false失敗<br />* 作 者 :ChenLi<br />* 編寫時間:2009-02-17<br />**********************************************************/<br />bool static Decode(const string& strIn, unsigned char *pOut, unsigned long *uOutLen) ;</p><p>/*********************************************************<br />* 函數說明:將輸入資料進行base64解碼<br />* 參數說明:[in]strIn需要進行解碼的資料<br />[out]pOut輸出解碼之後的節數資料<br />[out]uOutLen輸出的解碼之後的位元組數長度<br />* 傳回值 :true處理成功,false失敗<br />* 作 者 :ChenLi<br />* 編寫時間:2009-02-17<br />**********************************************************/<br />bool static Decode(const unsigned char *pIn, unsigned long uInLen, unsigned char *pOut, unsigned long *uOutLen) ;<br />};</p><p>#endif // ___BASE64_H___<br />
    Base64.cpp代碼如下:
    /*<br /> *<br /> *Base64?<br /> *<br /> *<br /> *<br />*/</p><p>#include "Base64.h"</p><p>static const char *g_pCodes =<br />"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";</p><p>static const unsigned char g_pMap[256] =<br />{<br />255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,<br />255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,<br />255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,<br />255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63,<br /> 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255,<br />255, 254, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6,<br /> 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,<br /> 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255,<br />255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,<br /> 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,<br /> 49, 50, 51, 255, 255, 255, 255, 255, 255, 255, 255, 255,<br />255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,<br />255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,<br />255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,<br />255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,<br />255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,<br />255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,<br />255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,<br />255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,<br />255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,<br />255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,<br />255, 255, 255, 255<br />};</p><p>CBase64::CBase64()<br />{<br />}</p><p>CBase64::~CBase64()<br />{<br />}</p><p>bool CBase64::Encode(const unsigned char *pIn, unsigned long uInLen, unsigned char *pOut, unsigned long *uOutLen)<br />{<br />unsigned long i, len2, leven;<br />unsigned char *p;</p><p>if(pOut == NULL || *uOutLen == 0)<br />return false;</p><p>//ASSERT((pIn != NULL) && (uInLen != 0) && (pOut != NULL) && (uOutLen != NULL));</p><p>len2 = ((uInLen + 2) / 3) << 2;<br />if((*uOutLen) < (len2 + 1)) return false;</p><p>p = pOut;<br />leven = 3 * (uInLen / 3);<br />for(i = 0; i < leven; i += 3)<br />{<br />*p++ = g_pCodes[pIn[0] >> 2];<br />*p++ = g_pCodes[((pIn[0] & 3) << 4) + (pIn[1] >> 4)];<br />*p++ = g_pCodes[((pIn[1] & 0xf) << 2) + (pIn[2] >> 6)];<br />*p++ = g_pCodes[pIn[2] & 0x3f];<br />pIn += 3;<br />}</p><p>if (i < uInLen)<br />{<br />unsigned char a = pIn[0];<br />unsigned char b = ((i + 1) < uInLen) ? pIn[1] : 0;<br />unsigned char c = 0;</p><p>*p++ = g_pCodes[a >> 2];<br />*p++ = g_pCodes[((a & 3) << 4) + (b >> 4)];<br />*p++ = ((i + 1) < uInLen) ? g_pCodes[((b & 0xf) << 2) + (c >> 6)] : '=';<br />*p++ = '=';<br />}</p><p>*p = 0; // Append NULL byte<br />*uOutLen = p - pOut;<br />return true;<br />}</p><p>bool CBase64::Encode(const unsigned char *pIn, unsigned long uInLen, string& strOut)<br />{<br />unsigned long i, len2, leven;<br />strOut = "";</p><p>//ASSERT((pIn != NULL) && (uInLen != 0) && (pOut != NULL) && (uOutLen != NULL));</p><p>len2 = ((uInLen + 2) / 3) << 2;<br />//if((*uOutLen) < (len2 + 1)) return false;</p><p>//p = pOut;<br />leven = 3 * (uInLen / 3);<br />for(i = 0; i < leven; i += 3)<br />{<br />strOut += g_pCodes[pIn[0] >> 2];<br />strOut += g_pCodes[((pIn[0] & 3) << 4) + (pIn[1] >> 4)];<br />strOut += g_pCodes[((pIn[1] & 0xf) << 2) + (pIn[2] >> 6)];<br />strOut += g_pCodes[pIn[2] & 0x3f];<br />pIn += 3;<br />}</p><p>if (i < uInLen)<br />{<br />unsigned char a = pIn[0];<br />unsigned char b = ((i + 1) < uInLen) ? pIn[1] : 0;<br />unsigned char c = 0;</p><p>strOut += g_pCodes[a >> 2];<br />strOut += g_pCodes[((a & 3) << 4) + (b >> 4)];<br />strOut += ((i + 1) < uInLen) ? g_pCodes[((b & 0xf) << 2) + (c >> 6)] : '=';<br />strOut += '=';<br />}</p><p>//*p = 0; // Append NULL byte<br />//*uOutLen = p - pOut;<br />return true;<br />}</p><p>bool CBase64::Decode(const string& strIn, unsigned char *pOut, unsigned long *uOutLen)<br />{<br />unsigned long t, x, y, z;<br />unsigned char c;<br />unsigned long g = 3;</p><p>//ASSERT((pIn != NULL) && (uInLen != 0) && (pOut != NULL) && (uOutLen != NULL));</p><p>for(x = y = z = t = 0; x < strIn.length(); x++)<br />{<br />c = g_pMap[strIn[x]];<br />if(c == 255) continue;<br />if(c == 254) { c = 0; g--; }</p><p>t = (t << 6) | c;</p><p>if(++y == 4)<br />{<br />if((z + g) > *uOutLen) { return false; } // Buffer overflow<br />pOut[z++] = (unsigned char)((t>>16)&255);<br />if(g > 1) pOut[z++] = (unsigned char)((t>>8)&255);<br />if(g > 2) pOut[z++] = (unsigned char)(t&255);<br />y = t = 0;<br />}<br />}</p><p>*uOutLen = z;<br />return true;<br />}<br />

    上面有了base64的演算法,下面寫個程式進行測試。
    main.cpp檔案內容如下:
    #include "Base64.h"</p><p>#include <iostream></p><p>using namespace std;</p><p>int main()<br />{<br />unsigned long len = 10;</p><p>unsigned char pIn[100];<br />unsigned char pOut[100];</p><p>memcpy(pIn,"你好",5);</p><p>string strout;</p><p>cout<<(char*)pIn<<endl;<br />if(CBase64::Encode(pIn, 4, strout))<br />cout<<strout<<endl;<br />else<br />cout<<"加密失敗"<<endl;</p><p>string stroIn = strout;<br />cout<<stroIn<<endl;</p><p>memset(pOut,0,sizeof(pOut));</p><p>if(CBase64::Decode(stroIn, pOut, &len))<br />{<br />cout<<(char *)pOut<<endl;<br />cout<<"len="<<strlen((char *)pOut)<<endl;<br />}<br />else<br />cout<<"解密失敗"<<endl;</p><p>cout<<len<<endl;</p><p>return 0;<br />}

    上面的代碼直接編譯就可以運行了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.