Close

How we can encode/decode base64 strings in python? Examples with code

How we can encode/decode base64 strings in python

Base64 is a base-64 number system that uses the 64 distinct digits represented by the 6 bits.

The Base64 encoding method transfers 8-bit information over a 7-bit data route by using the 7-bit ASCII characters used in email.

Bytes with binary or text data are converted to ASCII characters using the Base64 encoding. Encoding protects data from becoming damaged when it is transferred or processed through a text-only system. We’ll talk about Base64 encoding and decoding in python.

What is base64 encoding?

All data in computers is conveyed as a series of 1s and 0s. Some communication routes and apps, on the other hand, are unable to comprehend all of the data they get. This is the case because the type of data a series of 1s and 0s represents determines its meaning. If the number 10110001 is a letter or an image, it must be handled accordingly.

You can get past this restriction by transforming your data to text, which increases the likelihood of it being properly delivered and processed. Base64 is a commonly used method for converting binary data to ASCII characters that is supported by nearly all networks and applications.

Base64 encoding is used extensively on mail servers, which is a common real-world case. They were designed to handle text data, but we anticipate them to send photographs and other media in addition to messages. Your multimedia data will be Base64 encoded before being transferred in certain circumstances. When they are received, they will be Base64 decoded so that they can be used by an application.

How to encode base64 data in python?

Data is encoded and decoded with Python’s base64 package. The strings are first transformed to byte-like objects before being encoded with the base64 module. You can use a base64 encoder to encode the strings. But further we have also elaborate the process by giving the examples of each process. The example below demonstrates that the implementation of encoding strings is not based on base64 characters.

Example

sample string = “GeeksForGeeks is the best”

sample_string_bytes = sample_string.encode(“ascii”)

base64_bytes = base64.b64encode(sample_string_bytes)

base64_string = base64_bytes.decode(“ascii”)

print(f”Encoded string: {base64_string}”)

Output

Encoded string: R2Vla3NGb3JHZWVrcyBpcyB0aGUgYmVzdA==

Example 2

message = “Python is fun”

message_bytes = message.encode(‘ascii’)

base64_bytes = base64.b64encode(message_bytes)

base64_message = base64_bytes.decode(‘ascii’)

print(base64_message)

Output:

The base64 module was first imported in the code above. Our input string to be encoded is stored in the message variable. We use the string’s encoding method to transform it to a bytes-like object, which we store in message_bytes. Using the base64.b64encode method, we Base64 encode message_bytes and save the result in base64_bytes. By decoding the base64_bytes as ASCII, we can finally retrieve the string representation of the Base64 conversion.

What is base64 decoding?

The online decoder allows you to decode files in base64 format with a single click.

The following is the result of decoding the text “Base64”:

“Geico”

How to decode base64 data in python?

Decoding a Base64 string is the polar opposite of encoding it. We convert Base64 strings to unencoded data bytes first, then to a bytes-like object, which is then converted to a string. The decoding of the given example encodes string output is shown in the example below.

Example 1

base64_string =” R2Vla3NGb3JHZWVrcyBpcyB0aGUgYmVzdA ==”

base64_bytes = base64_string.encode(“ascii”)

sample_string_bytes = base64.b64decode(base64_bytes)

sample_string = sample_string_bytes.decode(“ascii”)

print(f”Decoded string: {sample_string}”)

Output

Decoded string: GeeksForGeeks is the best

Example 2

base64 import

base64 message =’UHl0aG9uIGlzIGZ1bg==’ base64 message =’UHl0aG9uIGlzIGZ1bg==’ base64 message =’U

base64 bytes is the same as base64 message.

encode(‘ascii’)

base64.b64 = message bytes

decode(base64 bytes)

message bytes are a variable that represents the size of a message.

decode(‘ascii’)

print(message)

Solution

We’ll have to import the base64 module once more. Then we convert our message to a byte object using encode (‘ASCII’). The base64 bytes in our message byte variable are decoded with the base64.b64decode function. Finally, we convert the message’s bytes into a string object message that humans can understand.

To see the output from this file, run it:

decoding text.py python3

Python is a lot of fun.

Summary

We learned about base64 encoding and decoding in this article. In addition, we learned how to use Python to encode and decode base64 data. You have already seen that this subject is not tough. You will master the knowledge once you get it. You can alternatively encode and decode the issues using an online base64 encoder/decoder.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Leave a comment
scroll to top