Base64 is typically used to encode data that may be corrupted during transfer. Before eight bit bytes became a standard many systems such as SMTP (Email) used seven, six and even three bit bytes which led to data being lost during transfer between systems. So a new encoding scheme was developed which represented binary data in the form of text strings which could easily be transferred between systems without any damage to the data.
Base64 is commonly used to encode the binary data of email attachments such as images and documents. It is also used to encode the images and audio files embedded in a webpage.
Base64 was also invented to reduce the expenses of data transfer between systems. In Base64 each character replaces six digits of binary which makes it a more efficient way of data transfer and reduces its cost.
For example, the binary data given above can be reduced to this short string using Base64 encoding:
How to encode and decode a string using Base64 in JavaScript
There are two different functions available in JavaScript named btoa and atob which enable programmers to encode or decode a string; These functions are supported by almost all major web browsers. In this article we will extensively discuss both btoa and atob functions; but first let’s discuss the btoa function:
How to encode a string using Base64 in JavaScript
JavaScript has a built-in function named btoa which can be used to encode a string into a Base64 string:
encodedStr = btoa(originalStr);
console.log(encodedStr);

How to decode a Base64 string in JavaScript
JavaScript provides the atob function to decode a Base64 string into a normal string:
encodedStr = btoa(originalStr);
console.log(encodedStr);
decodedStr = atob(encodedStr);
console.log(decodedStr);

Conclusion
Base64 is a data representation format which converts binary data into a special string of characters, digits and special characters; each character in Base64 string replaces 6 binary digits. Base64 does not conceal data, it only transforms it from one format to another for easier transfer between different systems. The Base64 encoded string should be treated as plain text; it does not protect data from the man in the middle attacks. In this post we learned to encode and decode strings using Base64 in JavaScript.
from https://ift.tt/3EccfSe

0 Comments