The Linux base64 command: encode and decode in base64

Introduction

In the world of system management and software development, it is often necessary to represent binary data in a text format that can be transmitted without corruption. The Base64 algorithm meets this function, transforming bytes into a secure ASCII character chain for email, URLs or configuration files. In Linux, the basic command line tool 64 allows this operation to be carried out quickly and easily.

What is Base64?

Base64 is a coding scheme that converts each group of three bytes (24 bits) into four ASCII characters selected from a 64-symbol alphabet (A-Z, a-z, 0-9, + and /). When the number of input bytes is not multiple of three, one or two pating characters (=) are added so that the output always has a multiple length of four. This feature makes encoded data easy to handle in environments that only accept flat text.

Basic syntax of the base64 command

The base64 command supports two main modes: encoding and decoding. By default, read from the standard input and write the result on the standard output, allowing it to be chained with pipes or redirections. The most used options are -d to decode and -i to ignore non-alphabetical characters during decoding. There is also -w COLS to adjust the line width of the encoded output.

Enable files and text

To encode the content of a file, simply indicate its name as an argument:

  • base64 archivo.txt > archivo.txt.b64
  • echo 'Text to encode' | base64

If the output is to have a specific line width, for example 76 characters (the one commonly used in post), the -w 76 option is added:

  • base64 -w 76 archivo.bin > archivo.bin.b64

In scripts, it is common to combine base64 with openssl or gpg to send data safely through channels that only accept text.

Decode Data

The decoding is as simple as using the -d flag. The command interprets the input as Base64 data and returns the original binary:

  • base64 -d archivo.txt.b64 > archivo.txt
  • echo 'VGhpcyBpcyBhIHRlc3Q =' | base64 -d

When the input can contain spaces, line jumps or invalid characters, the -i option makes it base 64 ignore them, avoiding format errors.

Practical examples

The following are several scenarios where the basic command is indispensable:

  • Sending files by email:encode an attached file and paste it into the message body as a flat text.
  • Imaging images in HTML or CSS:convert a PNG or JPEG image to Base64 and use syntax data: image / png; base64,... to avoid additional HTTP requests.
  • Storage of temporary credentials in environment variables:encode a key or token so that it does not appear legible in shell's history.
  • Data transfer between systems that do not support binary:use ssh or scp with pipes that encode and decode the flight.

Tips and tricks

  • Use -w 0 to deactivate the line jump and get a single continuous, useful line when the result is to be inserted into JSON or XML.
  • Combine base64 with md5sum or sha256sum to create coded data checksums without having to decode them previously.
  • Remember that Base64 is not a encryption method; if you need confidentiality, always apply encryption (e.g. with openssl enc) before encoding.
  • In embedded or resource-limited systems, the base64 version of GNU choreutils is very light and does not require additional dependencies.

Conclusion

The Linux base64 command is a versatile and essential tool for any administrator, developer or command line enthusiast. Its simple syntax, combined with the power of the pipes and redirections, allows to code and decode data quickly, safely and efficiently. Dominating its use opens the door to multiple applications, from the transmission of files by means that only accept text to the direct inclusion of resources on web pages. With the examples and tips presented, you are ready to incorporate Base64 into your daily workflow.

This work is under aCreative Commons License Attribution 4.0 International for Francesc Roig francesc @ vivaldi.net.

EnglishenEnglishEnglish