Skip to main content

How to Base64 Encode and Base64 Decode an Image and Text using Python

Python Secret #Author: ManishHacker1

Hello friends... Today my article about Encode and Decode in our text content and binary image using base64 algorithm. In this article, i will show you how to encode and decode our text content and binary image using base64 algorithm.

What is Base64?

Base64 is a group of similar binary to text encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation. The term Base64 originates from a specific MIME content transfer encoding.

Full article read about base64

Source:Read about Base64

Why we use Base64?

when we sent a text mail and other multimedia attachment to other person then email program use SMTP protocol and MIME(Multi Internet Mail Extensions) protocol respectively for sending mail successfully. Suppose you have a binary image file and you want to transfer whole network. But file was not received properly to other side. It contained only strange character. The reason behind that your file in its raw bits and bytes format, while media used was designed for streaming text.

First Example for text Content:

Base64 Encode text content


import base64
obj = open("Encode.txt","w")
obj.write(base64.b64encode(raw_input("Write your message: ")))      
obj.close()

The above code encode your text content which you want to write in your file and save as Encode.txt in your drive. When you open your Encode.txt file, file contain some strange character.

Well, If you want to Decode your file, run below program.


import base64
data1 = open("Encode.txt","r")
s=data1.read()
obj = open("Dicode.txt","w")
obj.write(base64.b64decode(s))
obj.close()

When you run above program, Your Encode.txt Decode your origanal content and save as same location as Decode.txt.

My another example for an Image:

Encode an Image
I am going to show you, how to Base64 Encode your image using Python.
I will be using following binary image. Assume that which name is panda.gif.


First import base64 module in your program.


import base64

Let see full program, how to read binary image and print and save as txt file in your hard drive.


import base64 
image = open('panda.gif', 'rb') #open binary file in read mode
image_read = image.read()
image_encode = base64.encodestring(image_read)
print image_encode

img = open('readImg.txt','w')
img.write(image_encode)
img.close()

The above program read your binary image and print encoding process and also save as readImg.txt file in your hard drive.

Decoding an Image

Now we will be decode an Image, we will be simple use base64.decodestring(s) funtion. So now putting it all together in your program using base64 encode and decode and image together in our program and see what happended.


import base64
img = open('panda.gif','rb')
img_read = img.read()
img_encode = base64.encodestring(img_read)
img_decode = base64.decodestring(img_encode)
img_result = open('panda_origenal.gif','wb') # create a writable Image
img_result.write(img_decode)
img_result.close()

The above program first encode your Image after that decode your image and save as in origanal form panda_origanel.gif in your hard drive. You can use this simple method in your diffirent application whenever you need it.

Thank You for reading this article. And also like my FB page givin below link and share it.

Kryptora Digital Security Provided Python Training and Ethical Hacking Training
  • Best Python Training in Noida
  • Best Python Training in Delhi
  • Best Python Training in Meerut
  • Best Python Training in India
  • Best Ethical Hacking Training in Noida
  • Best Ethical Hacking Training in Delhi
  • Best Ethical Hacking Training in Meerut
  • Best Ethical Hacking Training in India

Follow ManishHacker1

Comments

Popular posts from this blog

How to create Folder Lock with Password Protected using Python

Amazon.in Widgets Python Blog #Author: ManishHacker1 Hello guys, Today we will be learn how to create folder lock with password protected using Python programming language. What is Folder Lock? Folder Lock is a data security software that is allows its users to encrypt thier files and folder. Lock, hide and password protects files and folder on your computer. You can use Folder Lock to secure your files and folder on windows. So, I am going to tell you how to make your own Folder Lock, without using any software. Pre-Reuisites: You will need only pyhton installed in your operating system. if you want to distribute your Folder Lock then you will be also need pyinstaller using to convert our python file to .exe file. Compatibility: Compatible with any Microsoft Windows Operating system. Instructions: First open your python IDE or Notepad, where you want to write code. If you use to notepad please remember indentation using to write code. Or copy th...

How to Connect Window Machine to Linux Machine Using Python via SSH

Python Secret #Author: ManishHacker1 Hello everyone, This is my another article about SSH connectivity to the client machine. In my previous article we have some trouble using pxssh module on windows machine. That's why I am writing another article for the SSH connection. In this article we will use paramiko module to connect another machine. This is fully supported windows, Mac and Linux machine users. In this article, I will show you how to use Paramiko SSH (a Python SSH library) to connect and gather information from another Machine. What is Paramiko? Paramiko is a Python (2.6+, 3.3+) implementation of the SSHv2 protocol [1], providing both client and server functionality. While it leverages a Python C extension for low level cryptography (Cryptography), Paramiko itself is a pure Python interface around SSH networking concepts. Read Full Documentation Let start our demonstration: Requirment: Paramiko( For SSH connection) How to Install paramilko modu...

How to Create MD5 Brute Force Script Using Python

Python Secret #Author: ManishHacker1 How to Create MD5 Brute Force Script Using Python Hello guys, Today we will be learn How to create MD5 brute force script using Python. What is brute-force Attack? Brute-force attack also known exhaustive key search Process of checking all possible keys Using a dictionary to attack with Dictionary is usually more effective than searching the whole key space. Exponentially grow with increasing key size. Brute force Attack Limit Because time/energy required to crack a key grows exponentially with key size, encryption in today's standards and computing power are safe brute-force attack. A 256 bit key would take on 50 of today's super computer 3x10^51 years What is Dictionary Attack Much faster than a whole key space search Not guaranteed Commonaly used on passwords Dictionaries can be found online A popular one is darkc0de.lst Good to run before a big dictionary like darkc0de. Can eliminate the most com...