Search

Monday, May 9, 2016

Base 64 encoding, what is it used for?

Motivation

Problem

Some system only expect string data. Example: url, mail, ...

Related to legacy system ?

Base64

The idea is to safely transport data by encode the data to safely data, i.e.:string, transfer over the network in form of bits, then redo the process backward of the recipient end.

How to it works

  • Each 3 bytes of data are concatenated.
  • The concatenated data then split to groups of 6 bits data (2^6=64,hence it's called base64)
  • Encode the bits using base64 lookup table. Yielded in a string of alphanumeric, +, and / characters.
  • Add padding characters
  • Send the data over the network. The data will be encoded to bits (using ASCII encoding ?). Since the data only contain safe characters (alphanumeric, +, /) it willsafely received on the destination. The receiving end will repeat the process backward.

Example 1: send text

Let's say we want to transfer data :
Hello
world!
It will be sent as ASCII (or UTF-8) as like this :
72 101 108 108 111 10 119 111 114 108 100 33
The byte 10 is corrupted in some systems so we can base 64 encode these bytes as a Base64 string:
SGVsbG8sCndvcmxkIQ==
Which when encoded using ASCII looks like this:
83 71 86 115 98 71 56 115 67 110 100 118 99 109 120 107 73 61 61
All the bytes here are known safe bytes, so there is very little chance that any system will corrupt this message. I can send this instead of my original message and let the receiver reverse the process to recover the original message.

Drawback

It will increase the size of data to 4 [n/3]

Reffs

  • en.wikipedia.org/wiki/Base64
  • stackoverflow.com/questions/3538021/why-do-we-use-base64

No comments:

Post a Comment