Base64 Encode Decode string using Javascript

Base64 Encode Decode string using Javascript

In this Post We Will Explain About is Base64 Encode Decode string using Javascript With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to Encode and Decode Strings with Base64 in JavaScriptExample

In this post we will show you Best way to implement Base64 encode decode using Javascript, hear for Encode And Decode Base64 Data In Javascriptwith Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

Example

Basic Example:

// to simple encode function a string
var String_encode = window.etod(“Wel-come to Pakainfo.com”); 

// to simple decode function the string
var string_decode = window.dtoe(String_encode); 

Simple Example Base64 encode decode using Javascript

<!DOCTYPE html>
<html>
    <head>
        <title>Simple using Base64 Encode and Decode using Javascript : https://www.pakainfo.com</title>
        <style>
            #data_result{
                height: 300px;
                width: 600px;
                overflow-y: auto;
                border: #DFC dotted 1px;                
            }
        </style>
    </head>
    <body>
Data Result:
        <div id="data_result"> </div>
<table>
            <tr><td>your Enter valid String to Encode: </td><td><input type='text' id='encodeString' value=''></td><td> <button onclick="encodeStr()">Encode Data</button></td></tr>
        <tr><td>your Enter String valid to Decode: </td><td><textarea id="dstr"></textarea></td><td> <button onclick="your_decode_string()">Decode Data</button></td></tr>
</table>
        <script>
            function encodeStr()
            { // simple this will function encode the string
                var my_str_value = document.getElementById("encodeString").value;

                if (my_str_value === '')
                {
                    alert("Please Enter string to encode");
                } else {

                    var data_encrypt = window.btoa(my_str_value);
                    document.getElementById("data_result").innerHTML = data_encrypt;
                }
            }

            function your_decode_string()
            { // this code function will decode the string
                var my_str_value = document.getElementById("dstr").value;
                ;

                if (my_str_value === '')
                {
                    alert("Please Enter your string to simple Decode");
                } else {

                    var data_decrypt = window.atob(my_str_value);
                    document.getElementById("data_result").innerHTML = data_decrypt;
                }
            }
        </script>
 
    </body>
</html>

Simple Encode and Decode Custom Strings with Base64 in JavaScript

var Base64 = {
	//your encode string value
    _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
    encode: function(el) {
        var temp = "";
        var n2, random, i, s, o, u, a;
        var flag = 0;
        el = Base64._utf8_encode(el);
        while (flag < el.length) {
            n2 = el.charCodeAt(flag++);
            random = el.charCodeAt(flag++);
            i = el.charCodeAt(flag++);
            s = n2 >> 2;
            o = (n2 & 3) << 4 | random >> 4;
            u = (random & 15) << 2 | i >> 6;
            a = i & 63;
            if (isNaN(random)) {
                u = a = 64
            } else if (isNaN(i)) {
                a = 64
            }
            temp = temp + this._keyStr.charAt(s) + this._keyStr.charAt(o) +
                this._keyStr.charAt(u) + this._keyStr.charAt(a)
        }
        return temp
    },
    decode: function(el) {
        var temp = "";
        var n2, random, i;
        var s, o, u, a;
        var flag = 0;
        el = el.replace(/[^A-Za-z0-9\+\/\=]/g, "");
        while (flag < el.length) {
            s = this._keyStr.indexOf(el.charAt(flag++));
            o = this._keyStr.indexOf(el.charAt(flag++));
            u = this._keyStr.indexOf(el.charAt(flag++));
            a = this._keyStr.indexOf(el.charAt(flag++));
            n2 = s << 2 | o >> 4;
            random = (o & 15) << 4 | u >> 2;
            i = (u & 3) << 6 | a;
            temp = temp + String.fromCharCode(n2);
            if (u != 64) {
                temp = temp + String.fromCharCode(random)
            }
            if (a != 64) {
                temp = temp + String.fromCharCode(i)
            }
        }
        temp = Base64._utf8_decode(temp);
        return temp
    },
    _utf8_encode: function(el) {
        el = el.replace(/\r\n/g, "\n");
        var temp = "";
        for (var n2 = 0; n2 < el.length; n2++) {
            var random = el.charCodeAt(n2);
            if (random < 128) {
                temp += String.fromCharCode(random)
            } else if (random > 127 && random < 2048) {
                temp += String.fromCharCode(random >> 6 | 192);
                temp += String.fromCharCode(random & 63 | 128)
            } else {
                temp += String.fromCharCode(random >> 12 | 224);
                temp += String.fromCharCode(random >> 6 & 63 | 128);
                temp += String.fromCharCode(random & 63 | 128)
            }
        }
        return temp
    },
    _utf8_decode: function(el) {
        var temp = "";
        var n2 = 0;
        var random = c1 = chdata = 0;
        while (n2 < el.length) {
            random = el.charCodeAt(n2);
            if (random < 128) {
                temp += String.fromCharCode(random);
                n2++
            } else if (random > 191 && random < 224) {
                chdata = el.charCodeAt(n2 + 1);
                temp += String.fromCharCode((random & 31) << 6 | chdata & 63);
                n2 += 2
            } else {
                chdata = el.charCodeAt(n2 + 1);
                c3 = el.charCodeAt(n2 + 2);
                temp += String.fromCharCode((random & 15) << 12 | (chdata & 63) <<
                    6 | c3 & 63);
                n2 += 3
            }
        }
        return temp
    }
}

var mystring = "You are Most Welcome to Pakainfo.com";
console.log(Base64.encode(mystring));

Example

I hope you have Got What is Encode and Decode Strings with Base64 in JavaScript And how it works.I would Like to have FeadBack From My Blog(Pakainfo.com) readers.You