How to Encode and Decode a base64 String From the Command Line?

Encode and decode are the processes to encrypt and decrypt the data. In Linux, the encoding and decoding processes of Binary to ASCII (and ASCII to Binary) are performed through the base64 command. It converts the binary data into base64 string comprised of ASCII characters and also decodes it into its original format. 

This article will briefly demonstrate how to encode and decode base64 string from the command line: 

How to Encode a String in base64?

To encode a base64 string, the following syntax is used: 

Syntax:

$ echo <string> | base64
  • Type the string in the echo command.
  • Add base64 command through pipe (|).

Example 1: Encode a base64 String

To encode any string into base64 format is obtained as follows:

$ echo "Welcome to itslinuxfoss" | base64

The above command has encoded the given text.

You can also store the encoded text in the file using the redirection operator as follows:

$ echo "Welcome to itslinuxfoss" | base64 > encode.txt

Let’s check the encoded text in the file (encoede.txt):

$ cat encode.txt

The encoded text has been stored in the file.

How to Decode a base64 String?

To decode the base64 string in Linux, the following command syntax is examined:

Syntax:

$ echo <base64 string> | base64 -d
  • Type the string in the echo command.
  • Add base64 command with “–decode” flag through pipe (|).

To decode the base6464 string, put it in the echo statement 

$ echo "4oCcV2VsY29tZSB0byBpdHNsaW51eGZvc3PigJ0K" | base64 --decode

The encoded data has been converted into its original format.

Decode base64 Through .bashrc

You can also make a script function of decode and paste it into the “.bashrc” file and just call the function name to decode the encoded data. Copy and paste the following decode function into your “.bashrc” file. Go through the following steps to implement this method:

Step 1: Define the Decode Function 

Open the .basrch file, copy and paste the following function into the file:

decode () {
  echo "$1" | base64 -d ; echo
}

Save the file and exit.

Step 2: Execute the .bashrc 

Examine the source command to execute the file to apply the new changes:

$ source .bashrc

The file has been executed.

Verify the Result

Now, simply type the “decode” (function name) and give the encoded string:

$ decode V2VsY29tZSB0byBpdHNsaW51eGZvc3MK

The data has been decoded.

How to Encode Text Files in base64?

You can also encode any text file in Linux using the base64 command, Consider the following file output for the below implementation:

$ cat file.txt

To encode the text files in Linux, simply type the base64 command along with the file name and redirection operator to save the encoded file output:

$ base64 file.txt > encoded.txt

The file “file.txt” has been encoded to the “encoded.txt” files.

Let’s check the output of the “encoded.txt” file:

$ cat encoded.txt

The file “encoded.txt” has the encoded data.

How to Decode a base64 Text File?

To decode the base64 text file, use the “d” flag in the base64 command and give the file name to decode it:

$ base64 -d encoded.txt

The file “encoded.txt” has been decoded to its original format.

How to Encode the User Input in Base64?

Another way to encode the data is from the user input, you can use the bash script for that purpose. Use the following script in the bash script file and run it, it will ask you for the input to encode that text:

#!/bin/bash
# Message to Enter the Data
echo "Enter data to Encode: "
# Storing the input in the variable
read UserData
# Encoding and Saving it
Encod_Data=`echo -n $UserData | base64`
# Giving encoded output
echo "Encoded text is : $Encod_Data"

The script is described as:

  • “echo” statement for printing the message to the user, and storing the input in “UserData” variable using the “read” property.
  • Encoding the user data into base64 string.
  • Displaying the encoded string to the user.

Save the file and make it executable using the chmod command:

$ chmod +x script.sh

The script file is executable now.

Now, run the script file in the terminal and you will see the message to enter the data to encode it. Once you enter your data, hit the enter button:

$ ./script.sh

The data has been encoded.

How to Decode the User Base64 Input?

To decode data from the user input, use the following script in the bash script file, the script will receive and automatically decode it:

#!/bin/bash
# Message to Enter the Encoding String
echo "Enter the Encoding String"
read Received_Encoding
# DEcoding and Saving it
Decod_Data=`echo -n $Received_Encoding | base64 --decode`
# Giving Decoded output
echo "Decoded text is : $Decod_Data"

The script is described as

  • “echo” statement for printing the message to the user, and storing the input in “Received_Encoding” variable using the “read” property.
  • Decoding the user data into base64 string
  • Displaying the decoded string to the user.

Save the file and make it executable as we did in the above section:

$ chmod +x script2.sh

The file script file is executable now.

Now, run the script file in the terminal, it will ask you to enter the bae64 encoded data, paste it, and hit the enter button:

$ ./script2.sh

The encoded string has been decoded. 

Conclusion

To encode or decode the data into base64 string, examine the syntax “echo <string> | base64” for encoding or “echo <string> | base64 –decode” for decoding. Apart from this, the file method or bash method for encoding or decoding can also be considered. 

This write-up has illustrated the encoding and decoding process through the base64 command.