Photo by Dan Cristian Pădureț on Unsplash

How to use the base64 command on Linux

0

The base64 command converts binary data to ASCII data so that it can be transmitted as text. It’s a common method of encoding used to send images or PDF files or any kind of binary data as an “attachment” to an email message, so you’re probably using base 64 without even realising it. On Linux, you can use the base64 command in your terminal, should you ever have the need to manually encode binary data.

Using the base64 command to convert binary to ASCII

The base64 command reads from a file or from standard input (STDIN). You can see a simple example of base 64 encoding with a simple string:

$ echo "hello" | base64
aGVsbG8K

To confirm that aGVsbG8K is actually “hello” encoded, run base64 with the --decode option (-d for short):

$ echo "aGVsbG8K" | base64 --decode
hello

In practise, you’re more likely to use base64 to encode binary data. That’s data you can’t normally view in your terminal with commands like cat or less. For example, suppose you had an image of Tux, the emblem of the Linux kernel. If you try to view the data comprising the image in your terminal, you see garbage:

RIFFzWEBPVP8X
%ALPH �(I�i[}m��g۶m۶m۶m۶�k�������"b ]v`z����P�,��7ފ,��J��g�j�Q▒G6V3V��"5�:5f9*�2��.�����%�Y�m߹��v"b��������     ��7�������|���.0��ZXo����ɶ������L���lް�D)���)�U,.����!Ry�?�    {���ťn��Z=���U��1ײA�ӳ��� VP8 4p�* &>�:�G��"��      i�����i�@������H���p

However, convert it to base 64 and you can see the data as ASCII text. That doesn’t mean you see the rendered image “inside” the data, but you do see the data representing the image:

$ base64 tux.webp
UklGRnoCAABXRUJQVlA4WAoAAAAQAAAAHwAAJQAAQUxQSCACAAABkChJsmlbfW373mfbtm3btm3b
tm3bts1r29j9sPba6xMiYgIgXXZ/YHr4neGWUN0sjcI33oosg/h/Sr3UZ6Zq6lEYFUc2VjNW9P8i
NQuT1/lTm9LzIblHzYL1jdvzglet/Bovq5m6/+FwzjlyqOFPvlIzau/ttpU6r2Udf4ao6f8plxmA
Ogc1Zjkq6TIH/+cu9pOsr6Qlh1nnbd+5mZd2ImKfkvaBgeP6FJu2yjf54xStvAKzB532fLrxvS4w
nuQdBRN/Wlhv2OQOwPwHybaGiqWOhv5Mkv6uBmzesIpEKb6I5ikTuVUZLC6BF+mTyCFSeZ8/vwrZ
prx1jGG2MkuTWUPK5BMnkcMlrCL4DvKTeOIVf0h0SuF4Ay0YuIMsrHf7AmsbKM3MLWQfnZo8SV8D
Reh/nVyhs4H76GDAN3b6Z/KizhF+Hm9hwGbf5wvJPKuT02XhPQe5MsszSbK9qFBZYPvLHFIrnSNJ
zjIV+B2xBhy+h1SSGYHWCeE90NcOGN57VAkAqJKZ0lFiPGABoHUH4BB05zK9qN5OCF2/VsHBEjoW
H1LLimzaPzIRIPv4dgua6aBOTzOBxeEzP8xFwKy9rnrYMnZ0yZK1x33gDH+IS40ofchRYtkojcKh
zwQdzo2xQM3t+xc3cRSce03xM39zwOXoJgcIfdtuvry8eb7+wf0Le5rBxsWlbsbFWj2/D4KsVYez
MdeyQb/Ts6inzSAEVlA4IDQAAABwAwCdASogACYAPpE6mEeloyKhMAgAsBIJaQAAj6b/+LZp70AA
/bP/+KkO/0iDy/wScAAA

You can copy the command output from this article, and paste it into your own terminal as the input for base64 --decode to see the image. Your terminal doesn’t display graphics, so you must redirect the output into a file:

$ echo "UklGRnoCAABXRUJQVlA4WAoAAAAQAAAAHwAAJQAAQUxQSCACAAABkChJsmlbfW373mfbtm3btm3b
tm3bts1r29j9sPba6xMiYgIgXXZ/YHr4neGWUN0sjcI33oosg/h/Sr3UZ6Zq6lEYFUc2VjNW9P8i
NQuT1/lTm9LzIblHzYL1jdvzglet/Bovq5m6/+FwzjlyqOFPvlIzau/ttpU6r2Udf4ao6f8plxmA
Ogc1Zjkq6TIH/+cu9pOsr6Qlh1nnbd+5mZd2ImKfkvaBgeP6FJu2yjf54xStvAKzB532fLrxvS4w
nuQdBRN/Wlhv2OQOwPwHybaGiqWOhv5Mkv6uBmzesIpEKb6I5ikTuVUZLC6BF+mTyCFSeZ8/vwrZ
prx1jGG2MkuTWUPK5BMnkcMlrCL4DvKTeOIVf0h0SuF4Ay0YuIMsrHf7AmsbKM3MLWQfnZo8SV8D
Reh/nVyhs4H76GDAN3b6Z/KizhF+Hm9hwGbf5wvJPKuT02XhPQe5MsszSbK9qFBZYPvLHFIrnSNJ
zjIV+B2xBhy+h1SSGYHWCeE90NcOGN57VAkAqJKZ0lFiPGABoHUH4BB05zK9qN5OCF2/VsHBEjoW
H1LLimzaPzIRIPv4dgua6aBOTzOBxeEzP8xFwKy9rnrYMnZ0yZK1x33gDH+IS40ofchRYtkojcKh
zwQdzo2xQM3t+xc3cRSce03xM39zwOXoJgcIfdtuvry8eb7+wf0Le5rBxsWlbsbFWj2/D4KsVYez
MdeyQb/Ts6inzSAEVlA4IDQAAABwAwCdASogACYAPpE6mEeloyKhMAgAsBIJaQAAj6b/+LZp70AA
/bP/+KkO/0iDy/wScAAA" | base64 --decode > out.webp

Open the output file (tux.webp in this example) in an image viewer like Gwenview or Geeqie, or just double-click on the file and see what your desktop opens it in, and you see this image:

It’s a tiny low-quality image because I wanted it to fit easily into this article, but you can try base64 with high quality and large images, too. The base64 conversion process is lossless because it doesn’t compress data, it only converts it.

How base64 works

The base64 encoding method isn’t encryption, but a conversion process from binary (1s and 0s) data to a data set represented by a subset (64, to be precise) of ASCII characters. The ASCII character set available to base 64 is defined by RFC 4648, but it takes some parsing to align binary data to those 64 characters.

Because there are only 64 characters (the count starts at 0 in RFC 4648, so don’t be fooled by the peak of 63) in the base 64 specification, a binary sequence must be divided into 6-digit entities (there are 2 digits in binary, and 2⁶ is 64). For example, suppose you want to convert the byte 01000001 (that’s the number 65 in binary) to base64, but of course it contains 8, not 6, bits (because a “byte” is 8 bits). Splitting the byte into two groups of 6 digits for base 64 renders 010000 and 01, but you fill in 0s for the “missing” bits in the second half, so you end up with 010000 and 010000.

That’s 16 and 16. On the base 64 table, 16 maps to Q. During conversion, however, 0s were added to force 01 into 6 bits 010000, and that must be conveyed to base64 so it also adds padding while decoding. The base 64 specification provides the equals sign (=) to represent that padding was used during conversion.

The number 65 in base 64 is QQ==, which you can verify with the base64 command:

$ echo "QQ==" | base64 --decode
A

The letter A in ASCII is 65, a one-to-one conversion with the original binary number 01000001 that got encoded.

To encode 01000010 (that’s 66), you solve for 010000 and 100000 (16 and 32). In base 64, 16 maps to Q and 32 maps to g, so the base 64 representation of 66 is Qg==, which maps to the letter B in ASCII.

These are simple examples. When you’re dealing with binary blobs that are hundreds or thousands of bytes in size, the conversion is more complex.

Base 64 in real life

Realistically, most applications have the functionality of base64 built into them. When you receive an email containing an image, for example, you don’t have to copy and paste the base 64 representation of it and process it through your terminal. Your email client just displays the image.

However, some applications (such as many IMAP mail servers) expect base 64 encoding as input, so understanding how to convert a user name and password into base 64 encoding can be a useful tool while troubleshooting a mail server. The base64 command makes it trivial to encode and decode binary data to ASCII.

Leave a Reply