Technotes

Technotes for future me

Base64 encode/decode

Encode text string

echo  'blaataap' | base64
YmxhYXRhYXAK

Decode text string

echo  'YmxhYXRhYXAK' | base64 --decode
blaataap

Encoding text file

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
YmxhYXRhYXAK

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
YmxhYXRhYXAK

Disable line wrapping, usable for encoding certificates

base64 sample.txt -w0 > encodedData.txt
cat encodedData.txt
YmxhYXRhYXAK

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
blaataap

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
blaataap

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"
./encode.sh
Enter Some text to encode
blaataap
Encoded text is : YmxhYXRhYXA=
Last updated on 31 Jan 2021
Published on 5 Nov 2020
Edit on GitHub