szca_base64.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. var szca_base64 = {
  2. _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
  3. encode: function(e) {
  4. var t = "";
  5. var n, r, i, s, o, u, a;
  6. var f = 0;
  7. e = szca_base64._utf8_encode(e);
  8. while (f < e.length) {
  9. n = e.charCodeAt(f++);
  10. r = e.charCodeAt(f++);
  11. i = e.charCodeAt(f++);
  12. s = n >> 2;
  13. o = (n & 3) << 4 | r >> 4;
  14. u = (r & 15) << 2 | i >> 6;
  15. a = i & 63;
  16. if (isNaN(r)) {
  17. u = a = 64
  18. } else if (isNaN(i)) {
  19. a = 64
  20. }
  21. t = t + this._keyStr.charAt(s) + this._keyStr.charAt(o) + this._keyStr.charAt(u) + this._keyStr.charAt(a)
  22. }
  23. return t
  24. },
  25. decode: function(e) {
  26. var t = "";
  27. var n, r, i;
  28. var s, o, u, a;
  29. var f = 0;
  30. e = e.replace(/[^A-Za-z0-9+/=]/g, "");
  31. while (f < e.length) {
  32. s = this._keyStr.indexOf(e.charAt(f++));
  33. o = this._keyStr.indexOf(e.charAt(f++));
  34. u = this._keyStr.indexOf(e.charAt(f++));
  35. a = this._keyStr.indexOf(e.charAt(f++));
  36. n = s << 2 | o >> 4;
  37. r = (o & 15) << 4 | u >> 2;
  38. i = (u & 3) << 6 | a;
  39. t = t + String.fromCharCode(n);
  40. if (u != 64) {
  41. t = t + String.fromCharCode(r)
  42. }
  43. if (a != 64) {
  44. t = t + String.fromCharCode(i)
  45. }
  46. }
  47. t = szca_base64._utf8_decode(t);
  48. return t
  49. },
  50. _utf8_encode: function(e) {
  51. e = e.replace(/rn/g, "n");
  52. var t = "";
  53. for (var n = 0; n < e.length; n++) {
  54. var r = e.charCodeAt(n);
  55. if (r < 128) {
  56. t += String.fromCharCode(r)
  57. } else if (r > 127 && r < 2048) {
  58. t += String.fromCharCode(r >> 6 | 192);
  59. t += String.fromCharCode(r & 63 | 128)
  60. } else {
  61. t += String.fromCharCode(r >> 12 | 224);
  62. t += String.fromCharCode(r >> 6 & 63 | 128);
  63. t += String.fromCharCode(r & 63 | 128)
  64. }
  65. }
  66. return t
  67. },
  68. _utf8_decode: function(e) {
  69. var t = "";
  70. var n = 0;
  71. var r = c1 = c2 = 0;
  72. while (n < e.length) {
  73. r = e.charCodeAt(n);
  74. if (r < 128) {
  75. t += String.fromCharCode(r);
  76. n++
  77. } else if (r > 191 && r < 224) {
  78. c2 = e.charCodeAt(n + 1);
  79. t += String.fromCharCode((r & 31) << 6 | c2 & 63);
  80. n += 2
  81. } else {
  82. c2 = e.charCodeAt(n + 1);
  83. c3 = e.charCodeAt(n + 2);
  84. t += String.fromCharCode((r & 15) << 12 | (c2 & 63) << 6 | c3 & 63);
  85. n += 3
  86. }
  87. }
  88. return t
  89. }
  90. }