Base64.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. export default function Base64() {
  2. // 私钥
  3. let _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  4. // 加密
  5. this.encode = function(input) {
  6. var output = "";
  7. var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
  8. var i = 0;
  9. input = _utf8_encode(input);
  10. while (i < input.length) {
  11. chr1 = input.charCodeAt(i++);
  12. chr2 = input.charCodeAt(i++);
  13. chr3 = input.charCodeAt(i++);
  14. enc1 = chr1 >> 2;
  15. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  16. enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  17. enc4 = chr3 & 63;
  18. if (isNaN(chr2)) {
  19. enc3 = enc4 = 64;
  20. } else if (isNaN(chr3)) {
  21. enc4 = 64;
  22. }
  23. output = output +
  24. _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
  25. _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
  26. }
  27. return output;
  28. }
  29. // 解密
  30. this.decode = (input) => {
  31. var output = "";
  32. var chr1, chr2, chr3;
  33. var enc1, enc2, enc3, enc4;
  34. var i = 0;
  35. if (input == undefined || input == null) {
  36. } else {
  37. input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
  38. while (i < input.length) {
  39. enc1 = _keyStr.indexOf(input.charAt(i++));
  40. enc2 = _keyStr.indexOf(input.charAt(i++));
  41. enc3 = _keyStr.indexOf(input.charAt(i++));
  42. enc4 = _keyStr.indexOf(input.charAt(i++));
  43. chr1 = (enc1 << 2) | (enc2 >> 4);
  44. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  45. chr3 = ((enc3 & 3) << 6) | enc4;
  46. output = output + String.fromCharCode(chr1);
  47. if (enc3 != 64) {
  48. output = output + String.fromCharCode(chr2);
  49. }
  50. if (enc4 != 64) {
  51. output = output + String.fromCharCode(chr3);
  52. }
  53. }
  54. output = _utf8_decode(output);
  55. return output;
  56. }
  57. }
  58. // private method for UTF-8 encoding
  59. let _utf8_encode = (string) => {
  60. string = string.replace(/\r\n/g, "\n");
  61. var utftext = "";
  62. for (var n = 0; n < string.length; n++) {
  63. var c = string.charCodeAt(n);
  64. if (c < 128) {
  65. utftext += String.fromCharCode(c);
  66. } else if ((c > 127) && (c < 2048)) {
  67. utftext += String.fromCharCode((c >> 6) | 192);
  68. utftext += String.fromCharCode((c & 63) | 128);
  69. } else {
  70. utftext += String.fromCharCode((c >> 12) | 224);
  71. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  72. utftext += String.fromCharCode((c & 63) | 128);
  73. }
  74. }
  75. return utftext;
  76. }
  77. // private method for UTF-8 decoding
  78. let _utf8_decode = (utftext) => {
  79. var string = "";
  80. var i = 0;
  81. var c = c1 = c2 = 0;
  82. var c1 = 0;
  83. var c2 = 0;
  84. var c3 = 0;
  85. while (i < utftext.length) {
  86. c = utftext.charCodeAt(i);
  87. if (c < 128) {
  88. string += String.fromCharCode(c);
  89. i++;
  90. } else if ((c > 191) && (c < 224)) {
  91. c2 = utftext.charCodeAt(i + 1);
  92. string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
  93. i += 2;
  94. } else {
  95. c2 = utftext.charCodeAt(i + 1);
  96. c3 = utftext.charCodeAt(i + 2);
  97. string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
  98. i += 3;
  99. }
  100. }
  101. return string;
  102. }
  103. }