Base 64 encoding, Java or JavaScript? - Stack Overflow

i've a particular js function that encrypts some form inputs into base64, but I need to run it in

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 plicated. Here's some of js 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 >> 2;
         enc2 = (((chr1  << 4) | (chr2 >> 4))& 0x3f);
         enc3 = (((chr2  << 2) | (chr3 >> 6))& 0x3f);
         enc4 = chr3 & 0x3f;



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

    if(nMod == 1)
    {
        chr1 = input.charCodeAt(i++);
        enc1 = ((chr1 & 192)>>2);
        enc2 = ((chr1 & 3) <<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 & 192)>>2);
        enc2 = ((chr1 & 3) << 4 )|((chr2 & 0xf0) >> 4);
        enc3 = ((chr2 & 15) <<2);
        enc4 = "=";

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

      return output;
}

Thanks so much!

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 plicated. Here's some of js 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 >> 2;
         enc2 = (((chr1  << 4) | (chr2 >> 4))& 0x3f);
         enc3 = (((chr2  << 2) | (chr3 >> 6))& 0x3f);
         enc4 = chr3 & 0x3f;



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

    if(nMod == 1)
    {
        chr1 = input.charCodeAt(i++);
        enc1 = ((chr1 & 192)>>2);
        enc2 = ((chr1 & 3) <<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 & 192)>>2);
        enc2 = ((chr1 & 3) << 4 )|((chr2 & 0xf0) >> 4);
        enc3 = ((chr2 & 15) <<2);
        enc4 = "=";

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

      return output;
}

Thanks so much!

Share Improve this question asked Nov 18, 2009 at 13:50 user213762user213762 2
  • So you have a javascript function that you want to call from Java? If thats the case, not likely to happen. What exactly are you trying to do? – Nick Larsen Commented Nov 18, 2009 at 13:54
  • I've to get output string from this js in order to login – user213762 Commented Nov 18, 2009 at 13:59
Add a ment  | 

5 Answers 5

Reset to default 3

but I think will be more plicated.

Why would it be more plicated? You can perfectly do that in Java. If your actual problem is already the first line

input = escape(input);

then it's good to know that the Java equivalent is the URLEncoder#encode(). As to the remnant of the coding, it's ultimately straightforward. Just replace var by String or char here and there, align the methods according java.lang.String API and you'll be fine.

Edit: for some downvoting nitpickers out here: I did NOT say that URLEncoder#encode() does the Base64 encoding. It just does URL encoding the same way as Javascript's escape() function does. That was just the first line of his to-be-translated Javascript code. Please read answers, do not scan answers.

Or better use mons-codec 's Base64 class

P.S. You do not "encrypt" into Base64 - you "encode"

You could do that using a javascript engine written in Java, but I think it's better to simply translate it into java.

I have used http://iharder.sourceforge/current/java/base64/ and it works.

Translate the code into Java - it will be quicker. (Or search for an existing example in Java!)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745424680a4627120.html

相关推荐

  • Base 64 encoding, Java or JavaScript? - Stack Overflow

    i've a particular js function that encrypts some form inputs into base64, but I need to run it in

    4小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信