Home >Backend Development >PHP Tutorial >base64 custom encoding table php version

base64 custom encoding table php version

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-29 09:09:141068browse

In an interface docking, a base64 custom encoding table is used for encoding and decoding. I searched on the Internet and found that there are many and thorough explanations of the principles. The encoding examples are provided but there is no decoding. The following is my own implementation. An example of base64 custom dictionary decoding, which is relatively rough. After testing the assembly, there should be no problem. If you need this piece, you can take a look. First, take the principle from other people’s blogs

Base64 encoding, which is often used in our program development The encoding method used. It is a representation method based on using 64 printable characters to represent binary data. It is usually used as a encoding method for storing and transmitting some binary data! It is also a common encoding method for binary data represented by printable characters in MIME (Multipurpose Internet Mail Extensions, mainly used as an email standard)! It actually just defines a method of transmitting content using printable characters, and does not create a new character set! Sometimes, after we learn the idea of ​​conversion, we can actually construct some of our own interface definition coding methods based on our own actual needs. Okay, let’s take a look at its conversion ideas!

Base64 implementation conversion principle

It is a method of using 64 printable characters to represent all binary data. Since 2 to the 6th power is equal to 64, every 6 bits can be used as a unit, corresponding to a certain printable character. We know that three bytes have 24 bits, which can correspond to 4 Base64 units, that is, 3 bytes need to be represented by 4 Base64 printable characters. Printable characters in Base64 include letters A-Z, a-z, and numbers 0-9, so there are 62 characters in total. In addition, the two printable symbols are generally different in different systems. However, the other two characters of Base64 that we often refer to are: "+/". The corresponding table of these 64 characters is as follows.

Number Character Number Character Number Character Number 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 /

When converting, three bytes of data are put into a 24-bit buffer one after another, and the byte that comes first occupies the high bit. If the data is less than 3 bytes, the remaining bits in the buffer will be filled with 0s. Then, take out 6 bits at a time and select the characters in <br>ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/ according to their values ​​as the encoded output. Continue until all input data is converted.

If there are two input data left at the end, add 1 "=" after the encoding result; if there is one input data left at the end, add 2 "=" after the encoding result; if there is no data left, nothing. Do not add, so as to ensure the accuracy of data restoration.

The encoded data is slightly longer than the original data, 4/3 of the original. No matter what kind of characters, all characters will be encoded, so unlike Quoted-printable encoding, some printable characters are retained. Therefore, it is not as readable as Quoted-printable encoding!

Text M a n
ASCII encoding 77 97 110
Binary 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

M’s The Ascii code 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 look at the other situation where it's not exactly 3 bytes!

0QB0001 00001100xxxxxxBase64 encoding QkM =

这个讲的很透彻,原文地址:http://www.cnblogs.com/chengmo/archive/2014/05/18/3735917.html

class base64{
public $base64_config = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','_','-'];
public function getBytes($string) { 
$data = iconv("UTF-8","GBK",$string);
return unpack("C*",$data);

public function array_index($t){
return array_search($t, $this->base64_config);
}
public function decode($str){
$str = str_replace("!","",$str);
$slen = strlen($str);
$mod = $slen%4;
$num = floor($slen/4);
$desc = [];
for($i=0;$i<$num;$i++){
$arr = array_map("base64::array_index",str_split(substr($str,$i*4,4)));
$desc_0 = ($arr[0]<<2)|(($arr[1]&48)>>4);
$desc_1 = (($arr[1]&15)<<4)|(($arr[2]&60)>>2);
$desc_2 = (($arr[2]&3)<<6)|$arr[3];
$desc = array_merge($desc,[$desc_0,$desc_1,$desc_2]);
}
if($mod == 0) return implode('', array_map("chr",$desc));
$arr = array_map("base64::array_index", str_split(substr($str,$num*4,4)));
if(count($arr) == 1) {
$desc_0 = $arr[0]<<2;
if($desc_0 != 0) $desc = array_merge($desc,[$desc_0]);
}else if(count($arr) == 2) {
$desc_0 = ($arr[0]<<2)|(($arr[1]&48)>>4);
$desc = array_merge($desc,[$desc_0]);
}else if(count($arr) == 3) {
$desc_0 = ($arr[0]<<2)|(($arr[1]&48)>>4);
$desc_1 = ($arr[1]<<4)|(($arr[2]&60)>>2);
$desc = array_merge($desc,[$desc_0,$desc_1]);
}
return implode('', array_map("chr",$desc));
}
public function encode($str){
$byte_arr = $this->getBytes($str);
$slen=count($byte_arr);
$smod = ($slen%3);
$snum = floor($slen/3);
$desc = array();
for($i=1;$i<=$snum;$i++){
$index_num = ($i-1)*3;
$_dec0= $byte_arr[$index_num+1]>>2;
$_dec1= (($byte_arr[$index_num+1]&3)<<4)|($byte_arr[$index_num+2]>>4);
$_dec2= (($byte_arr[$index_num+2]&0xF)<<2)|($byte_arr[$index_num+3]>>6); 
$_dec3= $byte_arr[$index_num+3]&63;
$desc = array_merge($desc,array($this->base64_config[$_dec0],$this->base64_config[$_dec1],$this->base64_config[$_dec2],$this->base64_config[$_dec3]));
}
if($smod==0) return implode('',$desc);
$n = ($snum*3)+1;
$_dec0= $byte_arr[$n]>>2;
///只有一个字节
if(!isset($byte_arr[$n+1])){
$_dec1= (($byte_arr[$n]&3)<<4);
$_dec2=$_dec3="!";
}else{
///2个字节
$_dec1= (($byte_arr[$n]&3)<<4)|($byte_arr[$n+1]>>4);
$_dec2= $this->base64_config[($byte_arr[$n+1]&0xF)<<2];
$_dec3="!";
}
$desc = array_merge($desc,array($this->base64_config[$_dec0],$this->base64_config[$_dec1],$_dec2,$_dec3));
return implode('',$desc);
}
}

$base64 = new base64();
//echo array_search("E",$base64->base64_config);
//exit;
$tt = $base64->encode("中文那在场也不怕asdasdas23232323,。、");
echo $tt."
";
$ttt = $base64->decode($tt);
echo $ttt."
";

以上就介绍了base64自定义编码表 php版本,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Text (1 Byte) A
Binary bits 0 1 0 0 0 0 0 1 Binary digit (0 complement)
1 0 0 0 0 0 1 0 0 0 0 Base64 encoding
Q = = Text (2 Byte)
C Binary bit
1 0 0 0 0 1 0 0 1 0 0 0 0 1 1 x x x x
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn