Why do we use Base64?

Viewed 135877

Wikipedia says

Base64 encoding schemes are commonly used when there is a need to encode binary data that needs be stored and transferred over media that are designed to deal with textual data. This is to ensure that the data remains intact without modification during transport.

But is it not that data is always stored/transmitted in binary because the memory that our machines have store binary and it just depends how you interpret it? So, whether you encode the bit pattern 010011010110000101101110 as Man in ASCII or as TWFu in Base64, you are eventually going to store the same bit pattern.

If the ultimate encoding is in terms of zeros and ones and every machine and media can deal with them, how does it matter if the data is represented as ASCII or Base64?

What does it mean "media that are designed to deal with textual data"? They can deal with binary => they can deal with anything.


Thanks everyone, I think I understand now.

When we send over data, we cannot be sure that the data would be interpreted in the same format as we intended it to be. So, we send over data coded in some format (like Base64) that both parties understand. That way even if sender and receiver interpret same things differently, but because they agree on the coded format, the data will not get interpreted wrongly.

From Mark Byers example

If I want to send

Hello
world!

One way is to send it in ASCII like

72 101 108 108 111 10 119 111 114 108 100 33

But byte 10 might not be interpreted correctly as a newline at the other end. So, we use a subset of ASCII to encode it like this

83 71 86 115 98 71 56 115 67 110 100 118 99 109 120 107 73 61 61

which at the cost of more data transferred for the same amount of information ensures that the receiver can decode the data in the intended way, even if the receiver happens to have different interpretations for the rest of the character set.

13 Answers

Here is a summary of my understanding after reading what others have posted:

Important!

Base64 encoding is not meant to provide security

Base64 encoding is not meant to compress data

Why do we use Base64

Base64 is a text representation of data that consists of only 64 characters which are the alphanumeric characters (lowercase and uppercase), +, / and =. These 64 characters are considered ‘safe’, that is, they can not be misinterpreted by legacy computers and programs unlike characters such as <, > \n and many others.

When is Base64 useful

I've found base64 very useful when transfering files as text. You get the file's bytes and encode them to base64, transmit the base64 string and from the receiving side you do the reverse.

This is the same procedure that is used when sending attachments over SMTP during emailing.

How to perform base64 encoding/decoding

Conversion from base64 text to bytes is called decoding. Conversion from bytes to base64 text is called encoding. This is a bit different from how other encodings/decodings are named.

Dotnet and Powershell

Microsoft's Dotnet framework has support for encoding and decoding bytes to base64. Look for the Convert namespace in the mscorlib library.

Below are powershell commands you can use:

// Base64 encode PowerShell 
// See: https://adsecurity.org/?p=478
$Text='This is my nice cool text'
$Bytes = [System.Text.Encoding]::Unicode.GetBytes($Text)
$EncodedText = [Convert]::ToBase64String($Bytes)
$EncodedText


// Convert from base64 to plain text 
[System.Text.Encoding]::Unicode.GetString([Convert]::FromBase64String('VABoAGkAcwAgAGkAcwAgAG0AeQAgAG4AaQBjAGUAIABjAG8AbwBsACAAdABlAHgAdAA='))
Output>This is my nice cool text 

Bash has a built-in command for base64 encoding/decoding. You can use it like this:

To encode to base64:

echo 'hello' | base64

To decode base64-encoded text to normal text:

echo 'aGVsbG8K' | base64 -d

Node.js also has support for base64. Here is a class that you can use:


/**
 * Attachment class.
 * Converts base64 string to file and file to base64 string
 * Converting a Buffer to a string is known as decoding.
 * Converting a string to a Buffer is known as encoding.
 * See: https://nodejs.org/api/buffer.html
 * 
 * For binary to text, the naming convention is reversed.
 * Converting Buffer to string is encoding.
 * Converting string to Buffer is decoding.
 *  
 */
class Attachment {
    constructor(){

    }

    /**
     * 
     * @param {string} base64Str 
     * @returns {Buffer} file buffer
     */
    static base64ToBuffer(base64Str) {
        const fileBuffer = Buffer.from(base64Str, 'base64');
        // console.log(fileBuffer)
        return fileBuffer;
    }

    /**
     * 
     * @param {Buffer} fileBuffer 
     * @returns { string } base64 encoded content
     */
    static bufferToBase64(fileBuffer) {
        const base64Encoded = fileBuffer.toString('base64')
        // console.log(base64Encoded)
        return base64Encoded
    }
}

You get the file buffer like so:

  const fileBuffer = fs.readFileSync(path);

Or like so:

const buf = Buffer.from('hey there');

You can also use an API to do for you the encoding and encoding, here is one:

To encode, you pass in the plain text as the body.

POST https://mk34rgwhnf.execute-api.ap-south-1.amazonaws.com/base64-encode

To decode, pass in the base64 string as the body.

POST https://mk34rgwhnf.execute-api.ap-south-1.amazonaws.com/base64-decode

Related