Base64 Encoding
Tare da kayan aikin ɓoye na Base64, zaku iya ɓoye rubutun da kuka shigar da hanyar Base64. Idan kuna so, zaku iya yanke rufaffen lambar Base64 tare da kayan aikin Base64 Decode.
Menene Base64 Encoding?
Base64 Encoding wani tsari ne na ɓoyewa wanda ke ba da damar jigilar bayanan binary akan mahallin da ke amfani da wasu ƙuntataccen haruffa (muhalli waɗanda ba za a iya amfani da duk lambobin haruffa ba, kamar xml, html, rubutun, aikace-aikacen saƙon take). Adadin haruffa a cikin wannan makirci shine 64, kuma lamba 64 a cikin kalmar Base64 ta fito daga nan.
Me yasa Amfani da Base64 Encoding?
Bukatar rufaffiyar Base64 ta samo asali ne daga matsalolin da ke tasowa lokacin da ake watsa kafofin watsa labarai cikin danyen tsarin binary zuwa tsarin tushen rubutu. Saboda tsarin tushen rubutu (kamar i-mel) suna fassara bayanan binary a matsayin nau'ikan haruffa daban-daban, gami da haruffan umarni na musamman, yawancin bayanan binary ɗin da ake watsawa zuwa cibiyar canja wuri ana yin kuskure ta waɗannan tsarin kuma sun ɓace ko sun lalace a cikin watsawa. tsari.
Hanya ɗaya ta ɓoye irin waɗannan bayanan binary ta hanyar da za ta guje wa irin waɗannan matsalolin watsawa ita ce aika su azaman rubutu na ASCII a sarari a cikin tsarin Base64. Wannan yana ɗaya daga cikin dabarun da mizanin MIME ke amfani dashi don aika bayanai banda rubutu na fili. Yawancin harsunan shirye-shirye, irin su PHP da Javascript, sun haɗa da Base64 rufaffiyar da ayyukan yankewa don fassara bayanan da aka watsa ta amfani da rufaffiyar Base64.
Base64 Encoding Logic
A cikin Base64 encoding, 3 * 8 bits = 24 ragowa na bayanai da suka ƙunshi 3 bytes an raba su zuwa ƙungiyoyi 4 na 6 bits. Haruffan da suka yi daidai da ƙimar decimal tsakanin [0-64] na waɗannan ƙungiyoyin 6-bit 4 an daidaita su daga tebur na Base64 don ɓoyewa. Adadin haruffan da aka samu sakamakon rufaffiyar Base64 dole ne ya zama maɓalli na 4. Rufaffen bayanan da ba adadi na 4 ba basu da inganci na Base64. Lokacin da aka ɓoye tare da Algorithm na Base64, lokacin da ɓoyayyen ya cika, idan tsawon bayanan ba mahara na 4 ba ne, ana ƙara halayen "=" (daidai) zuwa ƙarshen rufaffen har sai ya zama mahara na 4. Misali, idan muna da bayanai masu haruffa 10 Base64 sakamakon rufaffen, "==" biyu ya kamata a ƙara zuwa ƙarshe.
Misalin Rubutun Base64
Misali, ɗauki lambobin ASCII guda uku 155, 162 da 233. Waɗannan lambobin guda uku suna samar da rafin binary na 100110111010001011101001. Fayil ɗin binary kamar hoto yana ƙunshe da rafi na binary wanda ke aiki don dubun ko ɗaruruwan dubunnan sifilai da ɗaya. A Base64 encoder yana farawa ta hanyar rarraba rafin binary zuwa rukuni na haruffa shida: 100110 111010 001011 101001. Kowane ɗayan waɗannan rukunin ana fassara shi zuwa lambobi 38, 58, 11, da 41. Ana juyar da rafin binary mai haruffa shida tsakanin binary (ko asali). 2) zuwa haruffa goma (tushe-10) ta hanyar karkatar da kowace ƙima da 1 ke wakilta a cikin tsararrun binary ta wurin murabba'in matsayi. Fara daga dama da matsawa zuwa hagu kuma farawa a sifili, ƙimar da ke cikin rafin binary suna wakiltar 2^0, sannan 2^1, sannan 2^2, sannan 2^3, sannan 2^4, sannan 2^ 5.
Ga wata hanyar duba ta. Fara daga hagu, kowane matsayi yana da daraja 1, 2, 4, 8, 16 da 32. Idan ramin yana da lambar binary 1, kun ƙara wannan darajar; idan ramin yana da 0, an ɓace. Binary Array 100110 ya juya 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 yana ɗaukar wannan layin binary kuma ya raba shi zuwa ƙimar 6-bit 38, 58, 11 da 41. A ƙarshe, ana canza waɗannan lambobi zuwa haruffa ASCII ta amfani da Teburin ɓoye na Base64.