andThenYouDie

Automation, IT and more.

08 Sep 2011

Base64_Encode and Decode

Little javascript scriptlet used in OO, to play with Base64


Base64_encode

    var base64_encode = function (a,b,c,d,e,f) {
            b="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
            c="=";
            for(d=f="";e&amp;=3,a.charAt(d++)||(b="=",e);f+=b.charAt(63&amp;c>>++e*2))c=c<<8|a.charCodeAt(d-=!e);
            return f
    };
     
    coded = base64_encode(scriptletInput);
    scriptletResult = coded;

Base64_decode


var base64_decode = function( d,b,c,u,r,q,x ) {
        b="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
        for(r=q=x="";c=d.charAt(x++);~c&amp;&amp;(u=q%4?u*64+c:c,q++%4)?r+=String.fromCharCode(255&amp;u>>(-2*q&amp;6)):0) c=b.indexOf(c);
        return r;
};
 
decoded = base64_decode(scriptletInput);
scriptletResult = decoded;