based64 decoding in javascript

Giganews Newsgroups
Subject: based64 decoding in javascript
Posted by:  mistral (polychr…@softhome.net)
Date: 13 Sep 2006

I need help implement based64 decoding in javascript: a function to
return a script that has been base64 encoded into a string (decoding in
client side). For encode, online base64 encoder tool was used.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>JavaScript Base64 Decoder</title>
<script type="text/javascript">
<!--
/*
//here is base64-encoded string:

PHNjcmlwdCB0eXBlPSJ0ZXh0L3Zic2NyaXB0Ij4gDQpGdW5jdGlvbiBlbmRlY3J5cHRfdmJzKGJvb2wpIA0KICAgIENvbnN0IGRpZmYgPSAxIA0KICAgIERpbSBjaGFyLCBjb2RlLCBpLCB0ZW1wLCB0ZXh0IA0KICAgICAgICB0ZW1wID0gIiIgDQogICAgICAgIHRleHQgPSBkb2N1bWVudC5nZXRFbGVtZW50QnlJZCgidGV4dCIpLnZhbHVlIA0KICAgIEZvciBpID0gMSBUbyBMZW4odGV4dCkgDQogICAgICAgIGNoYXIgPSBBc2MoTWlkKHRleHQsaSwxKSkgDQogICAgICAgIElmIGJvb2wgVGhlbiANCiAgICAgICAgICAgIGNvZGUgPSBjaGFyICsgZGlmZiANCiAgICAgICAgRWxzZSANCiAgICAgICAgICAgIGNvZGUgPSBjaGFyIC0gZGlmZiANCiAgICAgICAgRW5kIElmIA0KICAgICAgICB0ZW1wID0gdGVtcCAmIENocihjb2RlKSANCiAgICBOZXh0IA0KICAgIGRvY3VtZW50LmdldEVsZW1lbnRCeUlkKCJ0ZXh0IikudmFsdWUgPSB0ZW1wIA0KRW5kIEZ1bmN0aW9uIA0KPC9zY3JpcHQ+IA==

//Heres the decode function
function decode64(inp)
{
    var out = ""; //This is the output
    var chr1, chr2, chr3 = ""; //These are the 3 decoded bytes
    var enc1, enc2, enc3, enc4 = ""; //These are the 4 bytes to be decoded
    var i = 0; //Position counter

    // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
    var base64test = /[^A-Za-z0-9\+\/\=]/g;

    }
    inp = inp.replace(/[^A-Za-z0-9\+\/\=]/g, "");

    do { //Here.s the decode loop.

        //Grab 4 bytes of encoded content.
        enc1 = keyStr.indexOf(inp.charAt(i++));
        enc2 = keyStr.indexOf(inp.charAt(i++));
        enc3 = keyStr.indexOf(inp.charAt(i++));
        enc4 = keyStr.indexOf(inp.charAt(i++));

        //Heres the decode part. There.s really only one way to do it.
        chr1 = (enc1 << 2) | (enc2 >> 4);
        chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
        chr3 = ((enc3 & 3) << 6) | enc4;

        //Start to output decoded content
        out = out + String.fromCharCode(chr1);

        if (enc3 != 64) {
            out = out + String.fromCharCode(chr2);
        }
        if (enc4 != 64) {
            out = out + String.fromCharCode(chr3);
        }

        //now clean out the variables used
        chr1 = chr2 = chr3 = "";
        enc1 = enc2 = enc3 = enc4 = "";

    } while (i < inp.length); //finish off the loop

    //Now return the decoded values.
    return out;
}

//-->

</script>
</head>

<body>

</body>
</html>

I'm not expert in javascript, so i dont know the correct method to
decode, the above decoder script  is that i just found.
Another version:

    this.decode = function()
    {
        var binary = new String();
        var result = new String();
        for(i = 0; i < this.string.length; i++)
        {
            for(j = 0; j < this.base64.length; j++)
            {
                if(this.string.charAt(i) == this.base64[j])
                {
                    binary += String("000000" +
j.toString(2)).substring(j.toString(2).length);
                }
            }
        }
        for(i = 0; i < binary.length; i+=8)
        {
            var number = new Number();
            var counter = new Number();
            for(j = 0; j < binary.substring(i, i+8).length; j++)
            {
                for(k = 128; k >= 1; k-=(k/2))
                {
                    if(binary.substring(i, i+8).charAt(counter++) ==
"1")
                    {
                        number += k;
                    }
                }
            }
            result += String.fromCharCode(number);
        }
        return result;
    }
}

thank you.
mistral

Replies