Base64 Encoding

Ndi chida cha Base64 Encoding, mutha kubisa mawu omwe mwalowa ndi njira ya Base64. Ngati mukufuna, mutha kuyika nambala yobisidwa ya Base64 ndi chida cha Base64 Decode.

Kodi Base64 Encoding ndi chiyani?

Base64 Encoding ndi dongosolo la ma encoding lomwe limalola kuti data ya binary isamutsidwe kumalo omwe amagwiritsa ntchito ma encoding a zilembo zoletsedwa (malo omwe si ma code onse angagwiritsidwe ntchito, monga xml, html, script, mauthenga apompopompo). Chiwerengero cha zilembo mu dongosolo ili ndi 64, ndipo nambala 64 mu mawu Base64 amachokera apa.

Chifukwa Chiyani Mukugwiritsa Ntchito Base64 Encoding?

Kufunika kwa ma encoding a Base64 kumachokera kumavuto omwe amadza pomwe media imafalitsidwa mumitundu yabinare yaiwisi kupita kumakina otengera zolemba. Chifukwa machitidwe ogwiritsira ntchito malemba (monga e-mail) amatanthauzira deta ya binary monga mitundu yambiri ya zilembo, kuphatikizapo zilembo zapadera, zambiri za binary zomwe zimatumizidwa kumalo otumizira zimatanthauzidwa molakwika ndi machitidwewa ndipo zimatayika kapena zowonongeka pofalitsa. ndondomeko.

Njira imodzi yosungira deta ya binary yotereyi m'njira yopewa mavuto opatsirana ndi kuwatumiza ngati malemba omveka a ASCII mumtundu wa Base64. Iyi ndi imodzi mwa njira zomwe zimagwiritsidwa ntchito ndi muyezo wa MIME kutumiza deta ina osati mawu osavuta. Zilankhulo zambiri zamapulogalamu, monga PHP ndi Javascript, zimaphatikizanso ma encoding a Base64 ndi ntchito zomasulira kutanthauzira zomwe zimafalitsidwa pogwiritsa ntchito encoding ya Base64.

Base64 Encoding Logic

Mu encoding ya Base64, 3 * 8 bits = 24 bits ya data yopangidwa ndi 3 byte amagawidwa m'magulu 4 a 6 bits. Zilembo zomwe zimagwirizana ndi ziwerengero zapakati pa [0-64] zamagulu 4 a 6-bit awa amafananizidwa kuchokera pa tebulo la Base64 kuti alembetse. Chiwerengero cha zilembo zomwe zapezedwa chifukwa cha encoding ya Base64 ziyenera kuchulukitsa 4. Deta yosungidwa yomwe sichulukitsa 4 sizolondola za Base64. Pamene encoding ndi Base64 aligorivimu, pamene encoding watha, ngati kutalika kwa deta si kuchulukitsa 4, "=" (ofanana) khalidwe amawonjezedwa kumapeto kwa encoding mpaka kuchulukitsa kwa 4. Mwachitsanzo, ngati tili ndi data ya zilembo 10 za Base64 chifukwa cha encoding, ziwiri "==" ziyenera kuwonjezeredwa kumapeto.

Base64 Encoding Chitsanzo

Mwachitsanzo, tengani nambala zitatu za ASCII 155, 162 ndi 233. Nambala zitatuzi zimapanga mtsinje wa binary wa 100110111010001011101001. Fayilo ya binary monga chithunzi imakhala ndi mtsinje wa binary womwe umagwira ntchito makumi kapena masauzande a ziro ndi zina. Encoder ya Base64 imayamba ndikugawa mtsinje wa binary m'magulu a zilembo zisanu ndi chimodzi: 100110 111010 001011 101001. Gulu lililonse lamaguluwa limamasuliridwa kukhala manambala 38, 58, 11, ndi 41. Mtsinje wa binary wa zilembo zisanu ndi chimodzi umasinthidwa pakati pa binary (kapena zoyambira). 2) mpaka zilembo (zoyambira-10) pochulukitsa mtengo uliwonse woimiridwa ndi 1 mugulu la binary ndi masikweya okhazikika. Kuyambira kumanja ndikusunthira kumanzere ndikuyambira pa ziro, zikhalidwe zomwe zili mumtsinje wa binary zimayimira 2^0, kenako 2^1, kenako 2^2, kenako 2^3, kenako 2^4, kenako 2^ 5.

Nayi njira ina yowonera. Kuyambira kumanzere, malo aliwonse ndi ofunika 1, 2, 4, 8, 16 ndi 32. Ngati malowa ali ndi nambala ya binary 1, mumawonjezera mtengowo; ngati slot ili ndi 0, mukusowa. Mndandanda wa binary 100110 umasintha 38: 0 * 2 ^ 01 + 1 * 2 ^ 1 + 1 * 2 ^ 2 + 0 * 2 ^ 3 + 0 * 2 ^ 4 + 1 * 2 ^ 5 = 0 + 2 decimal + 4 + 0 + 0 + 32. Base64 encoding imatenga chingwe cha binarychi ndikuchigawa mu 6-bit values ​​38, 58, 11 ndi 41. Pomaliza, manambalawa amasinthidwa kukhala zilembo za ASCII pogwiritsa ntchito tebulo la encoding la Base64.