Base32 Encoder & Decoder

RFC 4648 Base32 writes UTF-8 bytes five bits at a time. The alphabet is A through Z and the digits 2 through 7. The digits 0, 1, 8, and 9 are not symbols, which keeps O and I distinct from 0 and 1. Encoding emits uppercase and omits the = padding the RFC allows at the end. Decoding accepts either case, drops spaces, and treats a trailing = as optional. Those bytes are text only when they are valid UTF-8. A TOTP seed uses this same alphabet for raw key bytes, not for a sentence, so reading the seed as text is a different job and often fails.

What five bits of a byte become in RFC 4648 Base32

RFC 4648 Base32 takes the UTF-8 bytes of a text and writes them five bits at a time. Thirty-two symbols cover those five bits: A through Z, then 2 through 7. The digits 0, 1, 8, and 9 are not in the alphabet, so a handwritten O is not a zero and a handwritten I is not a one. Encoding writes uppercase. On the way back, A and a are the same symbol.

Eight symbols carry forty bits, which is five bytes. A shorter input ends on a partial group. The encoder still emits one last symbol from the remaining bits and does not append =. The RFC allows = so the length becomes a multiple of eight. MZXQ and MZXQ==== are both the two bytes of fo. Spaces are not data. A trailing = is padding, not another bit.

The same bytes have other spellings. Base64 groups six bits and uses a larger alphabet, including lowercase. Hex encode and decode writes four bits as one digit, two digits per byte. Base32 is the longer, case-insensitive spelling of those bytes. It is not Crockford Base32 and not base32hex. Those alphabets assign different values, and a 0 or a 1 in them is not a symbol here.

How the alphabet, padding, and the other Base32 names line up

Why the alphabet is A through Z and 2 through 7, with no 0 or 1

  1. Five bits have thirty-two patterns. Twenty-six letters and the six digits 2 through 7 fill that set. 0, 1, 8, and 9 are outside it.
  2. The missing 0 and 1 are the point of the alphabet. O and 0, and I and 1, are easy to mix when a person retypes a seed. 8 and 9 are simply unused.
  3. Encoding emits only uppercase. Decoding folds lowercase to uppercase and then accepts A through Z and 2 through 7. Any other character, including 0 or 8, is not a symbol.

Why the = padding can be missing and the letter case does not matter

  1. A full group is eight characters for five bytes. fo is two bytes, so the data symbols are MZXQ. The RFC pads that to MZXQ====. Both spellings are the same bytes.
  2. hello is five bytes, exactly one full group, so it is NBSWY3DP with no room for padding. A missing = on a shorter string is still the same data.
  3. Spaces and line breaks are separators. Trailing = signs are padding. An = in the middle is not padding, and a symbol outside the alphabet is not a shortened form of a valid string.

How Base32 of the same UTF-8 bytes differs from Base64 and from hex

  1. All three start from the UTF-8 bytes. Base32 takes five bits, Base64 takes six, and hex takes four. The groups do not land on the same character boundaries.
  2. hello is NBSWY3DP in this alphabet, aGVsbG8= in Base64, and 68656c6c6f in hex. None of those strings is a different message. They are three writings of 68 65 6C 6C 6F.
  3. Base32 is longer than Base64 and shorter than a binary listing of the same bits. It stays inside one case, which Base64 does not. The bytes themselves are unchanged.

Why an OTP seed in this alphabet usually does not come back as a sentence

  1. An otpauth secret is the raw HMAC key, written with this alphabet so a person can type it. The key is random bytes. It was not a sentence that someone encoded.
  2. Valid symbols only mean the bits can be rebuilt. Those bits are text when they are valid UTF-8. A key that is not UTF-8 is rejected as text even though every character was in the alphabet.
  3. A string that is valid UTF-8 is still not the account name or the password a person remembers. NBSWY3DP is hello only because those five letters were the input. A twenty-byte key is not five letters.

Where a Base32 string is bytes rather than the words around it

An authenticator setup string that a person has to retype

The secret field of an otpauth URI is RFC 4648 Base32 of the HMAC key, often without = padding. Uppercase and lowercase are the same symbols, which is why the alphabet can be read aloud. The surrounding account name is ordinary text and is not inside that Base32. Decoding the secret as a sentence asks the key bytes to be UTF-8 words, which they usually are not.

A short value whose padded and unpadded forms look different

fo encodes to MZXQ. The padded RFC form is MZXQ====. The four equals signs fill the group out to eight characters and add no bits. hello needs no padding because five bytes already fill eight symbols, NBSWY3DP. Comparing the two forms by string equality fails when one side kept the padding and the other dropped it.

The same short word written as Base32, Base64, and hex

hello is the five bytes 68 65 6C 6C 6F. Base32 spells them NBSWY3DP. Base64 spells them aGVsbG8=, which is the subject of Base64. Hex spells them 68656c6c6f, two digits per byte, which is the subject of hex encode and decode. A decoder for one alphabet will not accept the other spelling.

Common questions

What is Base32 encoding?
It writes the UTF-8 bytes of a text five bits at a time, using the thirty-two symbols A through Z and 2 through 7. Eight symbols stand for five bytes. The mapping is public. It does not hide the bytes, and it is not a cipher. RFC 4648 is the alphabet used here. Crockford Base32 and base32hex use other symbols for other five-bit values.
Why does Base32 skip 0, 1, 8, and 9?
Five bits need thirty-two symbols, not ten digits plus letters. The RFC uses the twenty-six letters and then 2 through 7. Leaving out 0 and 1 keeps them from being mistaken for O and I when a person copies a seed by hand. 8 and 9 are left out because the set is already full. A string that contains 0, 1, 8, or 9 is not this alphabet.
Is Base32 case sensitive?
No. Encoding writes uppercase, so hello becomes NBSWY3DP. Decoding folds a through z to A through Z before it reads the symbols, so nbswy3dp is the same five bytes. The digits 2 through 7 have no case. A different alphabet that treats A and a as different values is not RFC 4648.
Does Base32 need equals signs at the end?
The RFC uses = to pad the length to a multiple of eight. The data does not need those signs. fo is MZXQ without padding and MZXQ==== with it, and both are the two bytes of fo. hello is already eight symbols, NBSWY3DP, so there is nothing to pad. An equals sign that is not at the end is not this padding.
What is the difference between Base32 and Base64?
Both spell the same UTF-8 bytes. Base32 takes five bits and stays in one case, with the alphabet A through Z and 2 through 7. Base64 takes six bits, uses uppercase, lowercase, digits, and two extra symbols, and is shorter. hello is NBSWY3DP in Base32 and aGVsbG8= in Base64. A decoder for one will not read the other.
Why does a TOTP secret fail when decoded as Base32 text?
A TOTP or otpauth secret is the raw HMAC key written in this alphabet. The key is random bytes, not a sentence that was encoded. Rebuilding the bytes succeeds whenever the symbols are legal. Turning those bytes into text then requires valid UTF-8. Most keys are not UTF-8, so the text decode fails even though the Base32 itself is well formed. The account name was never inside that string.
What is the difference between RFC 4648 Base32 and Crockford Base32?
RFC 4648 uses A through Z and 2 through 7, and A is the value zero. Crockford uses digits and letters, omits I, L, O, and U, and often allows a checksum. In that alphabet the letter A is not the value zero. A string with 0 or 1 can be Crockford and is rejected here. Even a string that happens to use only shared letters does not carry the same bits.
Why is Base32 longer than the original text?
Each symbol holds five bits, and a byte holds eight, so five bytes become eight symbols. ASCII such as hello grows from five characters to NBSWY3DP. A character outside ASCII is several UTF-8 bytes before that expansion. The ideograph U+4E2D is the three bytes E4 B8 AD, which this alphabet writes as 4S4K2. The Base32 length follows the bytes, not the number of letters on screen.