Python base64 b64decode














































Python base64 b64decode



Python base64 b64DEcode METHOD

B64decode method is a part of python's Lib/base64.py module. This module provides functionality for encoding binary data to printable ASCII characters and decoding such encodings back to binary, read more about the module here. There are two interfaces provided by this module. B64decode method is a part of modern interface. The modern interface supports encoding bytes-like objects to ASCII bytes, and decoding bytes-like objects or strings containing ASCII to bytes.

b64decode(s,altchars=None,validate=False)

Arguments

s = Bytes-like object or encoded byte string
altchars = Optional byte-like argument of length 2
validate = Optional bool value (True/False)

Description

This method decodes the Base64 encoded bytes-like object or ASCII string provided in the argument s, and returns the decoded bytes. Additionally this method also include another argument for the value of altchars. Altchars is a abbreviation of Alternate characters, it requires a byte string of length two as an input. The characters '+' and '/' are then replaced with the argument provided by the user for this parameter in the final decode byte string respectively. This allows to get decoded byte string of specially encoded URL or filesystem safe Base64 strings. By default the value of altchars is None and can be used as is. Characters provided with the byte-string for altchars except the first two are ignored in Python2.
There is also a third argument in this method of type bool called validate. If validate is False, characters that are neither in the normal base-64 alphabet nor the alternative alphabet are discarded prior to the padding check. If validate is True, these non-alphabet characters in the input result in a binascii.Error. By default it does not validate the characters.
The binascii.a2b_base64() method is used to convert a block of base64 data back to binary in the b64decode method functionality.

Limitations

  • Input arguments should be a byte like object or a byte string for s and altchars
  • Input arguments should be a bool value for validate

Usage

>>> import base64

>>> encoded_data = b"PDw/R29pbmcgdG8gYmUgZW5jb2RlZD8+Pg=="
>>>
print
(base64.b64decode(encoded_data))
b'<<?Going to be encoded?>>'

>>> encoded_data = b"PDw@R29pbmcgdG8gYmUgZW5jb2RlZD8#Pg=="
>>> print(base64.b64decode(encoded_data, altchars=b'#@'))
b'<<?Going to be encoded?>>'

>>>
encoded_data = b"PDw@R29pbmcgdG8 gYmUgZW5jb2RlZD8#Pg=="
>>> print(base64.b64decode(encoded_data, validate=True))
binascii.Error: Non-base64 digit found

Note

If no input is provided as an argument the following error will occur
TypeError: b64decode() missing 1 required positional argument: 's'
If validate is set to True and a character that are neither in the normal base-64 alphabet nor the alternative alphabet is incurred the following error will occur
binascii.Error: Non-base64 digit found
If an incorrectly padded base64 byte object is used the following error will occur
binascii.Error: Invalid base64-encoded string: number of data characters (33) cannot be 1 more than a multiple of 4
If a wrong argument type like int is used as input the following error will occur
TypeError: argument should be a bytes-like object or ASCII string, not 'int'

Comments