Saturday, July 14, 2018

Base64 Encoding and Decoding

Base64 Encoding and Decoding


Base64 encoding converts the binary data into text format, which is passed through communication channel where a user can handle text safely. Base64 is also called as Privacy enhanced Electronic mail (PEM) and is primarily used in email encryption process.
Python includes a module called BASE64 which includes two primary functions as given below −
  • base64.decode(input, output) − It decodes the input value parameter specified and stores the decoded output as an object.
  • Base64.encode(input, output) − It encodes the input value parameter specified and stores the decoded output as an object.

Program for Encoding

You can use the following piece of code to perform base64 encoding −
import base64
encoded_data = base64.b64encode("Encode this text")

print("Encoded text with base 64 is")
print(encoded_data)

Output

The code for base64 encoding gives you the following output −
Base64

Program for Decoding

You can use the following piece of code to perform base64 decoding −
import base64
decoded_data = base64.b64decode("RW5jb2RlIHRoaXMgdGV4dA==")

print("decoded text is ")
print(decoded_data)

Output

The code for base64 decoding gives you the following output −
Base64 Decoding

Difference between ASCII and base64

You can observe the following differences when you work on ASCII and base64 for encoding data −
  • When you encode text in ASCII, you start with a text string and convert it to a sequence of bytes.
  • When you encode data in Base64, you start with a sequence of bytes and convert it to a text string.

Drawback

Base64 algorithm is usually used to store passwords in database. The major drawback is that each decoded word can be encoded easily through any online tool and intruders can easily get the information.

No comments:

Post a Comment