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

Python Base64 Decode

To decode Base64 to original data, Python provides the b64decode function of the base64 module. In addition, this module also provides the b64encode function, as well as other useful functions to decode Base64 strings using several algorithms specified in RFC 3548.

Usage:

  • b64decode(b64) - Discards all invalid characters from b64 and decodes it from Base64 to original data
  • b64decode(b64, altchars) - Replaces altchars with +/ from b64 and decodes it from Base64 to original data
  • b64decode(b64, altchars, validate) - If validate is set to True the b64 must contain only valid Base64 characters

Arguments:

  • b64 (required string) - The Base64 value that should be decoded
  • altchars (optional string; default None) - Two characters that should replace “+” and “/”
  • validate (optional bool; default False; added in Python 3) - Allows to decode only valid Base64 values

Return Values:

  • str - On success, it returns the original data

Supported Versions:

  • Python 2.4+

Changelog:

  • Python 3.1:
    • Adds the decodebytes function, which Base64 values to bytes
    • The function decodestring is deprecated
  • Python 3.3:
    • ASCII-only Unicode strings are now accepted by the decoding functions of the modern interface
  • Python 3.4:
    • Adds the a85decode function, which decodes Ascii85 to original data
    • Adds the b85decode function, which decodes Base85 to original data
    • Any bytes-like objects are now accepted by all decoding functions in the base64 module

Example #1 (print the result):

from base64 import b64decode
b64 = 'Z3VydQ=='
print(b64decode(b64)) #-> 'guru'

Example #2 (print unicode string on Python 3):

from base64 import b64decode
b64 = '4oKsMTAw'
print(b64decode(b64).decode('utf-8')) #-> '€100'

Example #3 (decode data from a custom Base64 alphabet):

from base64 import b64decode
b64 = 'PDw_Pz8-Pg=='
print(b64decode(b64)) #-> '<<<���'
print(b64decode(b64, '-_')) #-> '<<???>>'

Example #4 (validate characters on Python 3):

from base64 import b64decode
b64 = 'Z 3 V y d Q=='
print(b64decode(b64)) #-> b'guru'
print(b64decode(b64, None, True)) #-> binascii.Error: Non-base64 digit found

Example #5 (errors for incorrectly padded Base64 values):

# Python 2.7: TypeError: Incorrect padding
# Python 3.6: binascii.Error: Incorrect padding
b64decode('a=b')

Example #6 (TypeError on missing required argument):

# Python 2.7 - b64decode() takes at least 1 argument (0 given)
# Python 3.6 - b64decode() missing 1 required positional argument: 's'
b64decode()

Example #7 (TypeError on wrong argument type):

# Python 2.7 - a2b_base64() argument 1 must be string or buffer, not int
# Python 3.6 - argument should be a bytes-like object or ASCII string, not 'int'
b64decode(123)
Comments (23)

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

  • Evan,
    How do I decode this? I tried but it didn’t work. I’m a complete noob I don’t know anything my friends sent me this asking me to try and won’t help. Thank you.

    WVcxdGFXdzZMeTltZUhwMExtZHpMM2xvWlhkNGF5OUVPRXN6WVVKclNpTkJRVU5aWlU5bE4zSmZiV2xqZVU1dGJqTlBRVkJ3
  • Jobmen,
    Please seerved seed can someone find the server seed from the Seerved hash (512) String
    123188:8681b78514365ed639ae6620e337c6f70a8b87dca580dd3ae9eebf876096b116:123188
    123188:0000:123188
  • Amol,
    I want to extract readable text from Base64 files(files can be of any format, like, ppt, pdf, images).
    How can i extract readable text?
  • Keval,
    Hello, I'm trying to decode pdf base64 into readable text, how can i do that ?
  • nqhXncMU,
    0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z
  • nqhXncMU,
    -1)) OR 984=(SELECT 984 FROM PG_SLEEP(15))--
  • nqhXncMU,
    fBOIpaIc' OR 737=(SELECT 737 FROM PG_SLEEP(15))--
  • nqhXncMU,
    0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z
  • nqhXncMU,
    1 waitfor delay '0:0:15' --
  • nqhXncMU,
    -1; waitfor delay '0:0:15' --
  • gBqsPxAZ,
    0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z
  • gBqsPxAZ,
    kx60uXqT')) OR 180=(SELECT 180 FROM PG_SLEEP(15))--
  • gBqsPxAZ,
    cncjGc0B' OR 575=(SELECT 575 FROM PG_SLEEP(15))--
  • gBqsPxAZ,
    -1' OR 2+552-552-1=0+0+0+1 or 't4feGOKr'='
  • gBqsPxAZ,
    if(now()=sysdate(),sleep(15),0)
  • gBqsPxAZ,
    555'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'
  • nqhXncMU,
    1nISZ553'; waitfor delay '0:0:15' --
  • nqhXncMU,
    -1)) OR 673=(SELECT 673 FROM PG_SLEEP(15))--
  • ncMUFCMU,
    -1); waitfor delay '0:0:15' --
  • nqhXncMU,
    aQmqiU8r'; 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)+"*/
  • ncMUFCMU,
    -1 OR 2+115-115-1=0+0+0+1
  • ncMUFCMU,
    -1)) OR 657=(SELECT 657 FROM PG_SLEEP(15))--
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.