Translated from http://www.yanshiba.com/archives/638
1: Why do I need base64?
The ASCII code specifies a total of 128-character encodings, which are 128 symbols, ranging between [0,127].
Among them, [0,31], and 127, 33 belong to the non-printable control characters.
When e-mail messages are transmitted, some mail gateways silently erase [0,31] these control characters.
There are also early programs that receive an international character between [128,255] when there is even an error.
How to secure the transmission between different mail gateways control characters, international characters, and even binary files?
-base64 was developed as part of the MIME multimedia e-mail standard.
1.a What is url_safe base64 encoding?
In the above base64 traditional code will appear +,/two will be directly escaped by the URL symbol, so if you want to transfer these encoded strings through a URL, we
You need to do the traditional base64 code, and then replace the + and/respectively with-_ two characters, at the receiving end do the opposite action decoding
Http://www.ietf.org/rfc/rfc4648.txt
/** * URL base64 decode * '-' + ' + ' * ' _ '/' * string length%4 remainder, complement ' = ' * @param unknown $string
*/ functionUrlsafe_b64decode ($string) { $data=Str_replace(Array(‘-‘,‘_‘),Array(' + ', '/'),$string); $mod 4=strlen($data)% 4; if($mod 4) { $data.=substr(' = = = ',$mod 4); } return Base64_decode($data); } /** * URL Base64 code * ' + '-'-' * '/' _ ' * ' = ' * @param unknown $string*/ functionUrlsafe_b64encode ($string) { $data=Base64_encode($string); $data=Str_replace(Array(' + ', '/', ' = '),Array(‘-‘,‘_‘,‘‘),$data); return $data; }
2: How does base64 work after a sentence?
The 8*n bits corresponding to the contents of N bytes are chopped into 1 segments per 6 bits, resulting in a (8*n)/6 unit,
The value of each unit is between [0,63], and the value corresponds to 1 ASCII characters, stitched together, ok!
Base64_encode (' PHP ') ==> ' UEHQ ', the encoding process is as follows:
3: What if every 6 bits are cut into 1 segments, but not divisible, and the remaining 2 bits or 4 bits?
Use "0″" to fill 6 bits and convert again to a character in the Base64 character list.
Then, with the "=" character as 6 bits, continue filling until the total number of digits can be divisible by 8.
String |
Binary sequence (red Word is fill bit) |
Coded results |
Php |
010100 000100 100001 010000 |
Uehq |
It |
011010 010111 0100 XX xxxxxx |
axq= |
bool |
011000 100110 111101 101111 011011 00 0000 xxxxxx xxxxxx |
ym9vba== |
4:base64 represents a picture
As you can see from the demo above, Base64 can also encode binary files, such as pictures and attachments in messages.
After encoding, we can be in the Web page or the source of the mail, directly reflect this picture,
Instead of putting pictures on the server, refer to their links.
Use case: Base64 (' abc.png ') ==> ' Encoded-result ';
In the Web page,
See this 5-point star? Right-click on the source, you will find the picture is a string
5:base64 byte changes after encoding
It is easy to deduce that, after encoding, each of the 6 bits becomes 8 bits.
Therefore, the word saving after encoding is 33% more than before encoding.
6:base64 the end of the string "=" can be removed?
From the above coding rules can be reversed, in the process of Base64 decoding, to erase the end of the equal sign,
Then the "Base64 Index and letter comparison table", converted to the original sequence of bytes.
Then, removing the trailing equals sign does not lose the original information, but the structure becomes non-canonical.
Whether the integrity is judged before decoding, it depends on your application.
The Base64_decode function in PHP is measured and does not detect the completeness of the trailing equals sign.
How does the Base64 code and the URL safe base64 work?