How does base64 encoding work and what are the advantages?

What is base64 encoding/decoding, and how can we use this format to our advantage?

 

By converting binary data into a base-64 representation, base64 schemes represent binary data in an ASCII string format (English alphabet, regular punctuation, and control chars). Basically, this implies that every letter (ASCII, UTF8, UTF16, etc.) with a control character may be mapped to the English alphabet (a-z, A-Z, 0-9), for example, and that you can then read them all on a screen or print them out.

First off, whenever you wish to add a product to your cart, Magento uses base64 encode/decode. When you enter the URL "http://loc.magento.com/checkout/cart/add/uenc/aHR0cDovL2xvYy5tYWdlbn...93/" with the POST data "product=93&related product=&super attribute[502]=37&qty," Magento will redirect you to this page.

 

Base64-encoded strings containing the characters "aHR0cDovL2xvYy5tYWdlbnRvLmNvbS9hc2ljcy1tZW4tcy1nZWwta2F5YW5vLXhpaS5odG1s" are visible in HTTP header traffic when viewed with Firebug, LiveHTTPHeaders (add-on for FF), Charles proxy,

 

If you're not sure what it means or how encoded characters are created, keep reading.

 

Additionally, feel free to share your thoughts with others if you have any practical examples of when you have used base64 encode or decode.

 

According to wiki: "Base64 encoding techniques are frequently utilized when binary data has to be encoded and transported through media that are intended to handle textual data. This is done to guarantee that the data gets transported without being altered.

 

In this situation, uenc stands for URL Encoding, and the encoded URL is as follows: http://loc.magento.com/asics-men-s-gel-kayano-xii.html.

 

What do you believe the rationale is behind Magento not using base64, which is around 33% longer, in URLs? You'll learn this later in the essay, hint...

 

As you can see, adding the char "/" to the URL would violate the attribute/value restriction for the "MOD rewrite" - GET parameter and prevent us from sending the most recent URL as a parameter.

To give a complete example, try this: "http://loc.magento.com/checkout/cart/add/uenc/http://loc.magento.com/asics-men-s-gel-kayano-xii.html/product/93/" — it's obviously incorrect.

 

This is but one instance of how base64 may be used. Additionally, in the administration section, you can see that Magento uses base64 to encrypt your filter when you search in a grid using a certain column's value (s). To put it another way, you don't have to worry about the character sets that your client's browser will support. With base64, you can be certain that every character will be sent across the wire.

 

So what must I do in order to encode characters using base64? Use a reliable code or just a little arithmetic to do this for you. Just Google "online base64 decode/encode" programs to get a ton of them. What kind of programming language should be used to create such scripts? Yes, you can use PHP to decode "safe" data sent over the wire by Javascript, and vice versa.

 

Additionally, the method encode base64(what) on line 57 of the file webroot/js/mage/adminhtml/hash.js in Magento contains base64 encoding algorithms.

 

Another illustration may be that you have a web service that only accepts ASCII characters. The receiver wants to get undisturbed data, but you want to save and subsequently move the user's data to another place (API). Base64 is once more used for it. The main drawback is that base64 encoding uses around 33% more space than standard string formatting.

 

Therefore, using base64, you can encrypt and send any sets of binary data over any network, then decode them to recover the original binary data. Nice, huh?

 

Gmail employs base64? Sure!

 

For your particular requirements, you may also create your own base64 algorithm!

 

Did you maybe purchase an extension with strange characters in it at the end but didn't know how to view the source code? If the answer is yes, did you perhaps realize that your code is likely base64 encoded while reading this article? Can you see the "eval()" method in that code, by any chance?

 

If the answer is affirmative, I'll demonstrate how to "reverse engineer" and view your source code for a number of extensions that you may purchase and are base64 encoded in my following post.

 

Remain tuned!

Cookie
We care about your data and would love to use cookies to improve your experience.