C++實現base64編解碼


Base64是常見的加密算法,代碼實現了基於C++的對於base64的編碼和解碼。

其中注釋掉的部分為編碼部分,取消注釋將解碼部分注釋掉即可實現編碼,反之可以實現解碼。

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <assert.h>
  4 
  5 typedef unsigned char     uint8;
  6 typedef unsigned long    uint32;
  7 
  8 static uint8 alphabet_map[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  9 static uint8 reverse_map[] =
 10 {
 11      255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
 12      255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
 13      255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63,
 14      52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255, 255, 255, 255,
 15      255,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
 16      15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255,
 17      255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
 18      41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 255, 255, 255, 255, 255
 19 };
 20 
 21 uint32 base64_encode(const uint8 *text, uint32 text_len, uint8 *encode)
 22 {
 23     uint32 i, j;
 24     for (i = 0, j = 0; i+3 <= text_len; i+=3)
 25     {
 26         encode[j++] = alphabet_map[text[i]>>2];                             //取出第一個字符的前6位並找出對應的結果字符
 27         encode[j++] = alphabet_map[((text[i]<<4)&0x30)|(text[i+1]>>4)];     //將第一個字符的后2位與第二個字符的前4位進行組合並找到對應的結果字符
 28         encode[j++] = alphabet_map[((text[i+1]<<2)&0x3c)|(text[i+2]>>6)];   //將第二個字符的后4位與第三個字符的前2位組合並找出對應的結果字符
 29         encode[j++] = alphabet_map[text[i+2]&0x3f];                         //取出第三個字符的后6位並找出結果字符
 30     }
 31 
 32     if (i < text_len)
 33     {
 34         uint32 tail = text_len - i;
 35         if (tail == 1)
 36         {
 37             encode[j++] = alphabet_map[text[i]>>2];
 38             encode[j++] = alphabet_map[(text[i]<<4)&0x30];
 39             encode[j++] = '=';
 40             encode[j++] = '=';
 41         }
 42         else //tail==2
 43         {
 44             encode[j++] = alphabet_map[text[i]>>2];
 45             encode[j++] = alphabet_map[((text[i]<<4)&0x30)|(text[i+1]>>4)];
 46             encode[j++] = alphabet_map[(text[i+1]<<2)&0x3c];
 47             encode[j++] = '=';
 48         }
 49     }
 50     return j;
 51 }
 52 
 53 uint32 base64_decode(const uint8 *code, uint32 code_len, uint8 *plain)
 54 {
 55     assert((code_len&0x03) == 0);  //如果它的條件返回錯誤,則終止程序執行。4的倍數。
 56 
 57     uint32 i, j = 0;
 58     uint8 quad[4];
 59     for (i = 0; i < code_len; i+=4)
 60     {
 61         for (uint32 k = 0; k < 4; k++)
 62         {
 63             quad[k] = reverse_map[code[i+k]];//分組,每組四個分別依次轉換為base64表內的十進制數
 64         }
 65 
 66         assert(quad[0]<64 && quad[1]<64);
 67 
 68         plain[j++] = (quad[0]<<2)|(quad[1]>>4); //取出第一個字符對應base64表的十進制數的前6位與第二個字符對應base64表的十進制數的前2位進行組合
 69 
 70         if (quad[2] >= 64)
 71             break;
 72         else if (quad[3] >= 64)
 73         {
 74             plain[j++] = (quad[1]<<4)|(quad[2]>>2); //取出第二個字符對應base64表的十進制數的后4位與第三個字符對應base64表的十進制數的前4位進行組合
 75             break;
 76         }
 77         else
 78         {
 79             plain[j++] = (quad[1]<<4)|(quad[2]>>2);
 80             plain[j++] = (quad[2]<<6)|quad[3];//取出第三個字符對應base64表的十進制數的后2位與第4個字符進行組合
 81         }
 82     }
 83     return j;
 84 }
 85 
 86 
 87 /*int main(void)
 88 {
 89     char input[256];
 90     while (true){
 91     printf("Please input string: ");
 92     scanf("%s", input);
 93     uint8 *text = (uint8 *)input;
 94     uint32 text_len = (uint32)strlen((char *)text);
 95     uint8 buffer[1024], buffer2[4096];
 96     uint32 size = base64_encode(text, text_len, buffer2);
 97     buffer2[size] = 0;
 98     printf("%s\n", buffer2);
 99 
100     size = base64_decode(buffer2, size, buffer);
101     buffer[size] = 0;
102     printf("%s\n", buffer);
103 
104     }
105     return 0;
106 }*/
107 //編碼
108 int main(void)
109 {
110     char input[256];
111     while (true){
112     printf("Please input what you want to decode: ");
113     scanf("%s", input);
114     uint8 *text = (uint8 *)input;
115     uint32 text_len = (uint32)strlen((char *)text);
116     uint8 buffer[1024],buffer2[4096];
117 
118     uint32 size = base64_decode(text, text_len, buffer);
119     buffer[size] = 0;
120     printf("Decoded content: %s\n", buffer);
121     size = base64_encode(buffer, size, buffer2);
122     buffer2[size] = 0;
123     printf("Confirmation of the original content: %s\n", buffer2);
124     }
125     return 0;
126 }
127 //解碼

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM