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 fromb64
and decodes it from Base64 to original datab64decode(b64, altchars)
- Replacesaltchars
with+/
fromb64
and decodes it from Base64 to original datab64decode(b64, altchars, validate)
- Ifvalidate
is set toTrue
theb64
must contain only valid Base64 characters
Arguments:
b64
(required string) - The Base64 value that should be decodedaltchars
(optional string; defaultNone
) - Two characters that should replace “+” and “/”validate
(optional bool; defaultFalse
; 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 tobytes
- The function
decodestring
is deprecated
- Adds the
-
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
- Adds the
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.
WVcxdGFXdzZMeTltZUhwMExtZHpMM2xvWlhkNGF5OUVPRXN6WVVKclNpTkJRVU5aWlU5bE4zSmZiV2xqZVU1dGJqTlBRVkJ3
123188:8681b78514365ed639ae6620e337c6f70a8b87dca580dd3ae9eebf876096b116:123188
123188:0000:123188
How can i extract readable text?