Base64 encryption algorithm C ++ code implementation, base64 encryption algorithm code

Source: Internet
Author: User

Base64 encryption algorithm C ++ code implementation, base64 encryption algorithm code

The base64 encryption rules are as follows:

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.

Implementation Method:

The first byte is processed according to the first byte of the Source byte. Rule: shifts the first byte of the source to two places to the right. Remove the lower two places and add the higher two places to zero. The value is 00 + the second byte with 6 Characters in height, and is processed by the first byte and the second byte of the Source byte. The rule is as follows: remove the first byte with a 6-bit high and then move it four places to the left. The second byte shifts the four places to the right: the first byte with a 2-bit low + the source 2nd byte with a 4-Bit High third byte, based on the combined processing of the second and third bytes of the Source byte, the second byte removes the 4-bit high and shifts the two places to the left (6 digits high ), the third byte shifts 6 bits to the right and removes the 6 bits in height (2 bits in height). The fourth byte can be added. Rule: remove the third byte from the second byte. In terms of programming, the encoding process is like this: the first character shifts two places to the right to obtain the Base64 table location of the first target character, according to this value, the corresponding character is the first target character. Then, perform the (&) operation on the first character and 0x03 (00000011), move the second character four places to the left, and then move the second character to the right four places to the former or (| ), that is, the second target character is obtained. Then, the second character and 0x0f (00001111) are operated with (&) and shifted to 2 places left. Then the third character is shifted to 6 places with the former or (| ), obtain the third target character. Finally, the third character and 0x3f (00111111) are connected with (&) 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 remaining bytes of the original text are converted separately according to the encoding rules (1 is changed to 2, 2 is changed to 3; the number of digits is not enough to be filled with 0 ), fill 4 bytes with the = sign. This is why some Base64 encoding ends with one or two equal signs, but the equal signs can only be two at most. Because: an original byte will be at least two target bytes. 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, it is converted into two Base64 encoded characters. 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. The source code is as follows:
  1. Char* Base64Encode (Char Const* OrigSigned, unsigned origLength)
  2. {
  3. Static Const CharBase64Char [] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 + /";
  4. UnsignedChar Const* Orig = (unsignedChar Const*) OrigSigned;
  5. If(Orig = NULL)ReturnNULL;
  6. UnsignedConstNumOrig24BitValues = origLength/3;
  7. BoolHavePadding = origLength> numOrig24BitValues * 3;
  8. BoolHavePadding2 = origLength = numOrig24BitValues * 3 + 2;
  9. UnsignedConstNumResultBytes = 4 * (numOrig24BitValues + havePadding );
  10. Char* Result =New Char[NumResultBytes + 1];
  11. // Map each full group of 3 input bytes into 4 output base-64 characters:
  12. Unsigned I;
  13. For(I = 0; I <numOrig24BitValues; ++ I)
  14. {
  15. Result [4 * I + 0] = base64Char [(orig [3 * I]> 2) & 0x3F];
  16. Result [4 * I + 1] = base64Char [(orig [3 * I] & 0x3) <4) | (orig [3 * I + 1]> 4) & 0x3F];
  17. Result [4 * I + 2] = base64Char [(orig [3 * I + 1] & 0x0f) <2) | (orig [3 * I + 2]> 6) & 0x3F];
  18. Result [4 * I + 3] = base64Char [(orig [3 * I + 2] & 0x3f) & 0x3F];
  19. }
  20. // Now, take padding into account. (Note: I = numOrig24BitValues)
  21. If(HavePadding)
  22. {
  23. Result [4 * I + 0] = base64Char [(orig [3 * I]> 2) & 0x3F];
  24. If(HavePadding2)
  25. {
  26. Result [4 * I + 1] = base64Char [(orig [3 * I] & 0x3) <4) | (orig [3 * I + 1]> 4) & 0x3F];
  27. Result [4 * I + 2] = base64Char [(orig [3 * I + 1] & 0x0f) <2) & 0x3F];
  28. }
  29. Else
  30. {
  31. Result [4 * I + 1] = base64Char [(orig [3 * I] & 0x3) <4) & 0x3F];
  32. Result [4 * I + 2] = ';
  33. }
  34. Result [4 * I + 3] = ';
  35. }
  36. Result [numResultBytes] = '/0 ';
  37. ReturnResult;
  38. }


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.