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 //解码