The standard BASE64 algorithm describes

Source: Internet
Author: User

zhanhailiang 日期:2014-12-19
Algorithm Description:

Base64 requires that every three bytes of 8Bit be converted to four 6Bit bytes (38 = 46 = 24), and then add 6Bit two bit higher 0 to make up four 8Bit bytes, that is, the converted string will theoretically be 1/3 longer than the original one.

    • The first character to get the Base64 table position of the first target character by moving right 2 bits, based on this value to the corresponding character on the table, is the first target character.
    • and then the second character with the 0x0f (00001111) and the (&) operation and left 2 bits, then the third word aligns move 6 bit with the former phase or (|), the third target character is obtained.
    • Finally, the third character with the 0x3f (00111111) and (&) operation, the fourth target character is obtained.
    • After each of these steps, the result is then manipulated with the 0x3F and the encoded characters are obtained.

      The remaining bytes continue to go separately according to the coding rules (1 to 2,2 to 3; not enough digits with 0 completion), and then the = number to fill 4 bytes. This is why some Base64 encodings end with one or two equals signs, but the equals sign is only two. because:
      So the remainder can only be one of the three numbers in 0,1,2 in any case. If the remainder is 0, it means that the original byte count is exactly a multiple of 3 (ideally). If it is 1, turn to 2 Base64 encoded characters, in order to let the BASE64 encoding is a multiple of 4, it is necessary to fill 2 equal signs, similarly, if 2, it is necessary to fill 1 equal signs.

The above algorithm describes the reference from: http://baike.baidu.com/link?url= Lrifeiuvmroqg0a0r0dzs0t8gemiqrnj13esgzuyffoyudsrhy824de7zjtbhsa5rpp2nidn2-mcolpbply1q_#3_3

Algorithm implementation:

The implementation of Base64_encode is stripped out of the PHP/EXT/STANDARD/BASE64.C code as follows: https://github.com/billfeller/billfeller.github.io/tree/ Master/code/base64


Test:
[[email protected]~/wade/git/billfeller.github.io/code/base64]# gcc -o base64 base64.c [[email protected]~/wade/git/billfeller.github.io/code/base64]# ./base64 "abcd*"YWJjZCo=[[email protected]~/wade/git/billfeller.github.io/code/base64]# /usr/local/php/bin/php -r ‘echo base64_encode("abcd*").PHP_EOL;‘YWJjZCo=

The standard BASE64 algorithm describes

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.