JS实现base64加密解密


  1. <script language=javascript> 
  2.  
  3. var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

  4.  
  5. var base64DecodeChars = new Array(  
  6.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  
  7.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  
  8.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,  
  9.   52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,  
  10.   -1, 0, 1, 2, 3,  4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,  
  11.   15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,  
  12.   -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,  
  13.   41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);


  14.   
  15. function base64encode(str) {  
  16.   var out, i, len;  
  17.   var c1, c2, c3;  
  18.   len = str.length;  
  19.   i = 0;  
  20.   out = "";  
  21.   while(i < len) {  
  22.  c1 = str.charCodeAt(i++) & 0xff;  
  23.  if(i == len)  
  24.  {  
  25.    out += base64EncodeChars.charAt(c1 >> 2);  
  26.    out += base64EncodeChars.charAt((c1 & 0x3) << 4);  
  27.    out += "==";  
  28.    break;  
  29.  }  
  30.  c2 = str.charCodeAt(i++);  
  31.  if(i == len)  
  32.  {  
  33.    out += base64EncodeChars.charAt(c1 >> 2);  
  34.    out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));  
  35.    out += base64EncodeChars.charAt((c2 & 0xF) << 2);  
  36.    out += "=";  
  37.    break;  
  38.  }  
  39.  c3 = str.charCodeAt(i++);  
  40.  out += base64EncodeChars.charAt(c1 >> 2);  
  41.  out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));  
  42.  out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6));  
  43.  out += base64EncodeChars.charAt(c3 & 0x3F);  
  44.   }  
  45.   return out;  

  46.  
  47. function base64decode(str) {  
  48.   var c1, c2, c3, c4;  
  49.   var i, len, out;  
  50.   len = str.length;  
  51.   i = 0;  
  52.   out = "";  
  53.   while(i < len) {  
  54.  /* c1 */  
  55.  do {  
  56.    c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff];  
  57.  } while(i < len && c1 == -1);  
  58.  if(c1 == -1)  
  59.    break;  
  60.  /* c2 */  
  61.  do {  
  62.    c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff];  
  63.  } while(i < len && c2 == -1);  
  64.  if(c2 == -1)  
  65.    break;  
  66.  out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));  
  67.  /* c3 */  
  68.  do {  
  69.    c3 = str.charCodeAt(i++) & 0xff;  
  70.    if(c3 == 61)  
  71.  return out;  
  72.    c3 = base64DecodeChars[c3];  
  73.  } while(i < len && c3 == -1);  
  74.  if(c3 == -1)  
  75.    break;  
  76.  out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));  
  77.  /* c4 */  
  78.  do {  
  79.    c4 = str.charCodeAt(i++) & 0xff;  
  80.    if(c4 == 61)  
  81.  return out;  
  82.    c4 = base64DecodeChars[c4];  
  83.  } while(i < len && c4 == -1);  
  84.  if(c4 == -1)  
  85.    break;  
  86.  out += String.fromCharCode(((c3 & 0x03) << 6) | c4);  
  87.   }  
  88.   return out;  

  89.  
  90. function utf16to8(str) {  
  91.   var out, i, len, c;  
  92.   out = "";  
  93.   len = str.length;  
  94.   for(i = 0; i < len; i++) {  
  95.  c = str.charCodeAt(i);  
  96.  if ((c >= 0x0001) && (c <= 0x007F)) {  
  97.    out += str.charAt(i);  
  98.  } else if (c > 0x07FF) {  
  99.    out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));  
  100.    out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));  
  101.    out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));  
  102.  } else {  
  103.    out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));  
  104.    out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));  
  105.  }  
  106.   }  
  107.   return out;  
  108. }  

  1. function utf8to16(str) {  
  2.   var out, i, len, c;  
  3.   var char2, char3;  
  4.   out = "";  
  5.   len = str.length;  
  6.   i = 0;  
  7.   while(i < len) {  
  8.  c = str.charCodeAt(i++);  
  9.  switch(c >> 4)  
  10.  {  
  11.   case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:  
  12.    // 0xxxxxxx  
  13.    out += str.charAt(i-1);  
  14.    break;  
  15.   case 12: case 13:  
  16.    // 110x xxxx  10xx xxxx  
  17.    char2 = str.charCodeAt(i++);  
  18.    out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));  
  19.    break;  
  20.   case 14:  
  21.    // 1110 xxxx 10xx xxxx 10xx xxxx  
  22.    char2 = str.charCodeAt(i++);  
  23.    char3 = str.charCodeAt(i++);  
  24.    out += String.fromCharCode(((c & 0x0F) << 12) |  
  25.     ((char2 & 0x3F) << 6) |  
  26.     ((char3 & 0x3F) << 0));  
  27.    break;  
  28.  }  
  29.   }  
  30.   return out;  
  31. }  
  32.   
  33. function doit() {  
  34.   var f = document.f  
  35.   f.output.value = base64encode(utf16to8(f.source.value))  
  36.   f.decode.value = utf8to16(base64decode(f.output.value))  
  37. }  
  38. </script> 
转载自http://blog.csdn.net/fengzheng0306/archive/2006/04/25/676055.aspx   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值