7

How to base64 encode and decode in Python

 1 year ago
source link: https://www.laravelcode.com/post/how-to-base64-encode-and-decode-in-python
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to base64 encode and decode in Python

  1252 views

  8 months ago

Python

Base64 encoding and decoding is used when there needs to transfer or store data. Base64 converts text data into binary data.

Every programming language has own functions to encode and decode in Base64 format. Python language uses base64 module to easily methods to encode and decode in base64 format.

First create base64_encode.py file and paste the below code.

import base64

text_data = "This is text message"
ascii_data = text_data.encode('ascii')
base64_byte_data = base64.b64encode(ascii_data)

encoded_data = base64_byte_data.decode('ascii')

print(encoded_data)

Now run the command in Terminal to see output of encoded data.

python3 base64_encode.py
VGhpcyBpcyB0ZXh0IG1lc3NhZ2U=

To decode base64, same way create base64_decode.py file and put the below code.

import base64

encoded_data = "VGhpcyBpcyB0ZXh0IG1lc3NhZ2U="
base64_byte_data = encoded_data.encode('ascii')
ascii_data = base64.b64decode(base64_byte_data)

text_data = ascii_data.decode('ascii')

print(text_data)

And run the command in Terminal to see decoded data.

python3 base64_decode.py
This is text message

I hope it will help you.

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK