[JavaScript] JavaScript的Base64编码代码片段 →→→→→进入此内容的聊天室

来自 , 2020-04-30, 写在 JavaScript, 查看 152 次.
URL http://www.code666.cn/view/a4df48d0
  1. var Base64 = {
  2.  
  3. // private property
  4. _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
  5.  
  6. // public method for encoding
  7. encode : function (input) {
  8.     var output = "";
  9.     var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
  10.     var i = 0;
  11.  
  12.     input = Base64._utf8_encode(input);
  13.  
  14.     while (i < input.length) {
  15.  
  16.         chr1 = input.charCodeAt(i++);
  17.         chr2 = input.charCodeAt(i++);
  18.         chr3 = input.charCodeAt(i++);
  19.  
  20.         enc1 = chr1 >> 2;
  21.         enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  22.         enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  23.         enc4 = chr3 & 63;
  24.  
  25.         if (isNaN(chr2)) {
  26.             enc3 = enc4 = 64;
  27.         } else if (isNaN(chr3)) {
  28.             enc4 = 64;
  29.         }
  30.  
  31.         output = output +
  32.         this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
  33.         this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
  34.  
  35.     }
  36.  
  37.     return output;
  38. },
  39.  
  40. // public method for decoding
  41. decode : function (input) {
  42.     var output = "";
  43.     var chr1, chr2, chr3;
  44.     var enc1, enc2, enc3, enc4;
  45.     var i = 0;
  46.  
  47.     input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
  48.  
  49.     while (i < input.length) {
  50.  
  51.         enc1 = this._keyStr.indexOf(input.charAt(i++));
  52.         enc2 = this._keyStr.indexOf(input.charAt(i++));
  53.         enc3 = this._keyStr.indexOf(input.charAt(i++));
  54.         enc4 = this._keyStr.indexOf(input.charAt(i++));
  55.  
  56.         chr1 = (enc1 << 2) | (enc2 >> 4);
  57.         chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  58.         chr3 = ((enc3 & 3) << 6) | enc4;
  59.  
  60.         output = output + String.fromCharCode(chr1);
  61.  
  62.         if (enc3 != 64) {
  63.             output = output + String.fromCharCode(chr2);
  64.         }
  65.         if (enc4 != 64) {
  66.             output = output + String.fromCharCode(chr3);
  67.         }
  68.  
  69.     }
  70.  
  71.     output = Base64._utf8_decode(output);
  72.  
  73.     return output;
  74.  
  75. },
  76.  
  77. // private method for UTF-8 encoding
  78. _utf8_encode : function (string) {
  79.     string = string.replace(/\r\n/g,"\n");
  80.     var utftext = "";
  81.  
  82.     for (var n = 0; n < string.length; n++) {
  83.  
  84.         var c = string.charCodeAt(n);
  85.  
  86.         if (c < 128) {
  87.             utftext += String.fromCharCode(c);
  88.         }
  89.         else if((c > 127) && (c < 2048)) {
  90.             utftext += String.fromCharCode((c >> 6) | 192);
  91.             utftext += String.fromCharCode((c & 63) | 128);
  92.         }
  93.         else {
  94.             utftext += String.fromCharCode((c >> 12) | 224);
  95.             utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  96.             utftext += String.fromCharCode((c & 63) | 128);
  97.         }
  98.  
  99.     }
  100.  
  101.     return utftext;
  102. },
  103.  
  104. // private method for UTF-8 decoding
  105. _utf8_decode : function (utftext) {
  106.     var string = "";
  107.     var i = 0;
  108.     var c = c1 = c2 = 0;
  109.  
  110.     while ( i < utftext.length ) {
  111.  
  112.         c = utftext.charCodeAt(i);
  113.  
  114.         if (c < 128) {
  115.             string += String.fromCharCode(c);
  116.             i++;
  117.         }
  118.         else if((c > 191) && (c < 224)) {
  119.             c2 = utftext.charCodeAt(i+1);
  120.             string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
  121.             i += 2;
  122.         }
  123.         else {
  124.             c2 = utftext.charCodeAt(i+1);
  125.             c3 = utftext.charCodeAt(i+2);
  126.             string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
  127.             i += 3;
  128.         }
  129.  
  130.     }
  131.  
  132.     return string;
  133. }
  134. }
  135.  
  136. //encode
  137. Base64.encode("Test"); //VGVzdA==
  138.  
  139. //decode
  140. Base64.decode("VGVzdA=="); // Test
  141. //javascript/4341

回复 "JavaScript的Base64编码代码片段"

这儿你可以回复上面这条便签

captcha