Online Base64 Calculator

By NodeCrypto ·

How to turn byte arrays and strings into Base64 strings with NodeCrypto.

A Base64 calculator workflow

Below is a workflow that converts the bytes 00112233445566778899AABBCCDDEEFF and the string "Hello NodeCrypto!" into Base64 strings. It uses the RFC 4648 Base64 format, and you can switch to another format in the node properties.

Press the "Open in app" button below to run it yourself!

Base64 workflow example

base64-calculator.ncw Open in app Download .ncw

How Base64 works, and its variants

In the workflow output, the 16 bytes 00112233445566778899AABBCCDDEEFF become ABEiM0RVZneImaq7zN3u/w==, and "Hello NodeCrypto!" becomes SGVsbG8gTm9kZUNyeXB0byE=. Let's look at why the output gets longer, why it ends with =, and why the same bytes can also be written as ABEiM0RVZneImaq7zN3u_w.

What is Base64?

Base64 is an encoding that represents any byte array using only 64 characters: upper- and lowercase letters, digits and two symbols. The 64 in the name is the number of values one character can stand for (2⁶ = 64), which means each character carries 6 bits. The format is specified in RFC 4648.

One point needs to be clear first: Base64 is a way of writing data, not encryption. There is no key, and anyone can recover the original bytes with the same rules. Converting a password or secret key to Base64 does not hide it. If the data needs protection, encrypt it first with a cipher such as AES-GCM.

Why is it needed?

Most cryptographic output is binary data that people cannot read. Ciphertexts, signatures, public keys and hash values contain arbitrary bytes such as 0x00 (null), 0x0A (line feed) and 0x22 (double quote), all of which have special meaning in text. Many places break if those bytes go in as they are.

Base64 turns any bytes into characters that are safe in all of these places. That is why it shows up in far more places than you might expect: JWT tokens, certificates and keys in PEM format (the body under -----BEGIN CERTIFICATE-----), email attachments, images embedded in HTML as data: URLs, HTTP Basic authentication headers and more.

Hexadecimal serves the same purpose. Hex writes 1 byte as 2 characters, doubling the size, while Base64 writes 3 bytes as 4 characters, growing it only by about 1.33 times. That is why hex is common when people compare bytes by eye, and Base64 is common when data is sent or stored.

How 3 bytes become 4 characters

Base64 processes the input 3 bytes (24 bits) at a time. It cuts the 24 bits into four 6-bit pieces, each a value from 0 to 63, and uses each value as an index to pick a character from a table of 64 characters (the alphabet). Here is how the string "Man" becomes TWFu.

The bytes 4D 61 6E of the string Man are laid out as 24 bits, cut into four 6-bit pieces giving indexes 19, 22, 5 and 46, which map to T, W, F and u in the Base64 alphabet

  1. The ASCII bytes of "Man" are 0x4D 0x61 0x6E.
  2. Written as bits, they are 01001101 01100001 01101110 (24 bits).
  3. Cut into 6-bit pieces they are 010011 010110 000101 101110, or 19, 22, 5 and 46 in decimal.
  4. In the alphabet, 19 is T, 22 is W, 5 is F and 46 is u.

In the alphabet, A to Z are 0 to 25, a to z are 26 to 51, 0 to 9 are 52 to 61, and the last two slots, 62 and 63, hold + and /. Those last two characters are the key to the "variants" described below.

Decoding runs the same steps in reverse: it turns characters back into indexes, joins the 6-bit pieces, and cuts them into 8-bit bytes again.

What padding (=) does

When the input length is not a multiple of 3, the last group has only 1 or 2 bytes left. Base64 then fills the missing bits with zeros to complete the last 6-bit piece, and appends = to make the output length a multiple of 4. This is called padding.

Base64 of Man, Ma and M compared. 3 bytes give TWFu with no padding, 2 bytes get 2 zero bits and become TWE=, and 1 byte gets 4 zero bits and becomes TQ==

The examples at the top of this post fit the rule. 16 bytes are five 3-byte groups plus 1 byte, so they end with two = as in ABEiM0RVZneImaq7zN3u/w==. "Hello NodeCrypto!" is 17 bytes, leaving 2 bytes, so it ends with one =.

Padding carries no information. The output length modulo 4 already tells how many bytes were left at the end, so the data can be decoded without padding. That is why padding is often dropped when the length is known another way, or where = causes trouble (in URLs, = separates query parameter names from values).

Standard Base64 formats

People usually think of "Base64" as a single format, but in practice there are several variants depending on where it is used. They differ in three ways: which characters are used for 62 and 63, whether padding is added, and whether line breaks are inserted.

Format Spec Chars 62, 63 Padding Line breaks Typical use
Standard Base64 RFC 4648 §4 + / Yes None JSON values, data URLs, default in most libraries
Base64URL RFC 4648 §5 - _ Optional None URLs, file names, cookies
Unpadded Base64URL RFC 7515 §2 - _ No None JWT, JWS, JWE, JWK
MIME Base64 RFC 2045 §6.8 + / Yes CRLF every 76 chars Email attachments
PEM RFC 7468 + / Yes Every 64 chars Certificate and private key files

Standard Base64 (RFC 4648 §4)

This is the basic format, using the alphabet described above (+, /) and padding (=). When someone says "Base64" without further detail, this is usually what they mean.

Base64URL (RFC 4648 §5)

The + and / of standard Base64 cause trouble in URLs: / separates path segments, and + may be read as a space in query strings. / is not allowed in file names either. So RFC 4648 defines a separate URL and Filename safe alphabet that replaces 62 with - and 63 with _. The other 62 characters are the same, so if a standard Base64 string contains no + or /, both formats give the same result. The 16 bytes in our example produce character 63 near the end, so they differ: .../w== versus ..._w==.

Unpadded Base64URL (JWT and friends)

RFC 7515, which defines JWS and is used by JWT and JWK, requires Base64URL without padding. That is why you never see = in the parts of a JWT token xxxxx.yyyyy.zzzzz. When you build or verify a JWT yourself, using standard Base64 or leaving the padding in will make signature verification fail.

MIME Base64 and PEM

The email standard MIME (RFC 2045) inserts a line break (CRLF) every 76 characters so lines do not get too long. The PEM format (RFC 7468) for certificates and keys puts standard Base64 wrapped every 64 characters between -----BEGIN ...----- and -----END ...-----. The alphabet and padding are the same as standard Base64 and only the line breaks differ, so once you strip the header, footer and line breaks, you can decode it as standard Base64.

See also: Base32, Base16, Multibase

Besides Base64, RFC 4648 also defines Base32 (5 bits per character, 32 characters) and Base16 (4 bits per character, i.e. hex). Where many encodings are mixed, as in decentralized identifiers (DIDs) or IPFS, Multibase adds a one-character prefix that tells which format follows. In Multibase, standard Base64 starts with m and unpadded Base64URL starts with u.

Choosing the Base64 format in NodeCrypto

In NodeCrypto, a single Bytes to Base64 node can produce all of the main formats above. Double-click the node (or select it and press F4) to open the properties window, which has two settings.

After changing the settings, press Apply & Close or Ctrl+Enter to apply them to the canvas, then run the workflow again to see the new result. Here are the 16 bytes from the example encoded with all four combinations.

The same 16 bytes encoded with the four combinations of the NodeCrypto Bytes to Base64 node's Base64 Variant (Standard, URL-Safe) and padding (on, off), with the typical use of each

Pick the settings by use case:

The Base64 to Bytes node, which goes the other way, has no settings. It accepts both +// and -/_, adds missing padding on its own, and ignores spaces and line breaks. So it decodes standard Base64, Base64URL, unpadded JWT segments and PEM bodies with line breaks as they are, without choosing a format. The PEM -----BEGIN ...----- header and footer are not Base64, though, so remove them first.

If you need Multibase, choose Base64 Standard (prefix: m) or Base64URL (prefix: u) under Multibase Format in the Bytes to Multibase node.

To try it yourself, open the workflow above in the app, set Base64 Variant to URL-Safe (-_) in the Bytes to Base64 node of the first group, uncheck padding and run it. The result changes to ABEiM0RVZneImaq7zN3u_w.