Want to learn how to encode and decode strings using the base64 binary-to-text scheme?

Where Is base64 Used?

base64 is widely used in different domains.

The Bash shell on Ubuntu.

Hannah Stryker / How-To Geek

Some of the most common areas include email attachments, web development, networking, and URL encoding.

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.

The Linux terminal showing the process of encoding a string

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.

The Linux terminal displaying the process of encoding a string using base64 omitting the newline character

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.

The Linux terminal showing the process of encoding a string received from the printf command using base64

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.

The Linux terminal showcases how to encode a string using the base64 command with the help of here string operator

However, you’re free to 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.

The Linux terminal displaying the process of encoding a file to base64 using the base64 command

This command saved it to another file instead.

Let’s see a demonstration using the echo command.

you’re able to ignore those while decoding the string by using the “-i” option.

The Linux terminal showcasing the process of encoding the content of a file to base64 and saving it to another file

Python has a base64 module that you’re able to use for encoding and decoding strings.

you’re free to either use the python3 terminal command or write a full program.

I’ll show you both ways.

The Linux terminal displaying the process of decoding a base64 string using the base64 command with the help of the echo command

The python3 command has a “-m” or module flag.

you might use this flag to invoke the base64 module.

Then you might pass your string with the help of the echo command or here-strings.

The Linux terminal displaying the process of decoding a base64 string using the base64 command with the help of here-strings

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.

The Linux terminal displays the process of encoding a string to base64 using Python language

Now it’s possible for you 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.

The Linux terminal showing the process of decoding a base64 string using the Python language

The Linux terminal showcasing the process of encoding a string using a Python program

The Linux terminal displaying the process of decoding a base64 string using a Python program