Analysis of Base64 encoding principles and PHP implementation

Source: Internet
Author: User
Tags printable characters
Analysis of Base64 encoding principles and PHP implementation Base64Is a representation of binary data based on 64 printable characters. Because the power of 6 in 2 is equal to 64, every 6 characters is a unit, corresponding to a printable character.

The three bites have 24 bits, corresponding to four Base64 units. that is, three bytes are represented by four printable characters. It can be used as the transmission code of the email.

The printable characters in Base64 include letters A-Z, a-z, and numbers 0-9, which have A total of 62 characters. the Two printable characters are different in different systems.

For example, in mime (multi-purpose mail extension), the 64 printable characters used by Base64

A-Za-z: 26 uppercase/lowercase letters each

0-9: add 10 numbers

+: Plus sign

/: Slash

A total of 64 characters. the equal sign "=" is used as the suffix.

The conversion relationship is

0-63: A-Za-z0-9 +/


During the conversion, three bytes of data are successively put into a 24-bit buffer, and the first byte occupies a high position. If the data is less than 3 bytes, the remaining bit in the buffer is supplemented with 0. Then, 6 bits (because 26 = 64) are extracted each time, and the characters in ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 +/are selected as the encoded output. Continue until all input data is converted.

When the original data length is not an integer multiple of 3, if the last input data is left, add two "=" after the encoding result; if the last two input data are left, add 1 "=" after the encoding result. if no data is left, do not add anything to ensure the correctness of data restoration.

Instance analysis:

Code: "Lailaiji"

Find the corresponding relationship by querying the ASCII table

L: 0x4c | a: 0x61 | I: 0x69 | l: 0x6C | j: 0x6A

Therefore, convert to binary: 0100 1100,011 0 0001,011 0 1001,011 0 1100,011 0 0001,011 0 1001,011 0 1010,011 0 1001

Step 1: First take the data of three bytes: 0100 1100,011 0 0001,011 0 1001, then take 6 digits from the three bytes, that is, 010011, adding two bits to the maximum bit 00 makes it a byte, that is, 0001 0011, and the remaining 18 bits are also in this loop. In the end, these three bytes will be extended into four bytes: 0001 0011,000 0 0110,000 0 0101,001 0 1001

Step 2: repeat the first step from the remaining byte sequence to the step smaller than 3 bytes.

Step 3: The remaining byte is 0110 1001, 0110 less than 3 bytes, and must be supplemented with 0 from the low position, that is, 0000, repeat the first step: 0001 0110,001 0100,000 0000 0,

After subsequent operations, we will get a set of bit sequences:

0001 0011,000 0 0110,000 0 0101,001 0 1001

0001 0110,000 0101,001 1001 0

0001 0110,001 0100,000 0000 0

Convert to decimal :,

The corresponding characters are T, G, F, p, B, G, F, p, a, m, k, and.

It is particularly important that the last byte is 0000 0000, that is, 0x00. The table is A. because the last eight bits are supplemented, it should be converted to A =, instead of.

Therefore, the final result is: TGFpbGFpamk =

PHP implementation:

 Encode ($ input); echo "Encode:", $ output. PHP_EOL; $ output = $ obj-> decode ($ output); echo "Decode:", $ output. PHP_EOL; class MyBase64 {private $ _ table = array (); private $ _ revtable = array (); public function _ construct () {$ this-> _ initTable ();} public function decode ($ string) {$ orign_len = strlen ($ string); $ j = 0; $ ret = null; for ($ I = 0; $ I <$ orign_len; $ I + = 4) {$ chr1 = $ this-> getRevChr ($ string [$ I]); $ chr2 = $ this-> getRev Chr ($ string [$ I + 1]); $ chr3 = $ this-> getRevChr ($ string [$ I + 2]); $ chr4 = $ this-> getRevChr ($ string [$ I + 3]); $ _ chr1 = $ chr1 <2 | ($ chr2 & 0x3F)> 4; $ _ chr2 = ($ chr2 & 0x0F) <4 | ($ chr3 & 0xFC)> 2; $ _ chr3 = ($ chr3 & 0x03) <6 | $ chr4; $ ret. = chr ($ _ chr1); $ ret. = chr ($ _ chr2); $ ret. = chr ($ _ chr3);} $ ret = rtrim ($ ret); return $ ret;} private function getRevChr ($ chr) {if (isset ($ this-> _ revtable [$ chr]) {return $ this-> _ revtable [$ ch R];} else {return 0;} public function _ decode ($ string) {$ orign_len = strlen ($ string); $ de = null; $ kv = array_flip ($ this-> _ table); $ B = null; for ($ I = 0; $ I <$ orign_len; $ I ++) {$ chr = $ string [$ I]; if ($ chr! = ') {$ C = $ kv [$ chr];} else {$ c = chr (0);} printf ("% x", $ c ); $ B [] = pack ('C', $ C); echo PHP_EOL ;}for ($ I = 0; $ I <count ($ B ); $ I + = 3) {$ region = ($ B [$ I] <2) | ($ B [$ I + 1]> 4 ); $ ch2 = ($ B [$ I + 1] <4) | ($ B [$ I + 2]> 2 ); $ ch3 = ($ B [$ I + 2] <6) | ($ B [$ I + 3]); printf ('% 08b, % 08b, % 08b', $ scheme, $ ch2, $ ch3); echo PHP_EOL; printf ('% 08b, % 08b', ($ B [$ I] <2 ), ($ B [$ I + 1]> 4); echo PHP_EOL;} public function encode ($ string) {$ orign_len = strlen ($ string ); $ len = intval (ceil ($ orign_len/3) * 3); $ bin = pack ('A '. $ len, $ string); $ gen = null; for ($ I = 0; $ I <$ len; $ I + = 3) {$ Pipeline = ord ($ bin [$ I])> 2; $ ch2 = (ord ($ bin [$ I]) & 0x03) <4) | (ord ($ bin [$ I + 1])> 4); $ ch3 = (ord ($ bin [$ I + 1]) & 0x0F) <2) | (ord ($ bin [$ I + 2]) & 0xC0)> 6); $ methane = ord ($ bin [$ I + 2]) & 0x3F; $ gen. = $ this-> _ table [$ Partition]; $ gen. = $ this-> _ table [$ ch2]; $ gen. = $ this-> _ table [$ ch3]; $ gen. = $ this-> _ table [$ methane];} if ($ orign_len-$ len) {$ gen = substr ($ gen, 0, -abs ($ orign_len-$ len); for ($ I = 0; $ I <$ len-$ orign_len; $ I ++) {$ gen. = ';}} return $ gen;} private function _ initTable () {$ tbl = array (); for ($ I = ord ('A '); $ I <= ord ('Z'); $ I ++) {$ tbl [] = chr ($ I );} for ($ I = ord ('A'); $ I <= ord ('Z'); $ I ++) {$ tbl [] = chr ($ I) ;}for ($ I = ord ('0'); $ I <= ord ('9 '); $ I ++) {$ tbl [] = chr ($ I) ;}$ tbl [] = '+'; $ tbl [] = '/'; $ reverse = array_flip ($ tbl); $ this-> _ table = $ tbl; $ this-> _ revtable = $ reverse ;}}

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.