Base64 Encode and Decode

simonzhang
3 min readJul 20, 2023

--

What is Base64?

Base64 is a method of encoding binary data into text. It represents binary data in an ASCII string format by translating it into a radix-64 representation. Some key points about base64 encoding:

- It uses a 65-character alphabet made up of upper and lowercase letters, digits, and two additional characters ‘+’ and ‘/’.

- Each base64 digit represents exactly 6 bits of data. So three 8-bit bytes of binary data can be represented by four 6-bit base64 characters.

- Base64 strings are typically padded with ‘=’ characters at the end if the binary data does not divide evenly into 24-bit groups.

- It is commonly used to send binary data over text-based mediums such as email, HTTP requests, XML documents, etc.

- It increases the size of the original data by 33% due to the use of 8-bit ASCII characters to represent 6-bit data.

- It is not encryption, just encoding. Base64 is usually part of a process to make binary data transmittable over textual mediums, not to keep it secure.

- Common uses include encoding binary attachments in emails, HTTP authorization headers, storing complex data in XML documents, and encoding JSON Web Tokens (JWT).

- There are several variants of base64 encoding such as URL-safe base64 which substitutes — and _ instead of + and /.

So in summary, base64 provides a mechanism to represent binary data in a text format for transmission over text-based systems. But it does not provide any security or encryption by itself.

Base64 application Scenario

Here are some common uses and applications where Base64 encoding is utilized:

  • Email attachments: Binary attachments like images, documents etc. can be Base64 encoded and included inline within the email body. This allows the transmission of binary data via SMTP.
  • HTTP authorization: HTTP Basic and Digest authentication schemes use Base64 to encode credentials in the Authorization header.
  • JSON Web Tokens (JWT): JWTs use Base64URL encoding (URL safe variant) to encode claims and other payload data within the token.
  • Storing complex data in XML: Binary data can be Base64 encoded to represent it in a XML document as text.
  • Data URIs: These embed small files like images inline within HTML/CSS using Base64 encoding.
  • Password hashes: Hashed passwords are often encoded in Base64 format for storage in databases.
  • Encoding binary data in URLs: Base64 can encode arbitrary binary data into web URLs.
  • Obfuscating text: Base64 can be used to encode text to make it unreadable.
  • Transferring files via IRC/instant messaging: Base64 can encode binary files for transmission via text-based chat protocols.
  • Cryptography: Used in certain cryptographic operations to encode binary ciphertexts.

Base64 Encode and Decode in Javascript

Here is how you can do Base64 encoding and decoding in JavaScript:

To encode data to Base64:

// Encode string 
let encoded = btoa("Hello World");

// Encode binary data
let raw = new Uint8Array([1,2,3]);
let encoded = btoa(String.fromCharCode.apply(null, raw));

The btoa() function encodes the given string or binary data to a Base64 string.

To decode Base64 data:

// Decode Base64 string
let decoded = atob("SGVsbG8gV29ybGQ=");

// Decode Base64 to binary data
let raw = atob("AQID");
let arr = new Uint8Array(raw.length);
for(let i = 0; i < raw.length; i++) {
arr[i] = raw.charCodeAt(i);
}

The atob() function decodes a Base64 string to the original string or binary data.

To handle binary data you need to convert it to/from strings before encoding/decoding.

There are also some libraries like base-64 that provide utility functions for Base64 encoding/decoding in JavaScript.

So in summary:

  • Use btoa() to encode strings or binary data to Base64
  • Use atob() to decode Base64 encoded strings
  • Convert binary data to/from strings before encoding/decoding

Free Online Web Tool for Base64 Encode and Decode

For easily encoding or decoding Base64 online, I recommend checking out the Base64 Encode and Decode tool on www.onlinecooltools.com

This handy web-based utility allows you to quickly encode text or files to Base64, and decode Base64 strings back to the original format.

The tool is completely free to use with no signup required. It works right in the browser so you don’t need to download any software.

For simple Base64 encoding and decoding tasks, this online tool provides a convenient way to get the job done fast. I highly recommend bookmarking it for your Base64 needs!

Check it out at: https://www.onlinecooltools.com/base64-encode-decode

So in summary, Tools4noobs offers an easy to use web-based tool for Base64 encoding and decoding which can come in handy for handling tasks related to Base64.

--

--