Want to learn how to encode and decode strings using the base64 binary-to-text scheme? This tutorial will show you two methods to encode and decode a string on Linux using the base64 command and the Python programming language.
Where Is base64 Used?
base64 is widely used in different domains. Some of the most common areas include email attachments, web development, networking, and URL encoding.
Some email systems use base64 to encode binary data such as images and documents into text format so that these can be safely transmitted with the message. Web developers also use base64 to embed images into HTML and CSS to reduce the number of HTTP requests and improve page load speed.

Another common use of base64 encoding is in authentication tokens. Usernames and passwords are sometimes masked using this encoding scheme and added to HTTP headers or URL parameters. In networking, base64 is used in protocols that use text-based communication, such as HTTP and SMTP, for transmitting data without corruption.
What you should know is that base64 is only an encoding scheme. The encoded data can be easily decoded to get the original data back. You should never use it if you need toencrypt data instead.

Encoding a String Using the base64 Command
The most basic way to encode a string using base64 is to output it to the terminal usingthe echo command. The trick is topipe the outputof the echo command to base64, like this:
Because of the default behavior of the echo command, there’s a trailing newline character at the end of the string. If you’d like to omit that and only use the string, run:

As you can notice, the output is different from the previous one. You can also do this using the printf command which doesn’t automatically append a newline character to the string. The format is given below:
This output is the same as the previous one because there are no newline characters this time. If you’re familiar withhere-stringson Linux, you can also use them to send your string output to the base64 command like this:

Similar to the echo command, here-strings also add a newline character at the end of the string.
Encoding Files Using the base64 Command
To encode a file using base64, you can pass the file directly as an option to the base64 command.
To test it out,create a new fileandappend some text to it. If you already have a text file, then use that. I’ve created a file called base.txt. To encode the file’s content to base64, run:
Remember to replace base.txt with your file name. The above command only displays the output in the terminal. It doesn’t save the encoded string anywhere. However, you can easily do that by redirecting the output to a new file. I’ve created another file called output.txt. This time I’ll save the output to that empty file. Here’s the command for that:
As you can see, the terminal didn’t display the output. This command saved it to another file instead.
Decoding a base64 String Using the base64 Command
For decoding a base64 string and turning it into a regular string, you’ll need to use the “-d” flag with the base64 command. Let’s see a demonstration using the echo command.
If you’d like to use here-strings for decoding a base64 string, then use:
Sometimes, there might be non-alphanumeric characters in a string. you’re able to ignore those while decoding the string by using the “-i” option.
Using Python to Encode and Decode a base64 String
If you’re a Python programmer or are more familiar with thePython programming languagethan Bash, then this method will be more suitable for you. Python has a base64 module that you can use for encoding and decoding strings. You can either use the python3 terminal command or write a full program. I’ll show you both ways.
The python3 command has a “-m” or module flag. you’re able to use this flag to invoke the base64 module. Then you can pass your string with the help of the echo command or here-strings. Here’s the full command:
To decode a base64 string, all you need to do is use the “-d” flag as seen previously with the base64 command. The syntax is below:
Of course, the convenient way is to create a Python program that can handle the encoding and decoding by taking user input. First, let’s create a program that will encode a string. Here’s the encoding code:
Save the file with a suitable name and a “.py” extension. I’m saving it by the name base64_encoder.py. Once done, run the program with:
You can also create a program to decode a base64 string. Here’s a code snippet you can use:
Save the file and run the program in the same way.
Now you’re able to use these Python programs to encode and decode any strings.
So these are two of the easiest ways to encode and decode strings using base64. If you’d like to learn more about the base64 command on Linux, it’s better to check outits manual page.