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



    
        Simple using Base64 Encode and Decode using Javascript : https://www.pakainfo.com
        
    
    
Data Result:
        
your Enter valid String to Encode:
your Enter String valid to Decode:

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

Leave a Comment