javascript 实现Base64加密

来源:互联网 发布:cnc自动编程软件 编辑:程序博客网 时间:2024/06/06 16:39

想必大家对base64并不陌生吧,在本文将为大家介绍下Js中的base64加密解密过程,感兴趣的朋友不要错过

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <script type="text/javascript">   
  2.   
  3. var base64encodechars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";   
  4. var base64decodechars = new Array(   
  5. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,   
  6. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,   
  7. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,   
  8. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,   
  9. -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,   
  10. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,   
  11. -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,   
  12. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);   
  13.   
  14. function base64encode(str) {   
  15. var out, i, len;   
  16. var c1, c2, c3;   
  17. len = str.length;   
  18. i = 0;   
  19. out = "";   
  20. while (i < len) {   
  21. c1 = str.charCodeAt(i++) & 0xff;   
  22. if (i == len) {   
  23. out += base64encodechars.charAt(c1 >> 2);   
  24. out += base64encodechars.charAt((c1 & 0x3) << 4);   
  25. out += "==";   
  26. break;   
  27. }   
  28. c2 = str.charCodeAt(i++);   
  29. if (i == len) {   
  30. out += base64encodechars.charAt(c1 >> 2);   
  31. out += base64encodechars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xf0) >> 4));   
  32. out += base64encodechars.charAt((c2 & 0xf) << 2);   
  33. out += "=";   
  34. break;   
  35. }   
  36. c3 = str.charCodeAt(i++);   
  37. out += base64encodechars.charAt(c1 >> 2);   
  38. out += base64encodechars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xf0) >> 4));   
  39. out += base64encodechars.charAt(((c2 & 0xf) << 2) | ((c3 & 0xc0) >> 6));   
  40. out += base64encodechars.charAt(c3 & 0x3f);   
  41. }   
  42. return out;   
  43. }   
  44.   
  45. function base64decode(str) {   
  46. var c1, c2, c3, c4;   
  47. var i, len, out;   
  48.   
  49. len = str.length;   
  50.   
  51. i = 0;   
  52. out = "";   
  53. while (i < len) {   
  54.   
  55. do {   
  56. c1 = base64decodechars[str.charCodeAt(i++) & 0xff];   
  57. } while (i < len && c1 == -1);   
  58. if (c1 == -1)   
  59. break;   
  60.   
  61. do {   
  62. c2 = base64decodechars[str.charCodeAt(i++) & 0xff];   
  63. } while (i < len && c2 == -1);   
  64. if (c2 == -1)   
  65. break;   
  66.   
  67. out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));   
  68.   
  69. do {   
  70. c3 = str.charCodeAt(i++) & 0xff;   
  71. if (c3 == 61)   
  72. return out;   
  73. c3 = base64decodechars[c3];   
  74. } while (i < len && c3 == -1);   
  75. if (c3 == -1)   
  76. break;   
  77.   
  78. out += String.fromCharCode(((c2 & 0xf) << 4) | ((c3 & 0x3c) >> 2));   
  79.   
  80. do {   
  81. c4 = str.charCodeAt(i++) & 0xff;   
  82. if (c4 == 61)   
  83. return out;   
  84. c4 = base64decodechars[c4];   
  85. } while (i < len && c4 == -1);   
  86. if (c4 == -1)   
  87. break;   
  88. out += String.fromCharCode(((c3 & 0x03) << 6) | c4);   
  89. }   
  90. return out;   
  91. }   
  92. </script>   

0 0