Base64 algorithm principle, as well as encoding, decoding "encryption, decryption" Introduction _php Tutorial

Source: Internet
Author: User
Tags printable characters
BASE64 coding is the encoding method that we often use in our program development. It is a representation of binary data based on a 64 printable character. It is commonly used as a way to store and transfer binary data encoding methods! It is also a common encoding method for the MIME (Multipurpose Internet Mail Extension, mainly used as an e-mail standard) to represent binary data in a printable character! It just defines a way to transfer content with printable characters, and does not produce a new character set! Sometimes, after we learn the idea of conversion, we can actually combine our own actual needs, to construct some of their own interface definition coding methods. All right, let's take a look at it and switch ideas!

Base64 Implementing the conversion principle

It is a binary all data method that is represented by 64 printable characters. Since 2 of the 6 is equal to 64, you can use every 6 bits as one unit, corresponding to a printable character. We know that three bytes have 24 bits, so we can just correspond to 4 Base64 units, that is, 3 bytes need to be represented by 4 Base64 printable characters. printable characters in Base64 include the letter A-Z, a-Z, number 0-9, a total of 62 characters, and two printable symbols that are generally different across systems. However, what we often call Base64 another 2 characters is: "+/". These 64 characters, the corresponding table is as follows.

number character number character number character
numbering 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 21st 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 /

When converting, the data of three bytes is placed in a 24bit buffer successively, and the first byte is the high. If the data is less than 3byte, the remaining bits in the buffer are filled with 0. Then, each time you remove 6 bits, follow the characters in their value selection
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
as the encoded output. Continue until the full input data conversion is complete.

If there are two input data left, add 1 "=" after the encoding result, and if there is a last input data, add 2 "=" after the encoding result, and if there is no data left, do not add anything, so as to guarantee the correctness of the data restoration.

The encoded data is slightly longer than the original data for the original 4/3. No matter what characters are all encoded, they are not encoded like quoted-printable, and some printable characters are retained. Therefore, it is less readable than quoted-printable encoding!

Text M A N
ASCII encoding 77 97 110
Bits 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 for M is 77, the first six digits correspond to 19, the corresponding Base64 character is T, and so on. Other character encodings can be automatically converted! Let's see if it's just a 3-byte case!

Text (1 bytes) A
Bits 0 1 0 0 0 0 0 1
Bits (complement 0) 0 1 0 0 0 0 0 1 0 0 0 0
BASE64 encoding Q Q = =
Text (2 bytes) B C
Bits 0 1 0 0 0 0 1 0 0 1 0 0 0 0 1 1 X X X X X X
Bits (complement 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, if we want to write a simple conversion, it seems to be very easy! Below, I write down I do convert PHP code!

/**

*base64编码方法、本方法只是做base64转换过程代码举例说明,通过该例子可以任意改造不同语言版

*@author 程默

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

*@param $src 原字符串

*@return string base64字符串*

*/

functionc_base64_encode($src)

{

static$base="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

////将原始的3个字节转换为4个字节

$slen=strlen($src);

$smod= ($slen%3);

$snum= floor($slen/3);

$desc= array();

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

{

////读取3个字节

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

///计算每一个base64值

$_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) returnimplode('',$desc);

///计算非3倍数字节

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

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

///只有一个字节

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

{

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

$_dec2=$_dec3="=";

}

else

{

///2个字节

$_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));

returnimplode('',$desc);

}

 
 

Well, through this example, I would like to Base64 code conversion principle, algorithm some understand it! The conversion process is very simple, only need to make a mapping table, and then the original to do some shift operations can be done! By this example, is it possible to make a code of our own base32? Welcome friends to communicate!

Author: Cheng's blog qq:8292669
Original URL: http://blog.chacuo.net/719.html
Subscribe Stay tuned: Http://blog.chacuo.net/feed
This article copyright belongs to the author, welcome reprint, please be sure to add the original link.

http://www.bkjia.com/PHPjc/771651.html www.bkjia.com true http://www.bkjia.com/PHPjc/771651.html techarticle Base64 Coding is the encoding method that we often use in our program development. It is a representation of binary data based on a 64 printable character. It is usually used as a 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.