View Categories

Linux Command Line Base64

1 min read

Base64 encoding is a common method for representing binary data in ASCII string format. We leverage this in our JSON configurations for passphrases but base64 encoding is used everywhere.

Encoding

We can use the base64 command to encode a string from your linux terminal:

JavaScript
echo -n 'This is a test!' | base64                                                                                                                                                                                                                                     
VGhpcyBpcyBhIHRlc3Qh

encoding This is a test! the base64 output is: VGhpcyBpcyBhIHRlc3Qh

In this example, we pipe the result to the base64 command, which performs the encoding. Notice, we used the -n flag with echo to prevent adding a trailing newline character to the string before performing the Base64 encoding. Alternatively, we can replace echo with printf to get the same output without extra switches.

JavaScript
printf 'This is a test!' | base64                                                                                                                                                                                                                                      
VGhpcyBpcyBhIHRlc3Qh

You can see its the same output

Decoding

We can still use the base64 command to decode a string from your linux terminal but we just use the -d:

JavaScript
base64 -d <<< VGhpcyBpcyBhIHRlc3Qh                                                                                                                                                                                                                                     
This is a test!

Here we obtain the original string from the encoded version.

Alternative is use openssl

You can use the openssl command, which offers a wide range of functions, including the base64 subcommand for encoding and decoding strings

Encode using openssl

JavaScript
openssl base64 <<< 'This is a test!'                                                                                                                                                                                                                                   
VGhpcyBpcyBhIHRlc3QhCg==

Decode using openssl

JavaScript
openssl base64 -d <<< VGhpcyBpcyBhIHRlc3QhCg==                                                                                                                                                                                                                         
This is a test!

What’s the difference?

I have had more luck and more accuracy using base64 with openssl

Protected By
Shield Security