Base64 encoding yog txheej txheem ntawm kev hloov cov ntaub ntawv binary mus rau ASCII txoj hlua hom los ntawm kev hloov cov ntaub ntawv binary rau hauv 6-ntsis cim sawv cev. Base64 txoj kev encoding yog siv thaum cov ntaub ntawv binary, xws li cov duab lossis video, raug xa mus rau cov tshuab uas tau tsim los xa cov ntaub ntawv hauv cov ntawv dawb (ASCII) hom.
Vim li cas Base64 Encoding siv?
Qhov xav tau rau Base64 encoding los ntawm cov teeb meem uas tshwm sim thaum cov xov xwm raug xa mus rau hauv cov ntaub ntawv nyoos binary rau cov ntawv nyeem.
Vim cov ntawv nyeem raws li cov kab ke (xws li email) txhais cov ntaub ntawv binary ua ntau yam cim, suav nrog cov cim tshwj xeeb, ntau ntawm cov ntaub ntawv binary uas tau xa mus rau kev hloov xov xwm raug txhais yuam kev los ntawm cov tshuab thiab ploj lossis puas hauv txheej txheem kis tau tus mob.
Ib txoj hauv kev encoding hom binary cov ntaub ntawv nyob rau hauv ib txoj kev uas tsis txhob muaj xws li cov teeb meem kis tau tus mob yog xa nws raws li cov ntawv ASCII dawb hauv Base64 encoded hom. Qhov no yog ib qho ntawm cov txheej txheem ua haujlwm los ntawm MIME tus qauv xa cov ntaub ntawv uas tsis yog cov ntawv dawb.
Ntau hom lus programming, xws li PHP thiab Javascript, suav nrog Base64 encoding thiab decoding functions txhawm rau txhais cov ntaub ntawv xa mus siv Base64 encoding.
Base64 Encoding Logic
Base64 encoding rhuav tshem cov ntaub ntawv binary rau hauv 6-ntsis ntu ntawm 3 tag nrho bytes thiab sawv cev rau cov ntawv luam tawm hauv ASCII tus qauv. Nws ua qhov ntawd hauv ob kauj ruam tseem ceeb.
Thawj kauj ruam yog txhawm rau binary hlua cia rau hauv 6-ntsis blocks. Base64 tsuas yog siv 6 khoom (xws li 2^6=64 cim) kom ntseeg tau tias cov ntaub ntawv encoded tuaj yeem luam tau thiab tib neeg nyeem tau. Tsis muaj cov cim tshwj xeeb muaj nyob hauv ASCII raug siv.
Tus 64 tus cim (li no lub npe Base64) yog 10 tus lej, 26 tus lej me, 26 tus tsiaj ntawv loj nrog rau cov cim ntxiv (+) thiab Forward Slash (/). Kuj tseem muaj 65 tus cim hu ua lub ncoo, uas yog lub cim sib npaug (=). Cov cim no yog siv thaum ntu kawg ntawm cov ntaub ntawv binary tsis muaj tag nrho 6 cov khoom.
Base64 Encoding Piv txwv
Piv txwv li, coj peb tus lej ASCII 155, 162, thiab 233. Cov peb tus lej no suav nrog binary kwj ntawm 100110111010001011101001. Ib daim ntawv binary, zoo li ib daim duab, muaj cov kwj binary khiav rau kaum lossis ntau pua txhiab tus. thiab ib qho.
A Base64 encoder pib los ntawm chunking binary kwj rau hauv pawg ntawm rau lub cim: 100110 111010 001011 101001. Txhua pawg no txhais ua cov lej 38, 58, 11, thiab 41.
A rau-tus cim binary kwj hloov ntawm binary (los yog hauv paus-2) mus rau tus lej (hauv paus-10) cov cim los ntawm squaring txhua tus nqi sawv cev los ntawm 1 hauv binary sib lawv liag nrog nws qhov chaw square. Pib los ntawm sab xis thiab txav mus rau sab laug, thiab pib nrog xoom, qhov tseem ceeb hauv binary kwj sawv cev 2^0, tom qab ntawd 2^1, tom qab ntawd 2^2, tom qab ntawd 2^3, ces 2^4, ces 2^5.
Ntawm no yog lwm txoj hauv kev los saib. Pib los ntawm sab laug, txhua txoj haujlwm muaj nqis 1, 2, 4, 8, 16, thiab 32. Yog tias tus lej binary muaj 1 hauv qhov, koj ntxiv tus nqi ntawd; yog tias nws muaj 0 hauv lub qhov, koj tsis ua. Cov hlua binary 100110 hloov mus rau tus lej lej 38: 02^01 + 12^1 + 12^2 + 02^3 + 02^4 + 12^5=0 + 2 +4+0+0+32.
Base64 encoding siv txoj hlua binary no thiab rhuav tshem nws mus rau hauv 6-ntsis qhov tseem ceeb 38, 58, 11 thiab 41.
Thaum kawg, cov lej no tau hloov pauv mus rau ASCII cov cim siv lub Base64 encoding table. Tus nqi 6-ntsis ntawm qhov piv txwv no txhais mus rau ASCII ib ntus m6Lp.
Siv lub rooj hloov pauv hauv Base64:
- 38 yog m
- 58 yog 6
- 11 is L
- 41 yog p
txheej txheem ob-kauj ruam no yog siv rau tag nrho binary hlua uas tau encoded.
Txhawm rau kom ntseeg tau tias cov ntaub ntawv encoded tuaj yeem luam tawm tau zoo thiab tsis pub dhau ib qho kev xa ntawv tus neeg rau zaub mov txoj kab ntev txwv, cov cim kab tshiab tau muab tso rau kom cov kab ntev hauv qab 76 cim. Cov cim kab tshiab tau encoded zoo li tag nrho lwm cov ntaub ntawv.
Lub hom phiaj tag nrho ntawm Base64 encoding, los ntawm kev ntxiv padding kom khaws 3-byte binary ntu kom hloov binary rau cov ntawv siv Base64 lub rooj, yog los khaws kev ncaj ncees ntawm cov ntaub ntawv sib kis binary.
Base64 Encoding Table
Cov lus hauv qab no txhais tag nrho 64 tus cim siv hauv Base64 encoding.
Base64 Encoding Table | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
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 | 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 | / |
daws qhov kawg
Thaum kawg ntawm cov txheej txheem encoding, tej zaum yuav muaj teeb meem. Yog tias qhov loj ntawm cov ntaub ntawv qub hauv bytes yog ntau ntawm peb, txhua yam ua haujlwm zoo. Yog tias tsis yog, tej zaum yuav muaj cov bytes khoob. Rau kev encoding kom raug, raws nraim 3-bytes ntawm binary cov ntaub ntawv yog xav tau.
Txoj kev daws teeb meem yog txhawm rau ntxiv cov bytes txaus nrog tus nqi ntawm 0 los tsim ib pawg 3-byte. Ob qhov txiaj ntsig no tau ntxiv yog tias cov ntaub ntawv xav tau ib qho ntxiv ntawm cov ntaub ntawv, ib qho ntxiv rau ob bytes ntxiv.
Tau kawg, cov khoom siv dag zog '0's tsis tuaj yeem raug encoded siv cov lus encoding hauv qab no. Lawv yuav tsum tau sawv cev los ntawm 65th cim. Lub Base64 padding cim yog qhov sib npaug kos npe (=) thiab muab tso rau ntawm qhov kawg ntawm cov ntaub ntawv encoded.