Python base64 Introduction














































Python base64 Introduction



Introduction to base64 library in python

Base64 is a group of binary to text encoding. The base64 module in python provides functioning for encoding and decoding text or binary data to ASCII and vice-versa respectively. The purpose of encoding is to transform data so that it can be safely consumed by a different type of system. The goal is to ensure that data is consumed properly then to keep the information secret.

The module provides encoding and decoding functions as specified in RFC 3584 which describes the commonly used base 64, base 32, and base 16 encoding schemes. There are two interfaces of this module legacy and modern.

The legacy interface provide functions for encoding and decoding to and from file objects while it does not support decoding from strings. On the other hand the modern interface supports encoding and decoding from bytes-like objects.

 

Importing base64 library

>>> import base64
 

Methods in base64 legacy interface

encode(input,output)

This method takes the contents of the binary input file and write the resulting base64 encoded data to the output file.

decode(input,output)

This method takes the content of the binary input file and write the resulting binary data to the output file.

encodebytes(s)

This method encodes the bytes-like object s and returns bytes having base64-encoded data.

decodebytes(s)

This method decodes the base64 encoded object s and returns decoded bytes.

 

Methods in base64 modern interface

b64encode(s,altchars=None)

This method encodes the bytes-like object s using Base64 and return the encoded bytes.

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

This method decodes the Base64 encoded bytes-like object or ASCII object s and returns the decoded bytes.

standard_b64encode(s)

This method encodes the bytes-like object s using the standard Base64 alphabet and return the encoded bytes.

standard_b64decode(s)

This method decodes the bytes-like object or ASCII string s using the standard Base64 alphabet and return the decoded bytes.

urlsafe_b64encode(s)

This method encodes the bytes-like object s using the URL- and filesystem-safe alphabet, and return the encoded bytes.

urlsafe_b64decode(s)

This method decodes the bytes-like object or ASCII string s using the URL- and filesystem-safe alphabet, and return the decoded bytes.

b32encode(s)

This method encodes the bytes-like object s using Base32 and return the encoded bytes.

b32decode(s,casefold=False,map01=None)

This method decode the Base32 encoded bytes-like object or ASCII string s and return the decoded bytes.

b16encode(s)

This method encodes the bytes-like object s using Base16 and return the encoded bytes.

b16decode(s,casefold=False)

This method decode the Base16 encoded bytes-like object or ASCII string s and return the decoded bytes.

a85encode(b,*,foldspace=False,wrapcol=0,pad=False,adobe=False)

This method encodes the bytes-like object b using Ascii85 and return the encoded bytes.

a85decode(b,*,foldspace=False,adobe=False,ignorechars=b" \t\n\v")

This method decode the Ascii85 encoded bytes-like object or ASCII string b and return the decoded bytes.

b85encode(b,pad=False)

This method encodes the bytes-like object b using base85 and return the encoded bytes.

b85decode(b)

This method decode the base85-encoded bytes-like object or ASCII string b and return the decoded bytes.

 

Example

>>> import base64

>>> encoded_string = base64.b64encode(b'String to be encoded')
>>> encoded_string
b'U3RyaW5nIHRvIGJlIGVuY29kZWQ='

>>> decoded_string = base64.b64decode(encoded_string)
>>> decoded_string
b'String to be encoded'

Comments