base64 encoding of byte array in javascript
<p>I'm trying to figure out how to do the reverse encode of a script that I'm using. I have the decode function. I now have to create the encode function, and I'm struggling a bit, where it details </p>

<pre><code>"chr2 = (enc2 &gt;&gt; 2) | ((enc3 &amp; 0x0F) &lt;&lt; "
</code></pre>

<p>I'm not sure exactly what this means, or how to ensure that the encoded data is padded correctly.</p>

<p>I'm using the decodeAsArray function, to turn into a byte array and that works just fine. Now just trying to do vice-versa, the opposite way to encode the base64, so I'm hoping to create a encodeFromArray function, where I give a byte array as the input to the function.</p>

<pre><code>Base64 = {
        _keyStr: ".ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+=",
        decode: function( input ) {
                    var output = "";
                    var hex = "";
                    var chr1, chr2, chr3 = "";
                    var enc1, enc2, enc3, enc4 = "";
                    var i = 0;
                    var base64test = /[^A-Za-z0-9\+\.\=]/g;

                    do {
                        enc1 = this._keyStr.indexOf(input.charAt(i++)) ;
                        enc2 = this._keyStr.indexOf(input.charAt(i++)) ;
                        enc3 = this._keyStr.indexOf(input.charAt(i++)) ;
                        enc4 = this._keyStr.indexOf(input.charAt(i++)) ;

                        chr1 = (enc1 | ((enc2 &amp; 3) &lt;&lt; 6));                                                                                                             
                        chr2 = (enc2 &gt;&gt; 2) | ((enc3 &amp; 0x0F) &lt;&lt; 4);                                                                                                     
                        chr3 = (enc3 &gt;&gt; 4) | (enc4 &lt;&lt; 2);


                        output = output + String.fromCharCode(chr1);
                        if (enc3 != 64) {
                            output = output + String.fromCharCode(chr2);
                        }
                        if (enc4 != 64) {
                            output = output + String.fromCharCode(chr3);
                        }
                        chr1 = chr2 = chr3 = "";
                        enc1 = enc2 = enc3 = enc4 = "";
                    } while (i &lt; input.length);

                    return (output);
                },

    decodeAsArray: function (b) {
        var d = this.decode(b),
            a = [],
            c;
        for (c = 0; c &lt; d.length; c++) {
            a[c] = d.charCodeAt(c)
        }
        return a
    }
</code></pre>

<p>I think it would look something like the following:</p>

<pre><code>    encode : function (input) {
    var output = "";
    var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
    var i = 0;


    while (i &lt; input.length) {


        chr1 = input.charCodeAt(i++);
        chr2 = input.charCodeAt(i++);
        chr3 = input.charCodeAt(i++);


        enc1 = chr1 &gt;&gt; 2;
        enc2 = ((chr1 &amp; 3) &lt;&lt; 4) | (chr2 &gt;&gt; 4);
        enc3 = ((chr2 &amp; 15) &lt;&lt; 2) | (chr3 &gt;&gt; 6);
        enc4 = chr3 &amp; 63;

        if (isNaN(chr2)) {
            enc3 = enc4 = 64;
        } else if (isNaN(chr3)) {
            enc4 = 64;
        }

        output = output +
        this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
        this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

    }

    return output;
}
</code></pre>

<p>However, I'm not sure how the enc2, enc3 and enc4 should be, based on the decode function, and then I'm also not sure how to do it from byte array</p>