base64.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. *
  3. * Base64 encode / decode
  4. *
  5. * @author haitao.tu
  6. * @date 2010-04-26
  7. * @email tuhaitao@foxmail.com
  8. *
  9. */
  10. function Base64() {
  11. // private property
  12. _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  13. var _this=this;
  14. this.subString=function(strArr){
  15. var str=_this.decode(arguments[0]);
  16. if( arguments.length<2) throw new Error("參數長度應該大於2");
  17. for(var index=1,len=arguments.length;index<len;index++){
  18. if(arguments[index].codeType==0){
  19. str+=arguments[index]["text"];
  20. }else{
  21. str+=_this.decode(arguments[index]["text"]);
  22. }
  23. }
  24. return _this.encode(str);
  25. };
  26. // public method for encoding
  27. this.encode = function (input) {
  28. var output = "";
  29. var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
  30. var i = 0;
  31. input = _utf8_encode(input);
  32. while (i < input.length) {
  33. chr1 = input.charCodeAt(i++);
  34. chr2 = input.charCodeAt(i++);
  35. chr3 = input.charCodeAt(i++);
  36. enc1 = chr1 >> 2;
  37. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  38. enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  39. enc4 = chr3 & 63;
  40. if (isNaN(chr2)) {
  41. enc3 = enc4 = 64;
  42. } else if (isNaN(chr3)) {
  43. enc4 = 64;
  44. }
  45. output = output +
  46. _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
  47. _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
  48. }
  49. return output;
  50. }
  51. // public method for decoding
  52. this.decode = function (input) {
  53. var output = "";
  54. var chr1, chr2, chr3;
  55. var enc1, enc2, enc3, enc4;
  56. var i = 0;
  57. input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
  58. while (i < input.length) {
  59. enc1 = _keyStr.indexOf(input.charAt(i++));
  60. enc2 = _keyStr.indexOf(input.charAt(i++));
  61. enc3 = _keyStr.indexOf(input.charAt(i++));
  62. enc4 = _keyStr.indexOf(input.charAt(i++));
  63. chr1 = (enc1 << 2) | (enc2 >> 4);
  64. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  65. chr3 = ((enc3 & 3) << 6) | enc4;
  66. output = output + String.fromCharCode(chr1);
  67. if (enc3 != 64) {
  68. output = output + String.fromCharCode(chr2);
  69. }
  70. if (enc4 != 64) {
  71. output = output + String.fromCharCode(chr3);
  72. }
  73. }
  74. output = _utf8_decode(output);
  75. return output;
  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. for (var n = 0; n < string.length; n++) {
  82. var c = string.charCodeAt(n);
  83. if (c < 128) {
  84. utftext += String.fromCharCode(c);
  85. } else if((c > 127) && (c < 2048)) {
  86. utftext += String.fromCharCode((c >> 6) | 192);
  87. utftext += String.fromCharCode((c & 63) | 128);
  88. } else {
  89. utftext += String.fromCharCode((c >> 12) | 224);
  90. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  91. utftext += String.fromCharCode((c & 63) | 128);
  92. }
  93. }
  94. return utftext;
  95. }
  96. // private method for UTF-8 decoding
  97. _utf8_decode = function (utftext) {
  98. var string = "";
  99. var i = 0;
  100. var c = c1 = c2 = 0;
  101. while ( i < utftext.length ) {
  102. c = utftext.charCodeAt(i);
  103. if (c < 128) {
  104. string += String.fromCharCode(c);
  105. i++;
  106. } else if((c > 191) && (c < 224)) {
  107. c2 = utftext.charCodeAt(i+1);
  108. string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
  109. i += 2;
  110. } else {
  111. c2 = utftext.charCodeAt(i+1);
  112. c3 = utftext.charCodeAt(i+2);
  113. string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
  114. i += 3;
  115. }
  116. }
  117. return string;
  118. }
  119. }