How to Encode and Decode Base64 on Mac (Terminal and Apps)
Decode and encode Base64 on Mac with the base64 command, fix URL-safe and unpadded strings, preview data URIs, and decode the clipboard with one shortcut.
GuideUpdated 9 min read
On this page
To decode Base64 on a Mac, open Terminal and run pbpaste | base64 -D. This decodes whatever is on your clipboard. To decode a file, run base64 -D -i encoded.txt -o output.bin. To encode text, pipe it in: printf '%s' 'hello world' | base64.
The built-in command handles standard Base64. It expects raw input in the standard alphabet, so URL-safe strings (from JWTs and URLs) and strings with missing = padding need fixing first. The fixes are below, along with a way to decode the clipboard with one shortcut.
Use the base64 command in Terminal
macOS includes a base64 command, so you don't need to install anything. To open Terminal, go to Applications ▸ Utilities, or press ⌘Space (Command-Space) and type "Terminal".
Encode a piece of text:
printf '%s' 'hello world' | base64This prints aGVsbG8gd29ybGQ=. Use printf '%s' instead of plain echo. echo adds a newline, and the newline gets encoded too: echo 'hello world' | base64 gives aGVsbG8gd29ybGQK, which decodes to the text plus a line break.
Decode it again:
printf '%s' 'aGVsbG8gd29ybGQ=' | base64 -DThe decoded text has no trailing newline, so it may appear right before your next prompt, with zsh marking the spot with a %. The output is still correct.
The flags you'll use:
| Flag | What it does |
|---|---|
-D (also -d, --decode) | Decode instead of encode |
-i file | Read from a file instead of standard input |
-o file | Write to a file instead of the screen |
-b count | Insert a line break every count characters (default: one unbroken line) |
How the Mac command differs from Linux
Commands copied from Linux tutorials often fail on a Mac, or behave differently:
- Line wrapping. GNU
base64on Linux wraps output at 76 characters by default, and-w 0turns wrapping off. The macOS command doesn't wrap by default and has no-woption. If you need 76-character lines, for example for MIME, use-b 76. -imeans something else. On Linux,-imeans "ignore garbage characters". On a Mac,-inames the input file.- Decoding. Linux tutorials use
-d. On a Mac,-Dis the safest spelling in scripts, because option names have changed between macOS versions.
Decode Base64 text and files on Mac
For text, pipe it in and read the result on screen, as shown above. To decode what you just copied and put the result back on the clipboard:
pbpaste | base64 -D | pbcopyFor files, use -i and -o. The first line below encodes a file into a text file. The second copies the encoded result to the clipboard instead:
base64 -i photo.png -o photo.txt
base64 -i photo.png | pbcopyTo decode Base64 back into a file:
Warning: -o overwrites the output file without asking if it already exists.
base64 -D -i photo.txt -o photo.pngWrite binary results (images, PDFs, compressed data) to a file with -o. If you decode them to the screen, Terminal shows unreadable characters, and copying that output won't give you the file back.
Fix URL-safe Base64 and missing padding
Standard Base64 uses + and /, and pads the end with = so the length is a multiple of 4. URL-safe Base64 (Base64URL) uses - and _ instead, and usually drops the padding. JWTs, OAuth tokens and many web APIs use it. The macOS base64 command expects the standard alphabet, so it may reject a URL-safe string or decode it wrongly.
Convert the string first, then pad it:
s='eyJzdWIiOiIxMjM0In0'
s=$(printf '%s' "$s" | tr '_-' '/+')
while [ $(( ${#s} % 4 )) -ne 0 ]; do s="$s="; done
printf '%s' "$s" | base64 -DLine 2 changes _ to / and - to +. Line 3 adds = until the length is a multiple of 4. The example prints {"sub":"1234"}.
To make URL-safe output without padding:
printf '%s' 'hello world' | base64 | tr '/+' '_-' | tr -d '='A full JWT has three parts separated by dots. Decode each part on its own, or see our guide to decoding a JWT on Mac.
The command accepts raw Base64 only, so strip anything around it first. For a data: URI, remove everything up to and including the comma:
Warning: this overwrites image.png if it exists.
pbpaste | sed 's/^data:[^,]*,//' | base64 -D -o image.pngFor PEM blocks, delete the -----BEGIN …----- and -----END …----- lines before decoding.
Encode and decode in Bytepliers
If you decode Base64 several times a day, the Terminal steps take time. Bytepliers is our Mac app of offline developer tools. Its Base64 tool skips the cleanup steps:
- Open Bytepliers and press ⌘2 (Command-2), or click Base64 in the sidebar under ENCODE.
- Choose Encode or Decode. Encode is the default.
- Paste or drop your input into the input pane. The result updates as you type.
- Press ⇧⌘C (Shift-Command-C) to copy the output.
What differs from the Terminal command:
- Decoding accepts both alphabets, whitespace and missing padding. You can paste a URL-safe or unpadded string as it is.
- URL-safe is an Encode option, off by default. It produces output with
-and_and no=padding. - Swap feeds the output back in as input, so you can check a round trip.
- The status line shows the output size.
- Live conversion stops at 2 MB of input. For anything larger, use the Terminal command.
The tool runs on your Mac and makes no network requests.


Preview Base64 images and data URIs
If the Base64 holds an image, decoded text won't help you. The Base64 Image tool (also in the ENCODE group) shows the image itself:
- Open Base64 Image from the sidebar or the Tools menu.
- Paste a
data:URI, a CSSurl(…)value or raw Base64 into Base64 / Data URI. You don't need to strip the prefix. - Check the Preview, which shows the image over a checkerboard with its format and size.
- Click Save… (⌘S) to write the image to a file. To reuse it as text, click Copy Data URI or Copy CSS url().
The tool detects PNG, JPEG, GIF, WebP, BMP, ICO, TIFF, HEIC/AVIF, PDF and SVG, including percent-encoded SVG.
It also works in reverse. Drop an image onto the tool, or click Encode Image… (⌘O), to get a data URI. Image files can be up to 10 MB.
Decode the clipboard with one keystroke
Bytepliers has two global shortcuts for this. Both work from any app.
Detect & Replace Clipboard (⌥⇧⌘B)
Copy a Base64 string and press ⌥⇧⌘B (Option-Shift-Command-B). Bytepliers works out what's on the clipboard, applies the most useful transform and replaces the clipboard with the result. For Base64, it decodes to text. A HUD confirms the change, and then you can paste.
- You can also Option-click the Bytepliers menu bar icon, or choose Tools ▸ Detect & Replace Clipboard.
- To undo, choose Tools ▸ Restore Previous Clipboard. The same item is in the menu bar menu.
- If the clipboard holds a JWT, you get the payload JSON instead.
- If nothing matches with enough confidence, it beeps and shows "No confident match — try the Quick Panel (⌥⌘B)".
Quick Panel (⌥⌘B)
The Quick Panel shows you the result before anything is copied:
- Copy the Base64 string.
- Press ⌥⌘B (Option-Command-B). The panel detects Base64 and shows the decoded text.
- If it picked a different kind, type
b64dto switch to Base64 Decode, orb64eto encode. - Press ⏎ to copy the result and close the panel, or ⌘O to open it in the main window.
You can change both shortcuts in Settings ▸ Shortcuts. Both skip clipboards larger than 1,000,000 bytes. The Quick Panel displays up to 20 KB or 400 lines of the result, but ⏎ copies all of it. The Quick Panel docs also cover the menu bar icon and the Compact Overlay.
To work on selected text instead of the clipboard, select it and choose app menu ▸ Services ▸ Transform with Bytepliers. The selection is replaced in place, and your clipboard isn't touched. The service has no shortcut by default. The Keyboard Shortcuts… button in Bytepliers' Settings ▸ Shortcuts opens System Settings ▸ Keyboard, where you can assign one.
Unpack gzip + Base64 payloads
APIs, logs and message queues often gzip data and then Base64-encode it. Decoding only the Base64 leaves you with binary output, so you also need to decompress it.
In Terminal, chain the two steps:
pbpaste | base64 -D | gzip -dcgzip -dc decompresses standard input and prints the result. This works only for gzip data. gzip can't read zlib or raw DEFLATE data.
In Bytepliers, open the GZIP tool (ENCODE group):
- Choose Decompress.
- Paste the Base64 or hex into the input. The tool reads gzip, zlib or raw DEFLATE, and checks the gzip CRC-32 and zlib Adler-32 checksums.
- Copy the text from the output.
Decompression is capped at 64 MB. Compress does the reverse and produces gzip, zlib or raw DEFLATE as Base64 or hex.
If you do this often, save it as a recipe. In Pipelines, add a Base64 → Gunzip step, then add Format JSON if the payload is JSON, and save. You can then run the recipe on the clipboard from the menu bar, ⌘K or the Quick Panel. The app ships with an example recipe called "Gunzip API payload". To build your own, see Pipelines and recipes.
Which method to use
| Method | Best for | URL-safe or unpadded input | Images | gzip |
|---|---|---|---|---|
base64 in Terminal | Scripts, files, large data | Needs tr and padding first | Decode to a file with -o | Pipe to gzip -dc (gzip only) |
| Bytepliers Base64 tool | Pasting, checking, round trips | Accepted as is | Use Base64 Image | Use GZIP tool |
| Base64 Image tool | Data URIs, inline images | Raw Base64 or data URI | Preview and save | No |
| Quick Panel / Detect & Replace | The clipboard, from any app | Accepted as is | No | Decompresses data it recognizes as gzip |
| GZIP tool | Compressed API payloads | Base64 or hex input | No | gzip, zlib, raw DEFLATE |
If you decode Base64 only now and then, the built-in command is enough. Bytepliers is more useful if you handle tokens and payloads all day. It's part of MacSuite, and you can try it free for 7 days. The Bytepliers tool reference lists every tool.
If it doesn't work
- The output looks like random symbols. The data is binary, such as an image, compressed data or a certificate. Save it to a file with
-o, or open it in the Base64 Image or GZIP tool. - Decoding fails or the end of the text is wrong. The string is probably URL-safe (it contains
-or_) or is missing its=padding. Use thetrand padding steps above, or paste it into Bytepliers, which accepts both. - The decoder rejects input that looks valid. The string contains something besides Base64, such as a
data:image/png;base64,prefix, PEM header lines or quotes copied with the value. Strip it first. - Your encoded string doesn't match someone else's. You probably used
echo, which adds a trailing newline that gets encoded too. Useprintf '%s'instead. - A Linux command fails with an unknown option. The macOS command has no
-w. Leave it out, because macOS doesn't wrap by default, or use-bto set a line length. - Bytepliers says "Input is … — the limit for live conversion is 2 MB." The Base64 tool and pipelines stop at 2 MB. Use the Terminal command for larger input.
FAQ
Is Base64 encryption?
No. Base64 is an encoding, and anyone can reverse it without a key. Don't use it to hide passwords or tokens.
Why does my Base64 end with = or ==?
That's padding, which brings the length up to a multiple of 4 characters. URL-safe Base64 usually leaves it off, so you may need to add it back before the macOS command will decode the string.
Does Bytepliers send my data anywhere?
No. The Base64, Base64 Image and GZIP tools run entirely on your Mac. The app connects to the network only for license checks, update checks and actions you start yourself, such as sending a request from the JSON Formatter.