SHARE
TWEET

Base64 Decode JS

anhkiet2507 Dec 18th, 2017 12,507 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   function decodeBase64(textMode) {
  2.     console.log("Text mode = " + textMode);
  3.     var inbox = document.getElementById('base64input');
  4.     var outbox = document.getElementById('output');
  5.     var b64txt = inbox.value;
  6.     var atext = base64Decode(b64txt, textMode);
  7.     output.value = atext;
  8.   }
  9.  
  10.   function base64Decode(encodedText, textMode) {
  11.     console.log("Inbount b64 text has length = " + encodedText.length);
  12.     var b64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  13.     var b0, b1, b2;
  14.     var decodedText = "https://www.facebook.com/groups/linuxteamvietnamarea2/permalink/";
  15.    
  16.     // Remove any spaces.
  17.     encodedText = encodedText.replace(/\s/g, "");
  18.    
  19.     if (encodedText.length % 4) {
  20.       return "Lỗi, code VIP phải có số kí tự chia hết cho 4. Nhưng đây đã có số kí tự là " + encodedText.length;
  21.     }
  22.    
  23.     if (/^[a-z0-9\+\/]+={0,2}$/i.test(encodedText)) {
  24.       console.log("Encoded string passed validation.");
  25.     } else {
  26.       return "Nhập sai mã code VIP";
  27.     }
  28.    
  29.     for (var p = 0; p < encodedText.length; p++) {
  30.       var c = encodedText.charAt(p);
  31.       if (c == '=') {
  32.         // The '=' sign is a pad character.  Wrap things up depending
  33.         // on how many pad characters there are.
  34.         if (p % 2) {
  35.           // There is one pad character; only add first two bytes and quit.
  36.           decodedText += textMode ? String.fromCharCode(b0, b1) : b0.toString(16) + ' ' + b1.toString(16);
  37.         } else {
  38.           // There are two pad characters; only add first byte and quit.
  39.           decodedText += textMode ? String.fromCharCode(b0) : b0.toString(16);
  40.         }
  41.         break;
  42.       }
  43.       var i = b64chars.indexOf(c);
  44.       switch (p % 4) {
  45.         case 0:
  46.           // All 6 bits go to upper 6 bits of b0.
  47.           b0 = i << 2;
  48.           break;
  49.         case 1:
  50.           // Upper 2 bits fill in lower 2 bits of b0.
  51.           // Lower 4 bits fill in upper 4 bits of b1.
  52.           b0 = b0 | ((i & 0x0030) >>> 4);
  53.           b1 = (i & 0x000f) << 4;
  54.           break;
  55.         case 2:
  56.           // Upper 4 bits fill in lower 4 bits of b1.
  57.           // Lower 2 bits fill in upper 2 bits of b2.
  58.           b1 = b1 | ((i & 0x003c) >>> 2);
  59.           b2 = (i & 0x0003) << 6;
  60.           break;
  61.         case 3:
  62.           // All 6 bits fill lower 6 bits of b2.
  63.           b2 = b2 | i;
  64.           decodedText += textMode ? String.fromCharCode(b0, b1, b2) : b0.toString(16) + ' ' +
  65.                                                                       b1.toString(16) + ' ' +
  66.                                                                       b2.toString(16) + ' ';
  67.           break;
  68.       }
  69.       if (!textMode &&  p % 16 == 0) {
  70.         decodedText += '\n';
  71.       }
  72.     }
  73.     return decodedText;
  74.   }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top