Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

1 Answer
Anvi Jain

Functions in the base64 module translate binary data into a subset of ASCII suitable for transmission using plaintext protocols.

The encoding and decoding functions implement specifications in RFC 3548, which defines the Base16, Base32, and Base64 algorithms, and for the de-facto standard Ascii85 and Base85 encodings. The RFC 3548 encodings are suitable for encoding binary data so that it can safely sent by email, used as parts of URLs, or included as part of an HTTP POST request.

The modern interface provided by this module encodes bytes-like objects to ASCII bytes, and decoding bytes-like objects or strings containing ASCII to bytes. Following base-64 alphabets defined in RFC 3548 (normal, and URL- and filesystem-safe) are supported.

The modern interface provides −

base64.b64encode(): Encodes the bytes-like object using Base64 and return the encoded bytes.

base64.b64decode(): Decode the Base64 encoded bytes-like object or ASCII string s and return the decoded bytes.

To demonstrate bse64 module's b64encode() and base64desode() functions, let us first encode a Python string.

>>> import base64
>>> string = 'Python programming'
>>> enc_string = string.encode()
>>> enc_string
b'Python programming'

The this byte object is encoded using b64encode() function

>>> b64_estring = base64.b64encode(enc_string)
>>> b64_estring
b'UHl0aG9uIHByb2dyYW1taW5n'

We can use decode() function to get string out of this byte object.

>>> dec_string = b64_estring.decode()
>>> dec_string
'UHl0aG9uIHByb2dyYW1taW5n'

The encode() function returns byte object

>>> b1 = dec_string.encode()
>>> b1
b'UHl0aG9uIHByb2dyYW1taW5n'

To decode using b64decode() function −

>>> d = base64.b64decode(b1)
>>> d
b'Python programming'

finally obtain original string by decoding above byte object

>>> originalstring = d.decode()
>>> originalstring
'Python programming'

The base64 alphabets may use + and /, if used in URLs, it is necessary to use an alternate encoding to substitute those characters.

urlsafe_b64encode(): Encode bytes-like object s using the URL- and filesystemsafe alphabet, which substitutes - instead of + and _ instead of/in the standard Base64 alphabet, and returns the encoded bytes.

urlsafe_b64decode(): Decode bytes-like object or ASCII string s using the URL and filesystem-safe alphabet, which substitutes - instead of + and _ instead of/in the standard Base64 alphabet, and returns the decoded bytes.

The legacy interface gives functions for encoding and decoding file objects −

base64.encode(input, output): Encode the contents of the binary input file and write the resulting base64 encoded data to the output file. input and output must be file objects. input will be read until input.read() returns an empty bytes object.

base64.decode(input, output): Decode the contents of the binary input file and write the resulting binary data to the output file. input and output must be file objects. input will be read until input.readline() returns an empty bytes object.

First, create a file using 'wb' mode

>>> f1 = open('file.txt','wb')
>>> f1.write('Simple is better than complex'.encode())
>>> f1.close()

Now encode the file using base64.encode method.

>>> f1 = open('file.txt','rb')
>>> f2 = open('file.dat','wb')
>>> base64.encode(f1,f2)
>>> f1.close()
>>> f2.close()

The fil.dat shows following content if opened in notepad

U2ltcGxlIGlzIGJldHRlciB0aGFuIGNvbXBsZXg=

To decode file.dat in filenew.txt use decode() function

>>> f1 = open('file.dat','rb')
>>> f2 = open('filenew.txt','wb')
>>> base64.decode(f1,f2)
>>> f1.close()
>>> f2.close()

Original text should be visible in filenew.txt

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.