How to encode and decode data using base32 and base64 commands in Linux

Base32 is the set of 32 bits numeral system and each bit can be represented by 5 bits. To represent base32 numbers in human-readable format is by using the standard 32-character set containing 22 uppercase alphabets and 0 to 9 digits. The resulting base32 character set can be used as files names as it cannot contain special characters in it.

Base64 is a group of binary to text encoding schemas that represent binary data in a sequence of 24-bits that can be represented by 6-bit Base64 digits. Common to all binary-to-text encoding schemes, Base64 is designed to carry data stored in binary formats across channels that only reliably support text content. Base64 is used to embed the image files, binary files in HTML and CSS files and sending email attachments.

Base32 encoding syntax:

base32 [OPTION]  [FILE]

BASE32 Encoding options:

How to encode standard input in Base32:

To encode the standard input or user input in base32 encoding format use below command. After entering the text in standard input press Ctrl + D to generate the base32 encoding.

$ base32

Test Line 1

Below is the base32 encoded value after pressing Ctrl + D in above command.

KRSXG5BANRUW4ZJAGEFA====

How to decode the base32 encoded value:

To decode the base32 encoded value use -d or –decode option. The encoded value has to be passed to the standard input to decode. After entering the encoded value press Ctrl+D and it will be decoded and the actual text will be displayed in the standard output.

$ base32 -d 
KRSXG5BANRUW4ZJAGEFA====

Below is the actual string which is decoded after pressing Ctrl + D in above command.

Test line 1

How to encode the command output in Base32:

Encoding command output is easy. Pass the command output to base32 command using the pipe in command line. Just use pipe separator after the command and use base32 as below.

$ cat reports.txt | base32

JZSXOIDEMF2GCCSUMVZXIIDMNFXGKIBRBJKGK43UEBWGS3TFEAZAUVDFON2CA3DJNZSSAMYKKRSX
G5BANRUW4ZJAGQFFIZLTOQQGY2LOMUQDKCSUMVZXIIDMNFXGKIBZBJKGK43UEBWGS3TFEAYTACSU
MVZXIIDMNFXGKIBRGEFE4ZLXEBSGC5DBEBUW4IDMNFXGKIBRGEFFIZLTOQQGY2LOMUQDCMQKKRSX
G5BANRUW4ZJAGEZQUVDFON2CA3DJNZSSAMJUEAFE4ZLXEBSGC5DBEBUW4IDMNFXGKIBRGQFA====

We can use echo command also to encode the string passing in echo command. This is useful to pass the string to base32 command instead of passing it through standard input.

$ echo "Test Line 1" | base32

4KAJYVDFON2CATDJNZSSAMPCQCOQU===

How to encode file content in Base32 encoding:

To encode the file contents by reading the file, pass the file name to base32 command as below.

$ base32 reports.txt

GU4WIN3EGA4TKOLEMFSDOOJZMVRWIMZXG5RTCZBVMMZTMZRZGMZGCMRUGJQTOYRSG42TGM3BMZRG
MNLEMU3DGNRTHEZWENJTG5SGMYRWMY3WENZQMM2TIMBTGJSTKZJWGBQTKMLDME2TEMDCHA2DQZLG
MI2DENJSHEYWEMBXMVSDGZJXMM3GGZLGMRRDGFEBGUZGEYJYGY4TIIBAORSXG5BOOR4HICRXHFVG
W2TEONUGMOJXMV4XEOJTPF4DS2DXMQ4XEN3OMRXXE2TOMNYXEYRYNYYGG4JAORSXG5C7GEXHI6DU
BI======

How to wrap encoded value in a single line:

By default base32 encoded value will be displayed in multiple lines. Base32 encoding data will be split in to next line after 76 characters. To print the encoding value in defined set of characters per line use -w or –wrap option. To print all the data in a single line instead of multiple columns or lines mention 0 in the number of lines column in the options.

Print 10 characters per line:

$ base32 -w 10 reports.txt
$ base32 --wrap 10 reports.txt
$ base32 -w 10 reports.txt

JZSXOIDEMF
2GCCSUMVZX
IIDMNFXGKI
BRBJKGK43U
EBWGS3TFEA
ZAUVDFON2C
A3DJNZSSAM
YKKRSXG5BA
NRUW4ZJAGQ
FFIZLTOQQG
Y2LOMUQDKC

Print 56 characters per line:

$ base32 -w 56 reports.txt
$ base32 --wrap 56 reports.txt

Print the encoded value in a single line:

$ base32 -w 0 reports.txt

JZSXOIDEMF2GCCSUMVZXIIDMNFXGKIBRBJKGK43UEBWGS3TFEAZAUVDFON2CA3DJNZSSAMYKKRSXG5BANRUW4ZJAGQFFIZLTOQQGY2LOMUQDKCSUMVZXIIDMNFXGKIBZBJKGK43UEBWGS3TFEAYTACSUMVZXIIDMNFXGKIBRGEFE4ZLXEBSGC5DBEBUW4IDMNFXGKIBRGEFFIZLTOQQGY2LOMUQDCMQKKRSXG5BANRUW4ZJAGEZQUVDFON2CA3DJNZSSAMJUEAFE4ZLXEBSGC5DBEBUW4IDMNFXGKIBRGQFA====

Ignore non-alphabetic characters while decoding:

Use -i or –ignore-garbage to ignore the non-alphabetic characters in the encoded value while decoding.

$ base32 -i reports.txt
$ base32 --ignore-garbage reports.txt

Please check this relevant article about encryption and decryption using BLAKE2 encryption mechanism using b2sum command in Linux.

BASE64 Encoding options:

Base64 encoding also having the same options as we have for Base32. Only the underlying encoding method will change in Base32 and Base64.

How to encode standard input in Base64:

$ base64

Test Line 1

Below is the base64 encoded value after pressing Ctrl + D in above command.

VGVzdCBsaW5lIDEK

How to decode the base64 encoded value:

$ base64 -d 

VGVzdCBsaW5lIDEK

Below is the decoded actual string value after pressing Ctrl + D in above command.

Test line 1

How to encode file content in Base64 encoding:

$ base64 reports.txt

TmV3IGRhdGEKVGVzdCBsaW5lIDEKVGVzdCBsaW5lIDIKVGVzdCBsaW5lIDMKVGVzdCBsaW5lIDQK
VGVzdCBsaW5lIDUKVGVzdCBsaW5lIDkKVGVzdCBsaW5lIDEwClRlc3QgbGluZSAxMQpOZXcgZGF0
YSBpbiBsaW5lIDExClRlc3QgbGluZSAxMgpUZXN0IGxpbmUgMTMKVGVzdCBsaW5lIDE0IApOZXcg
ZGF0YSBpbiBsaW5lIDE0Cg==

How to encode the command output in Base64:

$ cat reports.txt | base64
$ echo “Test Line 1” | base64
$ echo "Test line 1" | base64
VGVzdCBsaW5lIDEK

How to wrap encoded value in a single line:

Print 10 characters per line:

$ base64 -w 10 reports.txt
$ base64 --wrap 10 reports.txt
$ base64 --wrap 10 reports.txt

NTlkN2QwOT
U5ZGFkNzk5
ZWNkMzc3Yz
FkNWMzNmY5
MzJhMjQyYT
diMjc1MzNh
ZmJmNWRlNj
M2MzkzYjUz
N2RmYjZmN2

Print 56 characters per line:

$ base64 -w 56 reports.txt
$ base64 --wrap 56 reports.txt

Print the encoded value in a single line:

$ base64 -w 0 reports.txt

NTlkN2QwOTU5ZGFkNzk5ZWNkMzc3YzFkNWMzNmY5MzJhMjQyYTdiMjc1MzNhZmJmNWRlNjM2MzkzYjUzN2RmYjZmN2I3MGM1NDAzMmU1ZTYwYTUxY2E1MjBiODQ4ZWZiNDI1MjkxYjA3ZWQzZTdjNmNlZmRiM2Q1NTJiYTg2OTQgIHRlc3QudHh0Cjc5amtqZHNoZjk3ZXlyOTN5eDlod2Q5cjduZG9yam5jcXJiOG4wY3EgdGVzdF8xLnR4dAo=

Ignore non-alphabetic characters while decoding:

$ base64 -i reports.txt
$ base64 --ignore-garbage reports.txt

For more information about base32, base64 please visit base32, base64 pages.

Leave a Comment