Bash base64 encode and decode

29/12/2020
To encode or decode standard input/output or any file content, Linux uses base64 encoding and decoding system. Data are encoded and decoded to make the data transmission and storing process easier. Encoding and decoding are not similar to encryption and decryption. Encoded data can be easily revealed by decoding. So, this command line utility tool can’t be used for data security. Alphabet, number and ‘=’ symbol are used to encode any data.

Syntax:

base64 [OPTION] [INFILE] [OUTFILE]

You can use different types of options with base64 command. Data can be taken from any file or standard input while encoding or decoding. After encode or decode, you can send the output in a file or print the output in the terminal.

Options:

-e or –encode

This option is used to encode any data from standard input or from any file. It is the default option.

-d or –decode

This option is used to decode any encoded data from standard input or from any file.

-n or –noerrcheck

By default, base64 checks error while decoding any data. You can use –n or –noerrcheck option to ignore checking at the time of decoding.

-u or –help

This option is used to get information about the usage of this command.

-i, –ignore-garbage

This option is used to ignore non-alphabet character while decoding.

–copyright

It is used to get copyright information.

–version

It is used to get the version information.

How you use the base64 command in Linux is shown in this tutorial by using some examples.

Example#1: Encoding text data

You can encode any text data by using base64 in the command line. When you want to encode any data using base64 then using -e or –encode option is optional. So, if you don’t mention any option with base64 then it will work for encoding. The following command will encode the data, ‘linuxhint.com’ and print the encoded data as output.

$ echo  ‘linuxhint.com’ | base64

Output:

Example#2: Decoding text data

The following command will decode the encoded text, ‘bGludXhoaW50LmNvbQ==‘ and print the original text as output.

$ echo ‘bGludXhoaW50LmNvbQo=’ | base64 –decode

Output:

Example#3: Encoding text file

Create a text file named, ‘sample.txt’ with the following text that will be encoded by using base64.

Sample.txt
PHP uses base64_encode and base64_decode for data encoding and decoding

You can print the encoded text in the command line or store the encoded text into another file. The following command will encode the content of the sample.txt file and print the encoded text in the terminal.

$ base64 sample.txt

Output:

The following commands will encode the content of the sample.txt file and save the encoded text into the encodedData.txt file.

$ base64 sample.txt > encodedData.txt
$ cat encodedData.txt

Output:

Example#4: Decoding text file

The following command will decode the content of the encodedData.txt file and print the output in the terminal

$ base64 -d encodedData.txt

Output:

The following commands will decode the content of the encodedData.txt file and store the decoded content into the file, originalData.txt.

$ base64 –decode encodedData.txt > originalData.txt
$ cat originalData.txt

Output:

Example#5: Encoding any user-defined text

Create a bash file named encode_user_data.sh with the following code. The following script will take any text data as input, encode the text by using base64 and print the encoded text as output.

#!/bin/bash
echo "Enter Some text to encode"
read text
etext=`echo -n $text | base64`
echo "Encoded text is : $etext"

Run the script.

$ base  encode_user_data.sh

Output:

Example#6: Checking user validity by decoding text

Create a bash file named checkValidity.sh and add the following code. In this example, a secret text is taken from the user.  A predefined encoded text is decoded by base64  and compared with the user input. If both values are equal then the output will be ‘You are authenticated’ otherwise the output will be ‘You are not authenticated’. Using this simple decoding code, normal validation can be done very easily.

#!/bin/bash
echo "Type your secret code"
read secret
otext=`echo ‘Nzc3Nzk5Cg==’ | base64 –decode`
if [ $secret == $otext ]; then
echo "You are authenticated"
else
echo "You are not authenticated"
fi

Run the script.

$ bash  checkValidity.sh

Output:

Conclusion:

For any sensitive data like password or any confidential data, encoding and decoding system is not suitable at all. You must use encryption and decryption system for securing these type of data.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Linux Screen Command Tutorial

In this lesson on Linux Screen Command, we will install and use various commands related to Screen command. The screen...
29/12/2020

Bash brace expansion

Bash uses brace expansion to generate a sequence of strings from the terminal or by using any bash script. A sequence of...
29/12/2020

How to Handle Command Line Arguments in a Bash Script

In many cases, bash scripts require argument values to provide input options to the script. You can handle command line...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Dịch vụ thuê mua proxy US UK uy tín, chất lượng số #1
13/05/2024

Thuê mua proxy Việt Nam: Báo giá & các thông tin MỚI NHẤT
13/05/2024

Dịch vụ thuê mua proxy giá rẻ an toàn, tốc độ cao
13/05/2024

Thuê mua proxy V6 uy tín, chất lượng tại đâu?
11/05/2024

Thuê mua proxy Tiktok tăng doanh thu, hiệu quả cao
11/05/2024