Base64 गाइड
ये व्यावहारिक गाइड बताते हैं कि Base64 क्या करता है, कब उपयोगी है और वास्तविक डेटा की समस्याओं को कैसे ठीक किया जाए। जब तक अलग से न कहा जाए, उदाहरण मानक Base64 का उपयोग करते हैं।
संबंधित स्वतंत्र ट्यूटोरियल
- Base64 क्या है?
- JavaScript और UTF-8
- Python Base64
- Base64URL और padding
- Data URL इमेज
- UTF-8 character sets
- JWT सुरक्षित रूप से पढ़ना
- आम त्रुटियां
1. Base64 क्या है
Base64 binary-to-text encoding है। यह हर तीन input bytes को A-Z, a-z, 0-9, plus (+) और slash (/) से चुने गए चार printable characters में बदलता है। अगर मूल लंबाई तीन से विभाजित नहीं होती, तो आखिरी group में equal sign (=) padding देता है। इसलिए Base64 representation आम तौर पर लगभग एक तिहाई बड़ी हो जाती है। Encoding जानकारी छिपाती नहीं है: जिसके पास string है वह उसे decode कर सकता है।
यह format तब उपयोगी है जब transport text चाहता है लेकिन data binary हो। Email MIME parts, JSON fields, data URLs, configuration files और API payloads सामान्य उदाहरण हैं। बड़े files के लिए यह कम उपयुक्त है क्योंकि size बढ़ने से bandwidth और memory overhead बढ़ता है।
2. Text सही तरीके से encode करना
Text को पहले एक तय character set से bytes में बदलना चाहिए। Modern applications के लिए UTF-8 सबसे सुरक्षित default है क्योंकि यह Unicode को consistent तरीके से represent करता है। café string visual characters से सीधे encode नहीं होती; उसके UTF-8 bytes encode होते हैं।
Browser workflow में source charset चुनें, text paste करें और result देखें। Line breaks को जानबूझकर preserve करें: पूरा paragraph encode करना और हर line अलग encode करना अलग strings देता है। API के साथ data exchange करते समय charset और LF/CRLF normalization document करें।
3. Text और files decode करना
Decoded bytes हमेशा text नहीं होते। PNG, PDF, ZIP archive या certificate सफलतापूर्वक decode हो सकता है लेकिन text box में meaningless दिख सकता है। जब input data:application/pdf;base64,... जैसे data URL से शुरू हो या मूल content binary हो, तो file-oriented decoder उपयोग करें।
Email और documentation systems अक्सर Base64 में whitespace डाल देते हैं। Standard decoders harmless line breaks हटा सकते हैं, लेकिन alphabet के अंदर accidental edit data corrupt कर सकता है। Troubleshooting में file signature और length compare करें।
इसे Base64 डिकोडर के साथ आज़माएं।
4. Base64URL और padding
URLs plus और slash को खास तरीके से treat करते हैं, इसलिए Base64URL + को - और / को _ से बदलता है। कई producers trailing padding भी हटा देते हैं। URL-safe decoder को final bytes बनाने से पहले expected padding restore करना चाहिए।
Base64URL JSON Web Tokens और URL parameters में common है। यह encryption नहीं है और token को trustworthy नहीं बनाता। Applications को अभी भी authenticate, authorize, length validate और secrets को secure token design या encryption से protect करना चाहिए।
5. HTML और CSS में Data URLs
Data URL media type और encoded payload को एक string में embed करता है, जैसे data:image/png;base64,...। यह छोटे icons, test fixtures या self-contained demos के लिए convenient है। बड़े data URLs HTML/CSS को cache और maintain करना कठिन बनाते हैं, इसलिए सामान्य static assets अक्सर बेहतर होते हैं।
Data URL decode करते समय comma से पहले metadata को Base64 payload से अलग रखें। Media type बताता है कि bytes को कैसे interpret करना है; यह proof नहीं है कि bytes valid हैं। Untrusted content दिखाने से पहले file signatures validate करें।
6. APIs और JWTs में Base64
APIs binary fields के लिए अक्सर Base64 उपयोग करते हैं, लेकिन JSON खुद text है और ordinary strings के लिए Base64 की जरूरत नहीं होती। JWTs में periods से अलग header और payload Base64URL encoded होते हैं। ये readable encodings हैं, confidential storage नहीं।
API integrate करते समय record करें कि padding required है या नहीं, line breaks allowed हैं या नहीं, expected charset क्या है और maximum payload size कितना है। Empty input, Unicode text, one-byte/two-byte values और malformed characters test करें।
7. Security और privacy
Base64 अधिक से अधिक obfuscation देता है। यह message authenticate नहीं करता, tampering नहीं रोकता और secret protect नहीं करता। Decoded content को untrusted input मानें और parsing, rendering या execution से पहले normal validation लगाएं।
Confidential workflows के लिए अपनी organization के controls के तहत local command-line या offline library उपयोग करें। Transport security के लिए HTTPS और data के लिए designed encryption या authenticated-encryption scheme उपयोग करें।
8. आम errors और checklist
“Invalid character” आम तौर पर तब आता है जब standard decoder को URL-safe characters या unrelated punctuation मिले। “Incorrect padding” अक्सर truncated string या हटाए गए final equals signs बताता है। Garbled text charset mismatch दिखाता है; successful decode के बाद unreadable preview का मतलब binary result भी हो सकता है।
Diagnosis में क्रम से देखें: complete value copied है, standard या URL-safe alphabet पहचाना गया है, सिर्फ permitted whitespace हटाया गया है, original charset चुना गया है, byte length compare हुई है और expected file signature inspect की गई है। SGVsbG8= जैसे known-good test को रखें।
इसे Base64 एन्कोडर के साथ आज़माएं।
const text = "Hello, Base64!";
const encoded = btoa(unescape(encodeURIComponent(text)));
console.log(encoded); // SGVsbG8sIEJhc2U2NCE=
const decoded = decodeURIComponent(escape(atob(encoded)));
console.log(decoded);
import base64
value = "你好, Base64"
encoded = base64.b64encode(value.encode("utf-8")).decode("ascii")
decoded = base64.b64decode(encoded).decode("utf-8")
print(encoded)
print(decoded)
# Decode a text value on macOS or Linux
printf 'SGVsbG8=' | base64 --decode
# URL-safe decoding in Python
import base64
base64.urlsafe_b64decode("SGVsbG8")
सामग्री समीक्षा: 30 अगस्त 2026