{"id":6888,"date":"2024-08-08T05:53:47","date_gmt":"2024-08-08T09:53:47","guid":{"rendered":"https:\/\/www.both.org\/?p=6888"},"modified":"2024-08-08T05:53:47","modified_gmt":"2024-08-08T09:53:47","slug":"how-to-use-the-base64-command-on-linux","status":"publish","type":"post","link":"https:\/\/www.both.org\/?p=6888","title":{"rendered":"How to use the base64 command on Linux"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"6888\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">    <\/span>\r\n<\/div><\/div>\n<p>The <code>base64<\/code> command converts binary data to ASCII data so that it can be transmitted as text. It&#8217;s a common method of encoding used to send images or PDF files or any kind of binary data as an &#8220;attachment&#8221; to an email message, so you&#8217;re probably using base 64 without even realising it. On Linux, you can use the <code>base64<\/code> command in your terminal, should you ever have the need to manually encode binary data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using the base64 command to convert binary to ASCII<\/h2>\n\n\n\n<p>The <code>base64<\/code> command reads from a file or from standard input (STDIN). You can see a simple example of base 64 encoding with a simple string:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ echo \"hello\" | base64\naGVsbG8K<\/code><\/pre>\n\n\n\n<p>To confirm that <code>aGVsbG8K<\/code> is actually &#8220;hello&#8221; encoded, run <code>base64<\/code> with the <code>--decode<\/code> option (<code>-d<\/code> for short):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ echo \"aGVsbG8K\" | base64 --decode\nhello<\/code><\/pre>\n\n\n\n<p>In practise, you&#8217;re more likely to use <code>base64<\/code> to encode binary data. That&#8217;s data you can&#8217;t normally view in your terminal with commands like <code>cat<\/code> or <code>less<\/code>. 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:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>RIFFzWEBPVP8X\n%ALPH \ufffd(I\ufffdi&#91;}m\ufffd\ufffdg\u06f6m\u06f6m\u06f6m\u06f6\ufffdk\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\"b ]v`z\ufffd\ufffd\ufffd\ufffdP\ufffd,\ufffd\ufffd7\u078a,\ufffd\ufffdJ\ufffd\ufffdg\ufffdj\ufffdQ\u2592G6V3V\ufffd\ufffd\"5\ufffd:5f9*\ufffd2\ufffd\ufffd.\ufffd\ufffd\ufffd\ufffd\ufffd%\ufffdY\ufffdm\u07f9\ufffd\ufffdv\"b\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd     \ufffd\ufffd7\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd|\ufffd\ufffd\ufffd.0\ufffd\ufffdZXo\ufffd\ufffd\ufffd\ufffd\u0276\ufffd\ufffd\ufffd\ufffd\ufffd\ufffdL\ufffd\ufffd\ufffdl\u07b0\ufffdD)\ufffd\ufffd\ufffd)\ufffdU,.\ufffd\ufffd\ufffd\ufffd!Ry\ufffd?\ufffd    {\ufffd\ufffd\ufffd\u0165n\ufffd\ufffdZ=\ufffd\ufffd\ufffdU\ufffd\ufffd1\u05f2A\ufffd\u04f3\ufffd\ufffd\ufffd VP8 4p\ufffd* &amp;&gt;\ufffd:\ufffdG\ufffd\ufffd\"\ufffd\ufffd      i\ufffd\ufffd\ufffd\ufffd\ufffdi\ufffd@\ufffd\ufffd\ufffd\ufffd\ufffd\ufffdH\ufffd\ufffd\ufffdp<\/code><\/pre>\n\n\n\n<p>However, convert it to base 64 and you can see the data as ASCII text. That doesn&#8217;t mean you see the rendered image &#8220;inside&#8221; the data, but you do see the data representing the image:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ base64 tux.webp\nUklGRnoCAABXRUJQVlA4WAoAAAAQAAAAHwAAJQAAQUxQSCACAAABkChJsmlbfW373mfbtm3btm3b\ntm3bts1r29j9sPba6xMiYgIgXXZ\/YHr4neGWUN0sjcI33oosg\/h\/Sr3UZ6Zq6lEYFUc2VjNW9P8i\nNQuT1\/lTm9LzIblHzYL1jdvzglet\/Bovq5m6\/+FwzjlyqOFPvlIzau\/ttpU6r2Udf4ao6f8plxmA\nOgc1Zjkq6TIH\/+cu9pOsr6Qlh1nnbd+5mZd2ImKfkvaBgeP6FJu2yjf54xStvAKzB532fLrxvS4w\nnuQdBRN\/Wlhv2OQOwPwHybaGiqWOhv5Mkv6uBmzesIpEKb6I5ikTuVUZLC6BF+mTyCFSeZ8\/vwrZ\nprx1jGG2MkuTWUPK5BMnkcMlrCL4DvKTeOIVf0h0SuF4Ay0YuIMsrHf7AmsbKM3MLWQfnZo8SV8D\nReh\/nVyhs4H76GDAN3b6Z\/KizhF+Hm9hwGbf5wvJPKuT02XhPQe5MsszSbK9qFBZYPvLHFIrnSNJ\nzjIV+B2xBhy+h1SSGYHWCeE90NcOGN57VAkAqJKZ0lFiPGABoHUH4BB05zK9qN5OCF2\/VsHBEjoW\nH1LLimzaPzIRIPv4dgua6aBOTzOBxeEzP8xFwKy9rnrYMnZ0yZK1x33gDH+IS40ofchRYtkojcKh\nzwQdzo2xQM3t+xc3cRSce03xM39zwOXoJgcIfdtuvry8eb7+wf0Le5rBxsWlbsbFWj2\/D4KsVYez\nMdeyQb\/Ts6inzSAEVlA4IDQAAABwAwCdASogACYAPpE6mEeloyKhMAgAsBIJaQAAj6b\/+LZp70AA\n\/bP\/+KkO\/0iDy\/wScAAA<\/code><\/pre>\n\n\n\n<p>You can copy the command output from this article, and paste it into your own terminal as the input for <code>base64 --decode<\/code> to see the image. Your terminal doesn&#8217;t display graphics, so you must redirect the output into a file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ echo \"UklGRnoCAABXRUJQVlA4WAoAAAAQAAAAHwAAJQAAQUxQSCACAAABkChJsmlbfW373mfbtm3btm3b\ntm3bts1r29j9sPba6xMiYgIgXXZ\/YHr4neGWUN0sjcI33oosg\/h\/Sr3UZ6Zq6lEYFUc2VjNW9P8i\nNQuT1\/lTm9LzIblHzYL1jdvzglet\/Bovq5m6\/+FwzjlyqOFPvlIzau\/ttpU6r2Udf4ao6f8plxmA\nOgc1Zjkq6TIH\/+cu9pOsr6Qlh1nnbd+5mZd2ImKfkvaBgeP6FJu2yjf54xStvAKzB532fLrxvS4w\nnuQdBRN\/Wlhv2OQOwPwHybaGiqWOhv5Mkv6uBmzesIpEKb6I5ikTuVUZLC6BF+mTyCFSeZ8\/vwrZ\nprx1jGG2MkuTWUPK5BMnkcMlrCL4DvKTeOIVf0h0SuF4Ay0YuIMsrHf7AmsbKM3MLWQfnZo8SV8D\nReh\/nVyhs4H76GDAN3b6Z\/KizhF+Hm9hwGbf5wvJPKuT02XhPQe5MsszSbK9qFBZYPvLHFIrnSNJ\nzjIV+B2xBhy+h1SSGYHWCeE90NcOGN57VAkAqJKZ0lFiPGABoHUH4BB05zK9qN5OCF2\/VsHBEjoW\nH1LLimzaPzIRIPv4dgua6aBOTzOBxeEzP8xFwKy9rnrYMnZ0yZK1x33gDH+IS40ofchRYtkojcKh\nzwQdzo2xQM3t+xc3cRSce03xM39zwOXoJgcIfdtuvry8eb7+wf0Le5rBxsWlbsbFWj2\/D4KsVYez\nMdeyQb\/Ts6inzSAEVlA4IDQAAABwAwCdASogACYAPpE6mEeloyKhMAgAsBIJaQAAj6b\/+LZp70AA\n\/bP\/+KkO\/0iDy\/wScAAA\" | base64 --decode &gt; out.webp<\/code><\/pre>\n\n\n\n<p>Open the output file (<code>tux.webp<\/code> 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:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"32\" height=\"38\" src=\"https:\/\/www.both.org\/wp-content\/uploads\/2024\/08\/tux.webp\" alt=\"\" class=\"wp-image-6889\"\/><\/figure>\n\n\n\n<p>It&#8217;s a tiny low-quality image because I wanted it to fit easily into this article, but you can try <code>base64<\/code> with high quality and large images, too. The <code>base64<\/code> conversion process is lossless because it doesn&#8217;t compress data, it only converts it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How base64 works<\/h2>\n\n\n\n<p>The <code>base64<\/code> encoding method isn&#8217;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 <a href=\"https:\/\/datatracker.ietf.org\/doc\/html\/rfc4648#section-4\">defined by RFC 4648<\/a>, but it takes some parsing to align binary data to those 64 characters.<\/p>\n\n\n\n<p>Because there are only 64 characters (the count starts at 0 in RFC 4648, so don&#8217;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\u2076 is 64). For example, suppose you want to convert the byte 01000001 (that&#8217;s the number 65 in binary) to <code>base64<\/code>, but of course it contains 8, not 6, bits (because a &#8220;byte&#8221; 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 &#8220;missing&#8221; bits in the second half, so you end up with 010000 and 010000.<\/p>\n\n\n\n<p>That&#8217;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 <code>base64<\/code> so it also adds padding while decoding. The base 64 specification provides the equals sign (<code>=<\/code>) to represent that padding was used during conversion.<\/p>\n\n\n\n<p>The number 65 in base 64 is <code>QQ==<\/code>, which you can verify with the <code>base64<\/code> command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ echo \"QQ==\" | base64 --decode\nA<\/code><\/pre>\n\n\n\n<p>The <a href=\"https:\/\/www.ascii-code.com\/\">letter A in ASCII<\/a> is 65, a one-to-one conversion with the original binary number <code>01000001<\/code> that got encoded.<\/p>\n\n\n\n<p>To encode <code>01000010<\/code> (that&#8217;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 <code>Qg==<\/code>, which maps to the letter B in ASCII.<\/p>\n\n\n\n<p>These are simple examples. When you&#8217;re dealing with binary blobs that are hundreds or thousands of bytes in size, the conversion is more complex.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Base 64 in real life<\/h2>\n\n\n\n<p>Realistically, most applications have the functionality of <code>base64<\/code> built into them. When you receive an email containing an image, for example, you don&#8217;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.<\/p>\n\n\n\n<p>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 <code>base64<\/code> command makes it trivial to encode and decode binary data to ASCII.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The base64 command converts binary data to ASCII data so that it can be transmitted as text. It&rsquo;s<\/p>\n","protected":false},"author":31,"featured_media":6893,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[149,100,90],"tags":[521,151,104,91],"class_list":["post-6888","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bash","category-command-line","category-in-depth","tag-base64","tag-bash","tag-command-line","tag-linux"],"modified_by":"David Both","_links":{"self":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/6888","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/users\/31"}],"replies":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6888"}],"version-history":[{"count":2,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/6888\/revisions"}],"predecessor-version":[{"id":6897,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/6888\/revisions\/6897"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/media\/6893"}],"wp:attachment":[{"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6888"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}