Why is base64 useful?
Making data travel safely through channels that only handle text.
Base64 is a way of turning data (images, files, raw bytes) into plain text so that it can travel safely through systems that only handle text, such as email, JSON or XML. It does this by representing every 3 bytes of data as 4 characters drawn from a set of 64 safe ones (A-Z, a-z, 0-9, + and /).
It is worth noting that encoding is not encryption and anyone can decode it instantly. Also, it can make the data up to a third larger than the original. Despite the size increase, the advantage of enabling binary data to survive a journey trough the network in text-only channels is worth it.
Let’s have a look at some examples. First, encoding and decoding a simple text string:
echo "Hello, World!" | base64# Output: SGVsbG8sIFdvcmxkCg==
echo "SGVsbG8sIFdvcmxkCg==" | base64 -d# output: Hello, WorldNow encoding and decoding an image:
base64 image.png > image.png.b64# No output
head image.png.b64# With `head` we check the beginning of the file:# iVBORw0KGgoAAAANSUhEUgAAAb8AAAG/CAMAAAD/zSlAAAAA/1BMVEX6+vonYoyCuEkzMzP///8w...
base64 -d image.png.b64 > image_copy.png# No output, though we can attest its existence:
ls image_copy.png# Output: image_copy.png