In this post we will learn how to encode and decode the strings with base64. We will also see some example on this as well.
Base64 is a group of similar encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation. The Base64 term originates from a specific MIME content transfer encoding.
Base64 encoding schemes are commonly used when there is a need to encode binary data that need to be stored and transferred over media that are designed to deal with textual data. This is to ensure that the data remain intact without modification during transport. Base64 is commonly used in a number of applications including email via MIME, and storing complex data in XML.
Encode and Decode strings with base64 in linux/unix system
First install the openssl in your linux system.
- In CentOS or Redhat
yum install openssl
- In Debian or Ubuntu
sudo apt-get update sudo apt-get install openssl
Note: In Ubuntu 16.04 LTS or above Operating system you can replace the command with
apt only. Now the command should be run in given below format.
sudo apt update sudo apt install openssl
For eg. We are using a string called Mypers0nalbl0g@firstname.lastname@example.orgM
Encode The String With base64
Use the given below command to encode the string with base64.
sharad@mylaptop:~$ echo Mypers0nalbl0g@email@example.comM|openssl base64 TXlwZXJzMG5hbGJsMGdAc2hAcmFkY2hoM3RyMS5jb00K sharad@mylaptop:~$
Decode The Same String With base64
Use the given below command to decode The same string with base64
sharad@mylaptop:~$ echo "TXlwZXJzMG5hbGJsMGdAc2hAcmFkY2hoM3RyMS5jb00K" |openssl base64 -d Mypers0nalbl0g@firstname.lastname@example.orgM sharad@mylaptop:~$
Use of base64
Base64 can be used for various purpose.
Disclaimer:The given below information source is taken from Wikipedia.
1. Base64 can be used to transmit and store text that might otherwise cause delimiter collision
2. Spammers use Base64 to evade basic anti-spamming tools, which often do not decode Base64 and therefore cannot detect keywords in encoded messages.
3. Base64 is used to encode character strings in LDIF files
4. Base64 is often used to embed binary data in an XML file, using a syntax similar to … e.g. favicons in 5. Firefox’s exported bookmarks.html.
5. Base64 is used to encode binary files such as images within scripts, to avoid depending on external files.
6. The data URI scheme can use Base64 to represent file contents. For instance, background images and fonts can be specified in a CSS stylesheet file as data: URIs, instead of being supplied in separate files.
7. The FreeSWAN ipsec implementation precedes Base64 strings with 0s, so they can be distinguished from text or hexadecimal strings.
8. Although not part of the official specification for SVG, some viewers can interpret Base64 when used for embedded elements, such as images inside SVG