1. Overview

Base64 encoding is a common method for representing binary data in ASCII string format. It’s widely used for encoding images and documents in email attachments, as well as for including data in URLs and in numerous applications that support only text.

In this tutorial, we’ll explore how to encode and decode Base64 strings using command-line tools in Linux.

2. Using base64

To begin with, let’s explore the basic usage of the base64 command, a de facto standard for Base64 handling.

2.1. Encode

In general, we can use the base64 command to encode a string:

$ echo -n 'Hello, World!' | base64
SGVsbG8sIFdvcmxkIQ==

In this case, we pipe the result to the base64 command which performs the encoding. Notably, we’ve used the -n flag with echo to prevent adding a trailing newline character to the string before performing the Base64 encoding. Alternatively, we can replace echo with printf to get the same output without extra switches.

If adding a newline character isn’t an issue, we can simply drop the -n flag:

$ echo 'Hello, World!' | base64
SGVsbG8sIFdvcmxkIQo=

We see that the result of the encoding is slightly different now that a newline character is added at the end.

Furthermore, we can achieve the same result using a here-string to provide the input:

$ base64 <<< 'Hello, World!'
SGVsbG8sIFdvcmxkIQo=

Notably, the here-string automatically adds a newline character at the end of the input.

2.2. Decode

To perform decoding, we use the -d flag with the base64 command:

$ base64 -d <<< SGVsbG8sIFdvcmxkIQo=
Hello, World!

Consequently, we obtain the original string from the encoded version.

3. Using openssl

Alternatively, we can use the openssl command which offers a wide range of functions, including the base64 subcommand for encoding and decoding strings:

$ openssl base64 <<< 'Hello, World!'
SGVsbG8sIFdvcmxkIQo=

Here, we see the same result as earlier.

For decoding, we append the -d flag:

$ openssl base64 -d <<< SGVsbG8sIFdvcmxkIQo=
Hello, World!

Notably, if the input string is long, the encoding ends up spanning multiple lines:

$ openssl base64 <<< 'If the input string is long, the base64 encoded string will span multiple lines'
SWYgdGhlIGlucHV0IHN0cmluZyBpcyBsb25nLCB0aGUgYmFzZTY0IGVuY29kZWQg
c3RyaW5nIHdpbGwgc3BhbiBtdWx0aXBsZSBsaW5lcwo=

To decode a block of Base64-encoded data, we can use a here-document:

$ openssl base64 -d << EOF
> SWYgdGhlIGlucHV0IHN0cmluZyBpcyBsb25nLCB0aGUgYmFzZTY0IGVuY29kZWQg
> c3RyaW5nIHdpbGwgc3BhbiBtdWx0aXBsZSBsaW5lcwo=
> EOF
If the input string is long, the base64 encoded string will span multiple lines

Thus, we’ve restored the output which has remained intact.

4. Using Python

Python also provides the base64 module for encoding and decoding data in the Base64 format.

To invoke the base64 Python module, we use the -m flag with the python interpreter:

$ python3 -m base64 <<< 'Hello, World!'
SGVsbG8sIFdvcmxkIQo=

Just like with the other commands, we use the -d flag to perform decoding instead of encoding:

$ python3 -m base64 -d <<< SGVsbG8sIFdvcmxkIQo=
Hello, World!

Basically, the -d flag runs the base64 module in decoding mode.

5. Using Perl

Finally, Perl has a core module, MIME::Base64, for encoding and decoding data in the Base64 format similar to Python.

Again, the module is invoked using the -M flag:

$ perl -MMIME::Base64 -ne 'printf "%s",encode_base64($_)' <<< "Hello, World!"
SGVsbG8sIFdvcmxkIQo=

The -n flag is for reading the input line by line, whereas the -e flag is for executing the provided command enclosed in quotes for each line.

Specifically, the command uses printf to print the result of the encode_base64() function, formatted as a string. The encode_base64() function takes in the current line as input, denoted by $_.

For decoding, we only need to replace the encode_base64() function with decode_base64():

$ perl -MMIME::Base64 -ne 'printf "%s",decode_base64($_)' <<< "SGVsbG8sIFdvcmxkIQo="
Hello, World!

As expected, we see that the original string was decoded correctly.

6. Conclusion

In this article, we explored several command-line tools for encoding and decoding Base64 data. In particular, we used the base64 command, as well as openssl, in addition to Perl and Python modules for performing the encoding and decoding.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.