base 64 decode and encode


/ Published in: JavaScript
Save to your folder(s)

will encode text to base 64 and decode


Copy this code and paste it in your HTML
  1. var b64array = "ABCDEFGHIJKLMNOP" +
  2. "QRSTUVWXYZabcdef" +
  3. "ghijklmnopqrstuv" +
  4. "wxyz0123456789+/" +
  5. "=";
  6.  
  7. function encode64(input) {
  8.  
  9. var base64 = "";
  10. var hex = "";
  11. var chr1, chr2, chr3 = "";
  12. var enc1, enc2, enc3, enc4 = "";
  13. var i = 0;
  14.  
  15. do {
  16. chr1 = input.charCodeAt(i++);
  17. chr2 = input.charCodeAt(i++);
  18. chr3 = input.charCodeAt(i++);
  19.  
  20. enc1 = chr1 >> 2;
  21. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  22. enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  23. enc4 = chr3 & 63;
  24.  
  25. if (isNaN(chr2)) {
  26. enc3 = enc4 = 64;
  27. } else if (isNaN(chr3)) {
  28. enc4 = 64;
  29. }
  30.  
  31. base64 = base64 +
  32. b64array.charAt(enc1) +
  33. b64array.charAt(enc2) +
  34. b64array.charAt(enc3) +
  35. b64array.charAt(enc4);
  36. chr1 = chr2 = chr3 = "";
  37. enc1 = enc2 = enc3 = enc4 = "";
  38. } while (i < input.length);
  39.  
  40. //console.log(base64 );
  41. return base64;
  42.  
  43.  
  44. }
  45.  
  46. function decode64(input) {
  47.  
  48. var output = "";
  49. var hex = "";
  50. var chr1, chr2, chr3 = "";
  51. var enc1, enc2, enc3, enc4 = "";
  52. var i = 0;
  53.  
  54. var base64test = /[^A-Za-z0-9\+\/\=]/g;
  55.  
  56. if (!base64test.exec(input)) {
  57. throw new Error("There were invalid base64 characters in the input text.\n" +
  58. "Valid base64 characters are A-Z, a-z, 0-9, '+', '/',and '='\n" +
  59. "Expect errors in decoding.");
  60. }
  61.  
  62. //input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
  63.  
  64. do {
  65. enc1 = b64array.indexOf(input.charAt(i++));
  66. enc2 = b64array.indexOf(input.charAt(i++));
  67. enc3 = b64array.indexOf(input.charAt(i++));
  68. enc4 = b64array.indexOf(input.charAt(i++));
  69.  
  70. chr1 = (enc1 << 2) | (enc2 >> 4);
  71. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  72. chr3 = ((enc3 & 3) << 6) | enc4;
  73.  
  74. output = output + String.fromCharCode(chr1);
  75.  
  76. if (enc3 != 64) {
  77. output = output + String.fromCharCode(chr2);
  78. }
  79. if (enc4 != 64) {
  80. output = output + String.fromCharCode(chr3);
  81. }
  82.  
  83. chr1 = chr2 = chr3 = "";
  84. enc1 = enc2 = enc3 = enc4 = "";
  85.  
  86. } while (i < input.length);
  87.  
  88. //console.log(unescape(output));
  89. return output;
  90.  
  91. }
  92.  
  93. var inp = '<input type="hidden" name="ac15e53b6df3e8c82f6d8963793c1330" value="1" />';
  94.  
  95. encode = encode64(inp);
  96. console.log(inp);
  97. console.log(encode);
  98. console.log(decode64(encode));
  99.  
  100. //Compact decode without invalid character checking and more compact
  101.  
  102. function decode64_mini(input) {
  103. var output = "", hex = "", chr1, chr2, chr3 = "", enc1, enc2, enc3, enc4 = "",i = 0,len=input.length;
  104. do {
  105. enc1 = b64array.indexOf(input.charAt(i++));
  106. enc2 = b64array.indexOf(input.charAt(i++));
  107. enc3 = b64array.indexOf(input.charAt(i++));
  108. enc4 = b64array.indexOf(input.charAt(i++));
  109. chr1 = (enc1 << 2) | (enc2 >> 4);
  110. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  111. chr3 = ((enc3 & 3) << 6) | enc4;
  112. output = output + String.fromCharCode(chr1);
  113. if (enc3 != 64) {output = output + String.fromCharCode(chr2);}
  114. if (enc4 != 64) {output = output + String.fromCharCode(chr3);}
  115. chr1 = chr2 = chr3 = enc1 = enc2 = enc3 = enc4 = "";
  116. } while (i < len);
  117. return output;
  118. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.