Base 64 encoding, Java or JavaScript?
<p>i've a particular js function that encrypts some form inputs into base64, but I need to run it in my Java app. So my question is, how can i call that function inside a java class? Otherwise I'll have to translate it but I think will be more complicated. Here's some of js code:</p>

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

      var nMod =( input.length) % 3;
      //alert(nMod);
      //alert(input.length);
      do 
      {
         chr1 = input.charCodeAt(i++);
         chr2 = input.charCodeAt(i++);
         chr3 = input.charCodeAt(i++);

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



         output = output + 
            keyStr.charAt(enc1) + 
            keyStr.charAt(enc2) + 
            keyStr.charAt(enc3) + 
            keyStr.charAt(enc4);
         chr1 = chr2 = chr3 = "";
         enc1 = enc2 = enc3 = enc4 = "";
      } while (i &lt; input.length);

    if(nMod == 1)
    {
     chr1 = input.charCodeAt(i++);
     enc1 = ((chr1 &amp; 192)&gt;&gt;2);
     enc2 = ((chr1 &amp; 3) &lt;&lt;4);
     enc3 = "=";
     enc4 = "=";
     output = output + keyStr.charAt(enc1)
       + keyStr.charAt(enc2)
       + keyStr.charAt(enc3)
       + keyStr.charAt(enc4);
    }
    if(nMod == 2)
    {
     chr1 = input.charCodeAt(i++);
     chr2 = input.charCodeAt(i++);
     enc1 = ((chr1 &amp; 192)&gt;&gt;2);
     enc2 = ((chr1 &amp; 3) &lt;&lt; 4 )|((chr2 &amp; 0xf0) &gt;&gt; 4);
     enc3 = ((chr2 &amp; 15) &lt;&lt;2);
     enc4 = "=";

     output = output + keyStr.charAt(enc1)
       + keyStr.charAt(enc2)
       + keyStr.charAt(enc3)
       + keyStr.charAt(enc4);
    }

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

<p>Thanks so much!</p>
