Base64 introduction and code implementation

Source: Internet
Author: User
Tags 0xc0
Base64 introduction base64 is one of the most common encoding methods used to transmit 8-bit bytes of code on the network. For details, refer to rfc2045 ~ Rfc2049, which has the mime detailed specification.

Base64 requires that each three 8-bit bytes be converted into four 6-bit bytes (3*8 = 4*6 = 24), and then 6-bit bytes be added with two more high 0 values, it consists of four 8-bit bytes. That is to say, the converted string is theoretically 1/3 longer than the original one.

Will it be too abstract? Not afraid. Let's look at an example:

Before conversion, aaaaaabb ccccdddd eeffffff
00 aaaaaa 00 bbcccc 00 ddddee 00 ffffff after conversion

Should it be clear? The above three bytes are the original text, and the following four bytes are base64 encoded after conversion, and the first two are both 0.

After conversion, we use a code table to obtain the desired string (that is, the final base64 encoding). This table is as follows: (from rfc2045)

Table 1: The base64 alphabet

Value encoding value Encoding
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
6g 23x40 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 (PAD) =
15 p 32G 49 x
16 Q 33 H 50 y

Let's take a look at the actual example to help you better understand it!

Before conversion 10101101 10111010 01110110
00101011 00011011 00101001 00110110 after conversion
Decimal 43 27 41 54
Corresponding to the value r B P 2 in the code table

Therefore, the base64 value of the preceding 24-bit encoding is rbp2.
Similarly, the original code is obtained by reorganizing the binary join of rbq2 to get three 8-bit values.
(Decoding is only the inverse process of encoding. I will not talk about it here. In addition, there are still many RFC related to mime. If you need details, please search for it yourself .)

The coding process is similar to that of programming:

The first character shifts two places to the right to obtain the location of the base64 table of the first target character. Based on this value, the corresponding character on the table is the first target character.
Then, move the first character four places to the left and the second character four places to the right to obtain the second target character.
Then, remove the second character from the left to the second and the third character to the right to get the third target character.
Finally, take the right 6 digits of the third character to obtain the fourth target character.

After each of the preceding steps, perform the and operation on the result and 0x3f to get the encoded characters.

But wait ...... If you are smart, you may ask that the number of bytes in the original text should be a multiple of 3. What if this condition cannot be met?

The solution is as follows: the bytes in the original text can be supplemented with 0, and the base64 encoding is replaced with the = number during conversion. This is why some base64 encoding ends with one or two equal signs, but the equal signs can only be two at most. Because:

Remainder = number of original bytes mod 3

Therefore, the remainder can only be one of the three numbers 0, 1, and 2 in any case. If the remainder is 0, it indicates that the number of original bytes is exactly a multiple of 3 (ideally ). If it is 1, in order to make base64 encoding a multiple of 4, we need to add two equal signs. Similarly, if it is 2, we need to add one equal sign.
Online conversion: http://md5.mmkey.com/base64/
Base64 encoding and decoding tools have a lot, here we recommend ecrypt software, can be from the http://sourceforge.net/project/showfiles.php? Group_id = 190563 & package_id = 223563 & release_id = 490837 download software and source code. Base64 encoding/decoding class base64.h

#include <string>std::string base64_encode(unsigned char const* , unsigned int len);std::string base64_decode(std::string const& s);
Base64.cpp
#include "base64.h"#include <iostream>static const std::string base64_chars =              "ABCDEFGHIJKLMNOPQRSTUVWXYZ"             "abcdefghijklmnopqrstuvwxyz"             "0123456789+/";static inline bool is_base64(unsigned char c) {  return (isalnum(c) || (c == '+') || (c == '/'));}std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {  std::string ret;  int i = 0;  int j = 0;  unsigned char char_array_3[3];  unsigned char char_array_4[4];  while (in_len--) {    char_array_3[i++] = *(bytes_to_encode++);    if (i == 3) {      char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;      char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);      char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);      char_array_4[3] = char_array_3[2] & 0x3f;      for(i = 0; (i <4) ; i++)        ret += base64_chars[char_array_4[i]];      i = 0;    }  }  if (i)  {    for(j = i; j < 3; j++)      char_array_3[j] = '\0';    char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;    char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);    char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);    char_array_4[3] = char_array_3[2] & 0x3f;    for (j = 0; (j < i + 1); j++)      ret += base64_chars[char_array_4[j]];    while((i++ < 3))      ret += '=';  }  return ret;}std::string base64_decode(std::string const& encoded_string) {  int in_len = encoded_string.size();  int i = 0;  int j = 0;  int in_ = 0;  unsigned char char_array_4[4], char_array_3[3];  std::string ret;  while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {    char_array_4[i++] = encoded_string[in_]; in_++;    if (i ==4) {      for (i = 0; i <4; i++)        char_array_4[i] = base64_chars.find(char_array_4[i]);      char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);      char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);      char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];      for (i = 0; (i < 3); i++)        ret += char_array_3[i];      i = 0;    }  }  if (i) {    for (j = i; j <4; j++)      char_array_4[j] = 0;    for (j = 0; j <4; j++)      char_array_4[j] = base64_chars.find(char_array_4[j]);    char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);    char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);    char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];    for (j = 0; (j < i - 1); j++) ret += char_array_3[j];  }  return ret;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.