欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java 和 JavaScript 真正通用的Base64编码详解

程序员文章站 2024-03-09 15:25:17
 java 和 javascript base64编码 在开发java  web应用的时候,可能会在服务器端用java做base64编码,而在客户端用...

 java 和 javascript base64编码

在开发java  web应用的时候,可能会在服务器端用java做base64编码,而在客户端用javascript进行解码。这样就要求两边的base64编码机制保持一致。

使用base64编码,可能会碰到各种奇怪情况,甚至怀疑编码有bug。但实际上不是这样的。base64理论上操作的对象不是字符串而是字节数组。它的原理就是把ascii码的255个字符缩小到用64个来表示。具体就是原来三个字节用四个字节表示,编码后长度有一定的增长。

1) 最好一次编码,避免分段编码,确实要分段编码,每一段字节数应该是3的倍数。

长字节流,如果要边读取边编码,每一段必须是3的倍数,否则就可能在还原的时候出乱。一般人喜欢用2的乘方来定义数组,例如 byte[1024],因为不是3的倍数,可能还原时出错。正确的例子是:

byte[] bs=new byte[3*100] ....inputstream.read(bs)......encode(bs )....

对于字符串,一般要整个一次编码,以避免分段编码出错。

当然,如果你分段编码,还原的时候也是一段一段地还原,那是没有问题的。

2)确保字符串还原的时候按照原来的编码还原。

因为它操作的是字节数组,所以对于gbk编码的汉字和utf-8编码汉字,经过 base64编码后结果是不一样的。例如“我们”这两个字如果是gbk编码,转成base64后就是ztldxw== ;如果是utf-8编码,转成base64后就是5oir5lus。

也就是 “我们” ==》  getbytes("gbk") ==> base64

所以java这边用什么编码转换,在javascript那边就要用什么编码还原。要保证java和javascript通用,我们采用unicode的编码(javascript转成utf-8、gbk不方便,所以就采用了其本身的unicode编码),具体如下:

服务器端:

1)用getbytes("unicode")转成unicode字节数组。

2) 编码成base64字符串

3)传送到客户端

客户端:

1)base64 解码成字节数组

2)按unicode还原

代码如下(相关的函数看附件):

base64.encode(data,"unicode"); //java 端编码

decode64(data);   //javascript解码

附一:java中base64编码

package websharp.util; 
public class base64 { 
  private static final byte[] encodingtable = { 
      (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd', (byte) 'e', 
      (byte) 'f', (byte) 'g', (byte) 'h', (byte) 'i', (byte) 'j', 
      (byte) 'k', (byte) 'l', (byte) 'm', (byte) 'n', (byte) 'o', 
      (byte) 'p', (byte) 'q', (byte) 'r', (byte) 's', (byte) 't', 
      (byte) 'u', (byte) 'v', (byte) 'w', (byte) 'x', (byte) 'y', 
      (byte) 'z', (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd', 
      (byte) 'e', (byte) 'f', (byte) 'g', (byte) 'h', (byte) 'i', 
      (byte) 'j', (byte) 'k', (byte) 'l', (byte) 'm', (byte) 'n', 
      (byte) 'o', (byte) 'p', (byte) 'q', (byte) 'r', (byte) 's', 
      (byte) 't', (byte) 'u', (byte) 'v', (byte) 'w', (byte) 'x', 
      (byte) 'y', (byte) 'z', (byte) '0', (byte) '1', (byte) '2', 
      (byte) '3', (byte) '4', (byte) '5', (byte) '6', (byte) '7', 
      (byte) '8', (byte) '9', (byte) '+', (byte) '/' 
    }; 
  private static final byte[] decodingtable; 
  static { 
    decodingtable = new byte[128]; 
    for (int i = 0; i < 128; i++) { 
      decodingtable[i] = (byte) -1; 
    } 
    for (int i = 'a'; i <= 'z'; i++) { 
      decodingtable[i] = (byte) (i - 'a'); 
    } 
    for (int i = 'a'; i <= 'z'; i++) { 
      decodingtable[i] = (byte) (i - 'a' + 26); 
    } 
    for (int i = '0'; i <= '9'; i++) { 
      decodingtable[i] = (byte) (i - '0' + 52); 
    } 
    decodingtable['+'] = 62; 
    decodingtable['/'] = 63; 
  } 
  public static byte[] encode(byte[] data,int offset) { 
    byte[] bytes; 
    int realcount=data.length-offset; 
    int modulus = realcount % 3; 
    if (modulus == 0) { 
      bytes = new byte[(4 * realcount) / 3]; 
    } else { 
      bytes = new byte[4 * ((realcount / 3) + 1)]; 
    } 
    int datalength = (data.length - modulus); 
    int a1; 
    int a2; 
    int a3; 
    for (int i = offset, j = 0; i < datalength; i += 3, j += 4) { 
      a1 = data[i] & 0xff; 
      a2 = data[i + 1] & 0xff; 
      a3 = data[i + 2] & 0xff; 
      bytes[j] = encodingtable[(a1 >>> 2) & 0x3f]; 
      bytes[j + 1] = encodingtable[((a1 << 4) | (a2 >>> 4)) & 0x3f]; 
      bytes[j + 2] = encodingtable[((a2 << 2) | (a3 >>> 6)) & 0x3f]; 
      bytes[j + 3] = encodingtable[a3 & 0x3f]; 
    } 
    int b1; 
    int b2; 
    int b3; 
    int d1; 
    int d2; 
    switch (modulus) { 
    case 0: /* nothing left to do */ 
      break; 
    case 1: 
      d1 = data[data.length - 1] & 0xff; 
      b1 = (d1 >>> 2) & 0x3f; 
      b2 = (d1 << 4) & 0x3f; 
      bytes[bytes.length - 4] = encodingtable[b1]; 
      bytes[bytes.length - 3] = encodingtable[b2]; 
      bytes[bytes.length - 2] = (byte) '='; 
      bytes[bytes.length - 1] = (byte) '='; 
      break; 
    case 2: 
      d1 = data[data.length - 2] & 0xff; 
      d2 = data[data.length - 1] & 0xff; 
      b1 = (d1 >>> 2) & 0x3f; 
      b2 = ((d1 << 4) | (d2 >>> 4)) & 0x3f; 
      b3 = (d2 << 2) & 0x3f; 
      bytes[bytes.length - 4] = encodingtable[b1]; 
      bytes[bytes.length - 3] = encodingtable[b2]; 
      bytes[bytes.length - 2] = encodingtable[b3]; 
      bytes[bytes.length - 1] = (byte) '='; 
      break; 
    } 
    return bytes; 
  } 
  public static byte[] decode(byte[] data) { 
    byte[] bytes; 
    byte b1; 
    byte b2; 
    byte b3; 
    byte b4; 
    data = discardnonbase64bytes(data); 
    if (data[data.length - 2] == '=') { 
      bytes = new byte[(((data.length / 4) - 1) * 3) + 1]; 
    } else if (data[data.length - 1] == '=') { 
      bytes = new byte[(((data.length / 4) - 1) * 3) + 2]; 
    } else { 
      bytes = new byte[((data.length / 4) * 3)]; 
    } 
    for (int i = 0, j = 0; i < (data.length - 4); i += 4, j += 3) { 
      b1 = decodingtable[data[i]]; 
      b2 = decodingtable[data[i + 1]]; 
      b3 = decodingtable[data[i + 2]]; 
      b4 = decodingtable[data[i + 3]]; 
      bytes[j] = (byte) ((b1 << 2) | (b2 >> 4)); 
      bytes[j + 1] = (byte) ((b2 << 4) | (b3 >> 2)); 
      bytes[j + 2] = (byte) ((b3 << 6) | b4); 
    } 
    if (data[data.length - 2] == '=') { 
      b1 = decodingtable[data[data.length - 4]]; 
      b2 = decodingtable[data[data.length - 3]]; 
      bytes[bytes.length - 1] = (byte) ((b1 << 2) | (b2 >> 4)); 
    } else if (data[data.length - 1] == '=') { 
      b1 = decodingtable[data[data.length - 4]]; 
      b2 = decodingtable[data[data.length - 3]]; 
      b3 = decodingtable[data[data.length - 2]]; 
      bytes[bytes.length - 2] = (byte) ((b1 << 2) | (b2 >> 4)); 
      bytes[bytes.length - 1] = (byte) ((b2 << 4) | (b3 >> 2)); 
    } else { 
      b1 = decodingtable[data[data.length - 4]]; 
      b2 = decodingtable[data[data.length - 3]]; 
      b3 = decodingtable[data[data.length - 2]]; 
      b4 = decodingtable[data[data.length - 1]]; 
      bytes[bytes.length - 3] = (byte) ((b1 << 2) | (b2 >> 4)); 
      bytes[bytes.length - 2] = (byte) ((b2 << 4) | (b3 >> 2)); 
      bytes[bytes.length - 1] = (byte) ((b3 << 6) | b4); 
    } 
    return bytes; 
  } 
  public static byte[] decode(string data) { 
    byte[] bytes; 
    byte b1; 
    byte b2; 
    byte b3; 
    byte b4; 
    data = discardnonbase64chars(data); 
    if (data.charat(data.length() - 2) == '=') { 
      bytes = new byte[(((data.length() / 4) - 1) * 3) + 1]; 
    } else if (data.charat(data.length() - 1) == '=') { 
      bytes = new byte[(((data.length() / 4) - 1) * 3) + 2]; 
    } else { 
      bytes = new byte[((data.length() / 4) * 3)]; 
    } 
    for (int i = 0, j = 0; i < (data.length() - 4); i += 4, j += 3) { 
      b1 = decodingtable[data.charat(i)]; 
      b2 = decodingtable[data.charat(i + 1)]; 
      b3 = decodingtable[data.charat(i + 2)]; 
      b4 = decodingtable[data.charat(i + 3)]; 
      bytes[j] = (byte) ((b1 << 2) | (b2 >> 4)); 
      bytes[j + 1] = (byte) ((b2 << 4) | (b3 >> 2)); 
      bytes[j + 2] = (byte) ((b3 << 6) | b4); 
    } 
    if (data.charat(data.length() - 2) == '=') { 
      b1 = decodingtable[data.charat(data.length() - 4)]; 
      b2 = decodingtable[data.charat(data.length() - 3)]; 
      bytes[bytes.length - 1] = (byte) ((b1 << 2) | (b2 >> 4)); 
    } else if (data.charat(data.length() - 1) == '=') { 
      b1 = decodingtable[data.charat(data.length() - 4)]; 
      b2 = decodingtable[data.charat(data.length() - 3)]; 
      b3 = decodingtable[data.charat(data.length() - 2)]; 
      bytes[bytes.length - 2] = (byte) ((b1 << 2) | (b2 >> 4)); 
      bytes[bytes.length - 1] = (byte) ((b2 << 4) | (b3 >> 2)); 
    } else { 
      b1 = decodingtable[data.charat(data.length() - 4)]; 
      b2 = decodingtable[data.charat(data.length() - 3)]; 
      b3 = decodingtable[data.charat(data.length() - 2)]; 
      b4 = decodingtable[data.charat(data.length() - 1)]; 
      bytes[bytes.length - 3] = (byte) ((b1 << 2) | (b2 >> 4)); 
      bytes[bytes.length - 2] = (byte) ((b2 << 4) | (b3 >> 2)); 
      bytes[bytes.length - 1] = (byte) ((b3 << 6) | b4); 
    } 
    for(int i=0;i<bytes.length;i++) system.out.println(","+bytes[i]); 
    return bytes; 
  } 
  private static byte[] discardnonbase64bytes(byte[] data) { 
    byte[] temp = new byte[data.length]; 
    int bytescopied = 0; 
    for (int i = 0; i < data.length; i++) { 
      if (isvalidbase64byte(data[i])) { 
        temp[bytescopied++] = data[i]; 
      } 
    } 
    byte[] newdata = new byte[bytescopied]; 
    system.arraycopy(temp, 0, newdata, 0, bytescopied); 
    return newdata; 
  } 
  private static string discardnonbase64chars(string data) { 
    stringbuffer sb = new stringbuffer(); 
    int length = data.length(); 
    for (int i = 0; i < length; i++) { 
      if (isvalidbase64byte((byte) (data.charat(i)))) { 
        sb.append(data.charat(i)); 
      } 
    } 
    return sb.tostring(); 
  } 
  private static boolean isvalidbase64byte(byte b) { 
    if (b == '=') { 
      return true; 
    } else if ((b < 0) || (b >= 128)) { 
      return false; 
    } else if (decodingtable[b] == -1) { 
      return false; 
    } 
    return true; 
  } 
  public static string encode(string data,string charset)throws exception 
  { 
    // byte[] result = (data.getbytes("unicode")); 
     if(data==null || data.length()==0) return data; 
     int offset=0; 
     // getbytes("unicode")转完后会在前头加上两字节”fe“ 
     byte[] result=encode (data.getbytes(charset),offset); 
     stringbuffer sb=new stringbuffer(result.length); 
     for (int i=0;i<result.length;i++)  sb.append((char)result[i]); 
     return sb.tostring(); 
  } 
  public static string decode(string data,string charset)throws exception 
  { 
    if(data==null || data.length()==0) return data; 
    return new string(base64.decode(data),charset); 
  } 
  public static void main(string[] args) throws exception { 
    string data = "我们"; 
    string data1=encode(data,"unicode"); 
    string data2=decode(data1,"unicode"); 
    system.out.println(data); 
    system.out.println(data1); 
    system.out.println(data2); 
  } 
}

附二:javascript中base64编码

<html> 
<head> 
<title>base64 encoding/decoding</title> 
</head> 
<script type="text/javascript"><!-- 
var keystr = "abcdefghijklmnop" + 
       "qrstuvwxyzabcdef" + 
       "ghijklmnopqrstuv" + 
       "wxyz0123456789+/" + 
       "=";

function encode64(input) { 
  input = unicodetobytes(input); 
  var output = ""; 
  var chr1, chr2, chr3 = ""; 
  var enc1, enc2, enc3, enc4 = ""; 
  var i = 0;

  do { 
   chr1 = input[i++]; 
   chr2 = input[i++]; 
   chr3 = input[i++];

   enc1 = chr1 >> 2; 
   enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); 
   enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); 
   enc4 = chr3 & 63;

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

   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);

  return output; 
}

function decode64(input) { 
  var output = ""; 
  var chr1, chr2, chr3 = ""; 
  var enc1, enc2, enc3, enc4 = ""; 
  var i = 0;

  // remove all characters that are not a-z, a-z, 0-9, +, /, or = 
  var base64test = /[^a-za-z0-9/+///=]/g; 
  if (base64test.exec(input)) { 
   alert("there were invalid base64 characters in the input text./n" + 
      "valid base64 characters are a-z, a-z, 0-9, '+', '/', and '='/n" + 
      "expect errors in decoding."); 
  } 
  input = input.replace(/[^a-za-z0-9/+///=]/g, ""); 
  output=new array(); 
  do { 
   enc1 = keystr.indexof(input.charat(i++)); 
   enc2 = keystr.indexof(input.charat(i++)); 
   enc3 = keystr.indexof(input.charat(i++)); 
   enc4 = keystr.indexof(input.charat(i++));

   chr1 = (enc1 << 2) | (enc2 >> 4); 
   chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); 
   chr3 = ((enc3 & 3) << 6) | enc4;

   output.push(chr1); 
   if (enc3 != 64) { 
     output.push(chr2); 
   } 
   if (enc4 != 64) { 
     output.push(chr3); 
   }

   chr1 = chr2 = chr3 = ""; 
   enc1 = enc2 = enc3 = enc4 = "";

  } while (i < input.length); 
  return bytestounicode(output); 
}

 function unicodetobytes(s) 
  { 
   var result=new array(); 
   if(s==null || s=="") return result; 
   result.push(255); // add "fe" to head 
   result.push(254); 
   for(var i=0;i<s.length;i++) 
   { 
    var c=s.charcodeat(i).tostring(16); 
    if(c.length==1) i="000"+c; 
    else if(c.length==2) c="00"+c; 
    else if(c.length==3) c="0"+c; 
    var var1=parseint( c.substring(2),16); 
    var var2=parseint( c.substring(0,2),16); 
    result.push( var1); 
    result.push(var2) ; 
   } 
   return result; 
  }

  function bytestounicode(bs) 
  { 
   var result=""; 
   var offset=0; 
   if(bs.length>=2 && bs[0]==255 && bs[1]==254) offset=2; // delete "fe" 
   for(var i=offset;i<bs.length;i+=2) 
   { 
      var code=bs[i]+(bs[i+1]<<8); 
      result+=string.fromcharcode(code); 
   } 
   return result; 
  } 
//--> 
</script> 
<body> 
<form name="base64form"> 
  type in the message you want to encode in base64, or paste<br> 
  base64 encoded text into the text field, select encode or decode, <br> 
  and click the button!<br>

  <textarea name="thetext" cols="40" rows="6"></textarea><br>

  <input type="button" name="encode" value="encode to base64" 
   onclick="document.base64form.thetext.value=encode64(document.base64form.thetext.value);"> 
  <input type="button" name="decode" value="decode from base64" 
   onclick="document.base64form.thetext.value=decode64(document.base64form.thetext.value);"> 
</form> 
</body> 
</html>

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!