Register Login

Encoding and Decoding Base64 Strings in Python

Updated Nov 11, 2021

If you have ever received any PDF via email and found that some of the texts are non-readable or are difficult to understand its strange characters, due to mixing up with symbols, then do not worry. It can happen when your email server was specifically meant or designed to handle text data.

It means the PDF having images is not readable from the server's end as the file format does not support it. Files that have binary data format or bytes that have non-text information such as images get easily corrupted when transferred and treated by text-only systems.

In this article, you will understand these encoding schemes and how to make them work with simple Python programs.

Define the Base64 Encoding Scheme.

Base64 is a type of conversion standard or conversion scheme that turns bytes having binary or text data into ASCII formats or characters. When a conversion takes place from Base64 characters to binary, all its characters are represented in 6 bits of information. This encoding scheme is not at all related to cryptographic encryption.

In discrete and non-discrete mathematics, the base of a number system signifies how many different characters or elements are used to represent any number. The name of Base64 encoding has been derived directly from this mathematical definition of bases. This encoding scheme has 64 characters that help in representing numbers.

The Base64 character set contains:

  • 26 uppercase letters (A - Z)
  • 26 lowercase letters (a - z)
  • 10 numbers (0 - 9)
  • the + and /, for representing new lines

Working of a Base64 Encoding:

For converting a string to Base64, behind the scene, the following steps take place.

  • First, the algorithm will take the ASCII value mapped with each character in the string.
  • Next, it will calculate the 8-bit equivalent binary value of the ASCII code.
  • Next, the algorithm transforms the 8-bit chunk into a 6 bits chunk of information by re-clustering its digits.
  • Next, it converts the 6-bit binary combination to their respective decimal equivalent.
  • Finally, it will leverage a base64 encoding table. That table will help in assigning the respective base64 character for each decimal value.

Let us now take a look at how developers use it in Python by converting the string (in Python) to a Base64 string.

Encoding Strings with Base64 through Python:

Python 3 comes with a base64 module that allows developers to quickly and effortlessly encode and decode a string to its equivalent base64 format. For this, developers have to convert the Python string into a bytes-like object. Then developers can leverage the base64 module for encoding it.

The b64encode() method of the base64 module helps in converting the string message. This method takes a string value as its argument.

Syntax:

base64.b64encode(string_variable)

Program:

import base64
py_string = "Working on Base64 Module"
byte_msg = py_string.encode('ascii')
base64_val = base64.b64encode(byte_msg)
base64_string = base64_val.decode('ascii')
print("The Converted value of the string  \"", py_string, "\"  is" ,base64_string)

Output:

Explanation:

Here, we have to first import the base64 Python module. Then, we will create a variable that stores the normal string. Next, we create another variable that maps the string conversion type to ASCII (American Code for Information Interchange) format.

Then we take that encoded value and pass it as a parameter for base64.b64encode() method and then decode it back to ASCII string format. Finally, we have used the print() function to display the decoded string value.

Decoding Strings with Base64 through Python:

Most of the software or application that uses the encoding technique needs to use the decoding technique also to bring the characters back to their original form. Decoding a Base64 string is typically the reverse of the encoding process.

Here, the developer has to use another set of code to decode the Base64 string into bytes of un-encoded data. We then convert the bytes-like object into a string.

The b64decode() method of the base64 module helps in converting the byte message back to string format.

Syntax:

base64.b64decode(string_variable)

Program:

import base64
py_string = "V29ya2luZyBvbiBCYXNlNjQgTW9kdWxl"
byte_msg = py_string.encode('ascii')
base64_val = base64.b64decode(byte_msg)
base64_string = base64_val.decode('ascii')
print("The Converted value of the string  \"", py_string, "\"  is\n" ,base64_string)

Output:

Explanation:

This one takes a separate function, although the approach remains the same. We have to first import the base64 Python module. Then, we will create a variable that stores the decoded string. Next, we have created another variable that maps the string to its ASCII (American Code for Information Interchange) equivalent format.

Then we take that encoded value and pass it as a parameter for base64.b64decode() method and then decode it back to ASCII string format. Finally, we have used the print() function to display the encoded string value.

Usage of Base64 Encoding and Conversion:

In computer science, all data of various forms get transmitted as 0s and 1s over the internet. But all applications and communication channels cannot understand a standard format of the encoding scheme, i.e., the bits it receives. That is because the definition of a bit sequence represented in one system entirely depends on the type of data it represents.

To solve such data transmission limitations, developers usually use “Data to Text” encoding. It enhances the chances of data being transmitted & treated correctly. For Python developers, Base64 comes to the rescue for getting binary data into ASCII characters.

Conclusion:

So, if you, as a Python developer, want to convert any data bits that your system does not support or is showing strange characters or string sequence, keep in mind that the system is not supporting that encoding scheme and thus need proper conversion. Base64 is the most light-weight and easy to implement module of Python.


×