BASE64編碼和解碼

來源:互聯網
上載者:User

    Base64是網路上最常見的用於傳輸8Bit位元組代碼的編碼方式之一,Base64要求把每三個8Bit的位元組轉換為四個6Bit的位元組(3*8 = 4*6 = 24),然後把6Bit再添兩位高位0,組成四個8Bit的位元組,也就是說,轉換後的字串理論上將要比原來的長1/3,轉換之後通過一個BASE64的碼錶就可以得到BASE64編碼。如果字串不是3的倍數的話,轉換時位元組不足的地方用‘=’代替。

 

    BASE64碼錶:

 

索引 對應字元 索引 對應字元 索引 對應字元 索引 對應字元
0 A 17 R 34 i 51 z
1 B 18 S 35 j 52 0
2 C 19 T 36 k 53 1
3 D 20 U 37 l 54 2
4 E 21 V 38 m 55 3
5 F 22 W 39 n 56 4
6 G 23 X 40 o 57 5
7 H 24 Y 41 p 58 6
8 I 25 Z 42 q 59 7
9 J 26 a 43 r 60 8
10 K 27 b 44 s 61 9
11 L 28 c 45 t 62 +
12 M 29 d 46 u 63 /
13 N 30 e 47 v    
14 O 31 f 48 w    
15 P 32 g 49 x    
16 Q 33 h 50 y    

 

       舉個例子:假如源字串是

      00110110

      11001100

      11011010,則按照每6位劃分成4個位元組,並在高2為補零(6位不足,所以在高2位補零,組成一位元組),結果如下:

      00001101

      00101100

      00110011

      00011010

      對應的十進位分別是:

      13

      44

      51

      26

      根據碼錶,可以BASE64編碼為:Nsza。解碼只是編碼的逆過程。

 

      vc實現BASE64編碼如下:

     //定義碼錶

      const char* JBase64::pszEncodeTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

      //解碼函數

      bool JBase64::Encode(<br /> const BYTE* pByteIn,<br /> int nInLength,<br /> BYTE* pByteOut,<br /> int& nOutLength<br />)<br />{<br /> bool bReturn = false;<br /> assert(pByteIn != NULL);<br /> assert(pByteOut != NULL);<br /> assert(nInLength > 0);<br /> if (pByteIn == NULL || pByteOut == NULL || nInLength <= 0)<br /> {<br /> goto exit0;<br /> }</p><p> int nCount = nInLength / 3;<br /> int nMode = nInLength % 3;<br /> BYTE* pOut = pByteOut;<br /> for (int i = 0; i < nCount; i++)<br /> {<br /> *pOut++ = pszEncodeTable[(pByteIn[0] >> 2)];<br /> *pOut++ = pszEncodeTable[((pByteIn[0] & 3) << 4) | (pByteIn[1] >> 4)];<br /> *pOut++ = pszEncodeTable[(pByteIn[1] & 0xf) << 2 | (pByteIn[2] >> 6)];<br /> *pOut++ = pszEncodeTable[(pByteIn[2] & 0x3f)];<br /> pByteIn += 3;<br /> }<br /> if (nMode != 0)<br /> {<br /> *pOut++ = pszEncodeTable[(pByteIn[0] >> 2)];<br /> *pOut++ = pszEncodeTable[((pByteIn[0] & 3) << 4) | ((nMode == 1) ? 0 : (pByteIn[1] >> 4))];<br /> *pOut++ = (nMode == 1) ? '=':pszEncodeTable[((pByteIn[1] & 0xf) << 2)];<br /> *pOut++ = '=';<br /> }<br /> *pOut = '/0';<br /> nOutLength = static_cast<int>(pOut - pByteOut);<br /> bReturn = true;<br />exit0:<br /> return bReturn;<br />}

 

     //定義解碼錶

     const unsigned char JBase64::pszDecodeTable[256] =
    {
         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, 254, 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, 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, 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, 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
};

      //解碼函數

      bool JBase64::Decode(<br /> const BYTE* pByteIn,<br /> int nInLength,<br /> BYTE* pByteOut,<br /> int& nOutLength<br />)<br />{<br /> bool bReturn = false;<br /> assert(pByteIn != NULL);<br /> assert(pByteOut != NULL);<br /> assert(nInLength > 0);<br /> assert(nInLength % 4 == 0);<br /> if (pByteIn == NULL || pByteOut == NULL || nInLength % 4 != 0)<br /> {<br /> goto exit0;<br /> }<br /> BYTE tmp[4];<br /> BYTE* pOut = pByteOut;<br /> int nLength = nInLength / 4;<br /> for (int i = 0; i < nLength; i++)<br /> {<br /> tmp[0] = pszDecodeTable[pByteIn[0]];<br /> tmp[1] = pszDecodeTable[pByteIn[1]];<br /> tmp[2] = pszDecodeTable[pByteIn[2]];<br /> tmp[3] = pszDecodeTable[pByteIn[3]];</p><p> *pOut++ = ((tmp[0] << 2) | (tmp[1] >> 4));<br /> *pOut++ = ((tmp[1] << 4) | ((tmp[2] == 254 ? 0 : tmp[2] >> 2)));<br /> *pOut++ = (((tmp[2] == 254 ? 0 : tmp[2]<< 6)) | ((tmp[3] == 254 ? 0 : tmp[3])));<br /> pByteIn += 4;<br /> }<br /> *pOut = '/0';<br /> nOutLength = static_cast<int>(pOut - pByteOut);<br /> bReturn = true;<br />exit0:<br /> return bReturn;<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.