[Switch] the Base64 algorithm principle, the coding and decoding [encryption, decryption introduction]

    Base64 encoding is often used in the development of our program to the encoding method. It is based on a 64 printable characters to represent binary data representation. It is commonly used as storage, transfer some binary data encoding method! Also MIME (Multipurpose Internet Mail Extensions, is mainly used as a standard e-mail) of one common printable characters represent binary data encoding method! It is only defined by the character a way to transfer content to print, and no new characters! Sometimes, after we study the idea of ​​conversion, in fact, we can combine their actual needs, construct some of your own interface definition encoding. Well, we take a look, it's right to convert ideas!

Base64 achieve conversion principle

    It is a method for binary data 64 for all printable characters. Since the sixth power of 2 equal to 64, it can be used as a 6 bits per cell, corresponding to a printable character. We know that three bytes 24 bytes, can just Base64 corresponding to four units, i.e. 3 bytes need Base64 4 printable characters to represent. Base64 printable characters includes the letters AZ, az, 0-9, so that a total of 62 characters, in addition to two printable characters generally vary in different systems. However, we often say Base64 other two characters are: "+ /." These 64 characters, the correspondence table is as follows.

Numbering character   Numbering character   Numbering 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 from
4 E 20 The 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 FROM 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 THE 30 e 46 in 62 +
15 P 31 f 47 v 63 /

    Conversion when the three byte of data into the buffer has a 24bit, the first to occupy the high byte. Insufficient data 3byte words, in the buffer remaining bit with zeros. Then, in each case taking 6 'bit, which value is selected in accordance with
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
the character as the encoded output. Continues until all of the input data conversion is complete.

    If the last remaining two input data, plus an "=" after encoding result; if the last remaining one of the input data, the encoding results plus two "="; if there is no rest any data, do not add anything , so that it can ensure the correctness of data reduction.

    The encoded data is slightly longer than the original data, the original 4/3. No matter what kind of character will all be encoded, so unlike Quoted-printable encoding, but also retain some printable characters. So, it's not as good readability Quoted-printable encoding!

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

     Ascii character of M is 77, the value 19 corresponding to the first six, the corresponding base64 characters is T, and so on. Other character encoding can be automatically converted! We look at another situation is not exactly 3 bytes!

Text (1 Byte) A    
Bit 0 1 0 0 0 0 0 1                                
Bits (0s) 0 1 0 0 0 0 0 1 0 0 0 0                        
Base64 encoding Q Q = =
Text (2 Byte) B C  
Bit 0 1 0 0 0 0 1 0 0 1 0 0 0 0 1 1     x x x x x x
Bits (0s) 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 the method, if we want to write their own simple conversion, it seems, very easy! Now, I do write my conversion php code!

<? PHP 

/ * * 

* base64 encoding method, the method just do base64 code illustrates the conversion process may be arbitrarily transform this example by a different language version 

* @ author Cheng Mo 

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

* @ param $ src original string 

* @ return string base64 string * 

* / 

function c_base64_encode ( $ the src ) 

{ 

static  $ Base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 + /" ; 

// // convert the original 3 bytes 4 bytes 

$ sLen = strlen ( $ the src ); 

$ SMOD = ( $ sLen %. 3 ); 

$ SNUM = Floor ( $ sLen /. 3 );

desc $ = Array (); 

for ( $ I = 0; $ I < $ SNUM ; $ I ++ ) 

{ 

// // read three bytes 

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

// / a base64 value calculated for each 

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

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

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

$ _dec3 = $ _arr [2] & 63 is ; 

$ desc = The array_merge ( $ desc , Array ( $ Base [ $ _dec0 ], $ Base [ $ _dec1 ], $ Base [ $ _dec2 ] , $ Base [ $ _dec3 ])); 

} 

IF ( $ SMOD == 0) return  The implode ( '', $ desc ); 

// / non-calculating multiples of 3 bytes 

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

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

// / only one byte 

IF (! isset ( $ _arr [. 1 ])) 

{ 

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

$ _dec2 = $ _dec3 = "=" ; 

} 

the 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);

}

 

base64 encoding conversion principle 

    Well, by this example, I want to base64 encoding conversion principles, algorithms and some understanding of it! It is the conversion process is very simple, just need to be a map, and then do some original shift operation can be done! Through this example, it is not to be his own kind of base32 encoding it! Welcome friends to exchange!

 

---------------------
Author: Cheng Mo
Source: CNBLOGS
Original: https: //www.cnblogs.com/chengmo/archive/2014/05/18/ 3735917.html
Disclaimer: This article is the author original article, reproduced, please attach Bowen link!

Guess you like

Origin www.cnblogs.com/vilogy/p/12583770.html
Recommended