Skip to main content

How to Decode Base64url Text in the Terminal

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

Base64url text, the kind you find in JSON Web Tokens, page cursors and links, can't go straight into a standard base64 decoder. Two characters have to be swapped back and the = padding put back first. This guide decodes base64url in bash, zsh and PowerShell, with a small function you can reuse.

Syntax​

In bash or zsh, define this function once, then call it with the text to decode:

Syntax
b64url_decode() {
local data
data=$(printf '%s' "$1" | tr '_-' '/+')
while [ $(( ${#data} % 4 )) -ne 0 ]; do data="$data="; done
printf '%s' "$data" | openssl base64 -d -A
echo
}
b64url_decode '<base64url-text>'
PartWhat it does
b64url_decode() { … }Defines a function, a named group of commands you can run again by its name.
local dataMakes data a variable that only this function sees.
tr '_-' '/+'Swaps the base64url characters back: every _ becomes /, and every - becomes +. $1 is the text you pass in.
while [ $(( ${#data} % 4 )) -ne 0 ]; do data="$data="; doneAdds = until the length is a multiple of 4. ${#data} is the length, and % 4 is the remainder after dividing by 4.
openssl base64 -d -ADecodes standard base64. -d means decode, and -A reads the input as one line.
echoEnds the output with a line break, so your next prompt starts on its own line.
'<base64url-text>'The text to decode, between single quotes.

In PowerShell 7.6:

Syntax
[Text.Encoding]::UTF8.GetString([System.Buffers.Text.Base64Url]::DecodeFromChars('<base64url-text>'))
PartWhat it does
[System.Buffers.Text.Base64Url]::DecodeFromChars('<base64url-text>').NET's base64url decoder. It accepts text without padding and returns the bytes.
[Text.Encoding]::UTF8.GetString(…)Turns the bytes back into text.

The Example​

Many APIs split long lists into pages and give you a cursor, a short piece of base64url text, to fetch the next page. A food app's search for pizza returned this cursor:

The cursor
eyJxIjoicGl6emE_IiwicGFnZSI6Mn0

It has a _, which standard base64 doesn't use, and it's 31 characters long, which isn't a multiple of 4. Both need fixing before a base64 decoder can read it.

Prerequisites​

  • macOS or Linux: OpenSSL. macOS includes it, and most Linux distributions do too; check with openssl version.
  • Windows: PowerShell 7.6, the current long-term support release. The Base64Url class arrived in .NET 9, which Windows PowerShell 5.1 doesn't run on.

Step 1: Swap the Characters Back and Restore the Padding​

Store the cursor in a variable, then paste the function into your terminal. It prints nothing yet: it only becomes available.

Terminal
cursor='eyJxIjoicGl6emE_IiwicGFnZSI6Mn0'
b64url_decode() {
local data
data=$(printf '%s' "$1" | tr '_-' '/+')
while [ $(( ${#data} % 4 )) -ne 0 ]; do data="$data="; done
printf '%s' "$data" | openssl base64 -d -A
echo
}

Step 2: Decode It​

Terminal
b64url_decode "$cursor"
Output
{"q":"pizza?","page":2}

A zsh terminal where the cursor eyJxIjoicGl6emE_IiwicGFnZSI6Mn0 is stored in a variable, the b64url_decode function is defined, and calling it prints JSON with the search pizza? and page 2.

The cursor is plain JSON: the app remembers the search, pizza?, and the page to fetch next, 2. Base64url only packs it into characters that are safe in a web address. It doesn't hide anything, so anyone can read it.

Don't skip the padding

Without the =, openssl base64 -d doesn't report a problem: it prints part of the text, or nothing, and still exits with status 0. Fixed: openssl base64 -d Cuts Off a JWT Payload or Prints Nothing shows it happening. PowerShell's [Convert]::FromBase64String refuses the text instead, with The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters. The fixes post for that error shows both causes.

Common Mistakes​

MistakeWhat happensDo this instead
Decoding without restoring the paddingOpenSSL returns part of the text, or nothingAdd = until the length is a multiple of 4
Swapping the characters the wrong way- and _ stay in, and the decoder can't read them_ becomes /, and - becomes +
Expecting base64url to hide dataAnyone can decode it, as you just didNever put secrets in base64url text

Troubleshooting​

SymptomLikely causeFix
The output stops partway, or is emptyThe = padding is missingUse the function above, which adds it
PowerShell: The input is not a valid Base-64 string…FromBase64String was given base64url, or text without paddingSwap the characters and pad first, or use Base64Url.DecodeFromChars
The output looks like random symbolsThe text holds binary data, not textSave it to a file instead of printing it

FAQ​

How many = does the text need?​

It depends on the remainder when you divide the length by 4. RFC 7515's Appendix C gives the rule: a remainder of 2 needs ==, a remainder of 3 needs =, and 0 needs nothing. A remainder of 1 means the text is broken.

Can I decode a JWT this way?​

Yes, one part at a time: a JWT is three base64url parts joined by dots. How to Decode a JWT in the Terminal splits a token and decodes its header and payload.

How do I encode text as base64url?​

How to Base64url Encode Text in the Terminal goes the other way.

Conclusion​

To decode base64url in bash or zsh, swap _ and - back to / and +, add = until the length divides by 4, and run openssl base64 -d -A. The b64url_decode function does all three. In PowerShell 7.6, [System.Buffers.Text.Base64Url]::DecodeFromChars handles the swap and the padding for you.

Additional Resources​