base64ToPassword.js 3.5 KB

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