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.
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.
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.
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’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.
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.
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 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.
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.
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.