[关闭]
@PheonixHkbxoic 2017-05-09T09:05:35.000000Z 字数 2268 阅读 649

Base64

IT404.COM


  1. /**
  2. *
  3. * Base64 encode / decode
  4. *
  5. * @author IT404.COM
  6. *
  7. */
  8. function Base64() {
  9. // private property
  10. _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  11. // public method for encoding
  12. this.encode = function (input) {
  13. var output = "";
  14. var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
  15. var i = 0;
  16. input = _utf8_encode(input);
  17. while (i < input.length) {
  18. chr1 = input.charCodeAt(i++);
  19. chr2 = input.charCodeAt(i++);
  20. chr3 = input.charCodeAt(i++);
  21. enc1 = chr1 >> 2;
  22. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  23. enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  24. enc4 = chr3 & 63;
  25. if (isNaN(chr2)) {
  26. enc3 = enc4 = 64;
  27. } else if (isNaN(chr3)) {
  28. enc4 = 64;
  29. }
  30. output = output +
  31. _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
  32. _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
  33. }
  34. return output;
  35. }
  36. // public method for decoding
  37. this.decode = function (input) {
  38. var output = "";
  39. var chr1, chr2, chr3;
  40. var enc1, enc2, enc3, enc4;
  41. var i = 0;
  42. input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
  43. while (i < input.length) {
  44. enc1 = _keyStr.indexOf(input.charAt(i++));
  45. enc2 = _keyStr.indexOf(input.charAt(i++));
  46. enc3 = _keyStr.indexOf(input.charAt(i++));
  47. enc4 = _keyStr.indexOf(input.charAt(i++));
  48. chr1 = (enc1 << 2) | (enc2 >> 4);
  49. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  50. chr3 = ((enc3 & 3) << 6) | enc4;
  51. output = output + String.fromCharCode(chr1);
  52. if (enc3 != 64) {
  53. output = output + String.fromCharCode(chr2);
  54. }
  55. if (enc4 != 64) {
  56. output = output + String.fromCharCode(chr3);
  57. }
  58. }
  59. output = _utf8_decode(output);
  60. return output;
  61. }
  62. // private method for UTF-8 encoding
  63. _utf8_encode = function (string) {
  64. string = string.replace(/\r\n/g,"\n");
  65. var utftext = "";
  66. for (var n = 0; n < string.length; n++) {
  67. var c = string.charCodeAt(n);
  68. if (c < 128) {
  69. utftext += String.fromCharCode(c);
  70. } else if((c > 127) && (c < 2048)) {
  71. utftext += String.fromCharCode((c >> 6) | 192);
  72. utftext += String.fromCharCode((c & 63) | 128);
  73. } else {
  74. utftext += String.fromCharCode((c >> 12) | 224);
  75. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  76. utftext += String.fromCharCode((c & 63) | 128);
  77. }
  78. }
  79. return utftext;
  80. }
  81. // private method for UTF-8 decoding
  82. _utf8_decode = function (utftext) {
  83. var string = "";
  84. var i = 0;
  85. var c = c1 = c2 = 0;
  86. while ( i < utftext.length ) {
  87. c = utftext.charCodeAt(i);
  88. if (c < 128) {
  89. string += String.fromCharCode(c);
  90. i++;
  91. } else if((c > 191) && (c < 224)) {
  92. c2 = utftext.charCodeAt(i+1);
  93. string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
  94. i += 2;
  95. } else {
  96. c2 = utftext.charCodeAt(i+1);
  97. c3 = utftext.charCodeAt(i+2);
  98. string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
  99. i += 3;
  100. }
  101. }
  102. return string;
  103. }
  104. }
  105. var b = new Base64();
  106. var str = b.decode("aGFjay5pdC40MDQuaHRtbA==");
  107. console.log(str)
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注