Base64 algorithm principle and encoding and decoding [encryption and decryption] _ PHP Tutorial

Source: Internet
Author: User
Base64 algorithm principle, as well as encoding and decoding [encryption and decryption] introduction. Base64 encoding is a commonly used encoding method in program development. It is a representation of binary data based on 64 printable characters. It is usually used for storage and Base64 encoding. it is often used in program development. It is a representation of binary data based on 64 printable characters. It is usually used to store and transmit binary data encoding methods! It is also a common MIME (multi-purpose Internet mail extension, mainly used as the e-mail standard) encoding method that can print characters to represent binary data! It is actually just a way to define the transfer content with printable characters, and does not produce a new character set! Sometimes, after learning the conversion idea, we can also combine our actual needs to construct some of our own interfaces to define encoding methods. Okay. let's take a look at it. let's take a look at its conversion ideas!

Base64 conversion principle

It uses 64 printable characters to represent all binary data. Because the power of 6 in 2 is equal to 64, each 6-digit dollar can be used as a unit, corresponding to a printable character. We know that the three bytes have 24 bits, which exactly corresponds to four Base64 units, that is, the three bytes need to be expressed by four Base64 printable characters. 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 generally different in different systems. However, the other two characters that we often call Base64 are: "+ /". The 64 characters are as follows.

No. Character No. Character No. Character No. Character
0 A 16 Q 32 G 48 W
1 B 17 R 33 H 49 X
2 C 18 S 34 I 50 Y
3 D 19 T 35 J 51 Z
4 E 20 U 36 K 52 0
5 F 21 V 37 L 53 1
6 G 22 W 38 M 54 2
7 H 23 X 39 N 55 3
8 I 24 Y 40 O 56 4
9 J 25 Z 41 P 57 5
10 K 26 A 42 Q 58 6
11 L 27 B 43 R 59 7
12 M 28 C 44 S 60 8
13 N 29 D 45 T 61 9
14 O 30 E 46 U 62 +
15 P 31 F 47 V 63 /

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 zone is supplemented with 0. Then, retrieve 6 bits each time and select the values according to the values.
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
As the encoded output. Continue until all input data is converted.

If the last two inputs are left, add 1 "=" after the encoding result. if the last one is left, add 2 "=" after the encoding result. if no data is left, do not add anything to ensure the correctness of data restoration.

The encoded data is slightly longer than the original data, which is 4/3 of the original data. All characters are encoded, so some printable characters are not encoded like Quoted-printable. Therefore, it is not as readable as Quoted-printable encoding!

Text M A N
ASCII code 77 97 110
Binary bit 0 1 0 0 1 1 0 1 0 1 1 0 0 0 0 1 0 1 1 0 1 1 1 0
Index 19 22 5 46
Base64 encoding T W F U

The Ascii code of M is 77, the first six digits correspond to 19 characters, and the base64 character is T, and so on. Other character codes can be automatically converted! Let's see if it's not exactly 3 bytes!

Text (1 Byte) A
Binary bit 0 1 0 0 0 0 0 1
Binary bit (0) 0 1 0 0 0 0 0 1 0 0 0 0
Base64 encoding Q Q = =
Text (2 bytes) B C
Binary bit 0 1 0 0 0 0 1 0 0 1 0 0 0 0 1 1 X X X X X X
Binary bit (0) 0 1 0 0 0 0 1 0 0 1 0 0 0 0 1 1 0 0 X X X X X X
Base64 encoding Q K M =

Base64 conversion code implementation

Now that we know the method, it seems easy to write a simple conversion by ourselves! Next, I will write down my php code for conversion!

/**

* The base64 encoding method. this method only provides an example of the base64 conversion process code. the example can be used to modify different language versions.

* @ Author Cheng Mo

*@copyright http://blog.chacuo.net

* @ Param $ src original string

* @ Return string base64 string *

*/

function c_base64_encode($src)

{

static $base="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

//// Convert the original three bytes into four bytes

$slen=strlen($src);

$smod = ($slen%3);

$snum = floor($slen/3);

$desc = array();

for($i=0;$i<$snum;$i++)

{

//// Read 3 bytes

$_arr = array_map('ord',str_split(substr($src,$i*3,3)));

/// Calculate each base64 value

$_dec0= $_arr[0]>>2;

$_dec1= (($_arr[0]&3)<<4)|($_arr[1]>>4);

$_dec2= (($_arr[1]&0xF)<<2)|($_arr[2]>>6);

$_dec3= $_arr[2]&63;

$desc = array_merge($desc,array($base[$_dec0],$base[$_dec1],$base[$_dec2],$base[$_dec3]));

}

if($smod==0) return implode('',$desc);

/// Calculate non-3-fold bytes

$_arr = array_map('ord',str_split(substr($src,$snum*3,3)));

$_dec0= $_arr[0]>>2;

/// Only one byte

if(!isset($_arr[1]))

{

$_dec1= (($_arr[0]&3)<<4);

$_dec2=$_dec3="=";

}

else

{

/// 2 bytes

$_dec1= (($_arr[0]&3)<<4)|($_arr[1]>>4);

$_dec2= $base[($_arr[1]&7)<<2];

$_dec3="=";

}

$desc = array_merge($desc,array($base[$_dec0],$base[$_dec1],$_dec2,$_dec3));

return implode('',$desc);

}

 

Okay. In this example, I want to know some about the base64 encoding conversion principle and algorithm! The conversion process is very simple. you only need to create a ing table and then perform some original shift operations! In this example, can we make our own base32 encoding! Welcome to the discussion!

Author: Cheng Mo's blog QQ: 8292669
Http://blog.chacuo.net/719.html
Subscription keep-in: http://blog.chacuo.net/feed
This article is copyrighted by the Author. You are welcome to reprint it. please add the original article link.

Bytes. It is a representation of binary data based on 64 printable characters. It is usually used for storage, transmission...

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.