Index table

Values 0 to 61 are the following, in order:

  • A – Z
  • a – z
  • 0 – 9

Then the last two values can change but typically + and /.

Padding character: =.

Usage

JavaScript

Use the builtin functions btoa and atob.

From Mozilla docs:

In JavaScript there are two functions respectively for decoding and encoding base64 strings:

  • btoa(): creates a base-64 encoded ASCII string from a “string” of binary data (“btoa” should be read as “binary to ASCII”).
  • atob(): decodes a base64 encoded string(“atob” should be read as “ASCII to binary”).

No imports are needed.

Encode

> btoa("Hello, world!")
"SGVsbG8sIHdvcmxk"

Decode

> atob("SGVsbG8sIHdvcmxk")
"Hello, world!"

Python

Guide for Python 3.

Use the builtin library.

Import

>>> import base64

Encode

>>> base64.b64encode("Hello, world!".encode())
b'SGVsbG8sIHdvcmxkIQ=='

Always convert to bytes first using str.encode. Otherwise you get an error (in both PY 2 and 3).

As bytes means any special characters like accents or emojis will be ASCII.

For ordinary ASCII characters, the bytes will look the same:

>>> "Hello, world!".encode()
b'Hello, world!'

Decode

Bytes input.

>>> base64.b64decode(b'SGVsbG8sIHdvcmxkIQ==')
b'Hello, world!'

Or (unicode) string input.

>>> base64.b64decode('SGVsbG8sIHdvcmxkIQ==')
b'Hello, world!'

Shell

Use base64 and -d or --decode to decode.

Encode:

$ echo  'Hello, world!' | base64
SGVsbG8sIHdvcmxkIQo=

Decode:

$ echo 'SGVsbG8sIHdvcmxkIQo=' | base64 -d
Hello, world!