How to Decode Base64 Strings in Linux

Have you ever come across strange-looking jumbled text made up of letters, numbers and sometimes symbols like + and /? Chances are it was Base64 encoded.
Base64 encoding is commonly used to send binary data over media that only supports text - like displaying images in an HTML email.
When would you need to decode Base64 strings in Linux? Here are some common examples:
Reading encoded data - Configuration files, API keys and other sensitive data are often Base64 encoded to protect it. You may need to decode it to use in scripts or applications.
Images and documents - Base64 is used in data URIs for images, CSS, HTML, PDFs and more. Decoding it renders the files.
APIs and web apps - JSON web tokens (JWT), hashes and other data sent via APIs are Base64 encoded.
The good news is Linux has built-in tools to easily decode Base64 on the command line. Keep reading to learn multiple methods to handle your Base64 decoding needs.
Base64 Decoding Basics
First, a quick overview of Base64. It uses 64 printable characters to represent binary data. The 64 characters include upper and lowercase letters, numbers and two symbols.
Base64 takes 3 bytes of data and encodes it into 4 bytes - increasing the size by 33%. This allows binary data to be safely sent via textual communication channels.
For more details, see the Base64 Wikipedia page. Now let's look at how to decode in Linux.
Using the base64 Program
Most Linux distributions ship with the base64 program used to encode and decode Base64 data.
To decode a Base64 string with base64 use:
base64 --decode [encoded_string]
For example:
echo "SGVsbG8gV29ybGQ=" | base64 --decode
This decodes the Base64 text SGVsbG8gV29ybGQ= and prints out the result Hello World.
You can also decode Base64 data from a file:
base64 --decode file_to_decode.b64 > decoded_file
The base64 program makes it easy to handle simple encode/decode tasks. But for more robust Base64 manipulations, we need other tools.
Using OpenSSL
OpenSSL is an open-source toolkit for working with cryptography. Part of that involves encoding and decoding schemes like Base64.
To decode with OpenSSL:
openssl enc -base64 -d -in encoded.b64 -out decoded_file
Benefits:
Decodes any sized file instead of being limited to command line text
Options to auto-detect encoding formats
Supports PEM and other cryptography standards
This unlocks handling larger Base64 data including documents and images.
Leverage Base64 Linux Utilities
There are also handy Linux utilities focused specifically on Base64:
base64d - Base64 decode files or standard input
ybase64 - Encode/decode files, strings and streams
For example ybase64:
ybase64 -d encoded_string.txt decoded_data.bin
The utilities provide functionality similar to base64 and OpenSSL but with extra features and focus just on Base64.
Which Method to Use?
With multiple Base64 decoding options, which should you use?
base64 - best for quick command line encodings
OpenSSL - robust cryptography toolkits
base64d & ybase64 - specialty Base64 utilities
So if you just need to decode a short string, use base64. For larger files and data leverage OpenSSL or base64d. And ybase64 provides the most features for working with diverse Base64 data streams.
Conclusion
As you can see Linux offers great built-in Base64 decoding capabilities. The base64 program, OpenSSL toolkit and utilities like ybase64 provide complementary ways to handle all your Base64 manipulation needs directly on the command line.
Some key takeaways:
Base64 encodes binary data using 64 characters to represent data in a text format
Linux has base64, OpenSSL and other utilities for decoding
base64 works best for short command line strings
Use OpenSSL or specialty utilities for larger Base64 data
Now you have the knowledge to easily decode any Base64 encoded data you encounter in Linux. You can handle sensitive configured data, encode images for web sites, parse API payloads and unlock other Base64 formats.
Base64 no longer needs to look like a jumble of gibberish - your Linux toolbox can now make sense of it.






