Related Articles

# Encrypt and Decrypt Image using Python

• Last Updated : 05 Aug, 2021

In this article, we will encrypt/decrypt an image using a simple mathematical logic. It requires two things, data and key and when XOR operation is applied on both the operands i.e data and key, the data gets encrypted but when the same process is done again with same key value data gets decrypted.

### Encryption

It is nothing but a simple process in which we convert our data or information into secrete code to prevent it from unauthorized access and keep it private and secure.

First, we will select an image, and then we will convert that image into a byte array due to which the image data will be totally converted into numeric form, and then we can easily apply the XOR operation on it. Now, whenever we will apply the XOR function on each value of the byte array then the data will be changed due to which we will be unable to access it. But we should remember one thing that here our encryption key plays a very important role without that key we can not decrypt our image. It acts as a password to decrypt it.

Below program depicts the basic approach of encryption:

## Python3

 `# Assign values``data ``=` `1281``key ``=` `27` `# Display values``print``(``'Original Data:'``, data)``print``(``'Key:'``, key)` `# Encryption``data ``=` `data ^ key``print``(``'After Encryption:'``, data)` `# Decryption``data ``=` `data ^ key``print``(``'After Decryption:'``, data)`

Output:

```Original Data: 1281
Key: 27
After Encryption: 1306
After Decryption: 1281```

Here in above program, as we can see that how XOR operation works, it takes two variables data and key, whenever we perform XOR operation on them for the first time we get encrypted data. Then when we perform XOR operation between our data and key again, we get same value as our input variable data (decrypted data). Same logic will be applicable on byte array of Image during encryption and decryption.

Executable code for Encryption:

## Python3

 `# try block to handle exception``try``:``    ``# take path of image as a input``    ``path ``=` `input``(r``'Enter path of Image : '``)``    ` `    ``# taking encryption key as input``    ``key ``=` `int``(``input``(``'Enter Key for encryption of Image : '``))``    ` `    ``# print path of image file and encryption key that``    ``# we are using``    ``print``(``'The path of file : '``, path)``    ``print``(``'Key for encryption : '``, key)``    ` `    ``# open file for reading purpose``    ``fin ``=` `open``(path, ``'rb'``)``    ` `    ``# storing image data in variable "image"``    ``image ``=` `fin.read()``    ``fin.close()``    ` `    ``# converting image into byte array to``    ``# perform encryption easily on numeric data``    ``image ``=` `bytearray(image)` `    ``# performing XOR operation on each value of bytearray``    ``for` `index, values ``in` `enumerate``(image):``        ``image[index] ``=` `values ^ key` `    ``# opening file for writing purpose``    ``fin ``=` `open``(path, ``'wb'``)``    ` `    ``# writing encrypted data in image``    ``fin.write(image)``    ``fin.close()``    ``print``(``'Encryption Done...'``)` `    ` `except` `Exception:``    ``print``(``'Error caught : '``, Exception.__name__)`

Output:

```Enter path of Image : C:\Users\lenovo\Pictures\Instagram\enc.png
Enter Key for encryption of Image : 22
The path of file :  C:\Users\lenovo\Pictures\Instagram\enc.png
Key for encryption :  22
Encryption done...```

Here in the above code first we take the path of image and encryption key as input from the user then we use the file handling concept to handle the binary file and open that file for reading purpose then read binary data of image and store it in image variable. Now we convert that binary form of data into a byte array, then we apply the XOR operation on each value of byte array which changes the data due to which we will be unable to open the image.

### Decryption

It is nothing but a process of converting our encrypted data into a readable form. Here we will again apply the same XOR operation on an encrypted image to decrypt it. But always remember that our encryption key and decryption key must be the same.

Executable Code for Decryption:

## Python3

 `# try block to handle the exception``try``:``    ``# take path of image as a input``    ``path ``=` `input``(r``'Enter path of Image : '``)``    ` `    ``# taking decryption key as input``    ``key ``=` `int``(``input``(``'Enter Key for encryption of Image : '``))``    ` `    ``# print path of image file and decryption key that we are using``    ``print``(``'The path of file : '``, path)``    ``print``(``'Note : Encryption key and Decryption key must be same.'``)``    ``print``(``'Key for Decryption : '``, key)``    ` `    ``# open file for reading purpose``    ``fin ``=` `open``(path, ``'rb'``)``    ` `    ``# storing image data in variable "image"``    ``image ``=` `fin.read()``    ``fin.close()``    ` `    ``# converting image into byte array to perform decryption easily on numeric data``    ``image ``=` `bytearray(image)` `    ``# performing XOR operation on each value of bytearray``    ``for` `index, values ``in` `enumerate``(image):``        ``image[index] ``=` `values ^ key` `    ``# opening file for writing purpose``    ``fin ``=` `open``(path, ``'wb'``)``    ` `    ``# writing decryption data in image``    ``fin.write(image)``    ``fin.close()``    ``print``(``'Decryption Done...'``)`  `except` `Exception:``    ``print``(``'Error caught : '``, Exception.__name__)`

Output:

```Enter path of Image : C:\Users\lenovo\Pictures\Instagram\enc.png
Enter Key for Decryption of Image : 22
The path of file :  C:\Users\lenovo\Pictures\Instagram\enc.png
Note : Encryption key and Decryption key must be same.
Key for Decryption :  22
Decryption done...```

Here in the above Decryption program we use same procedure that we use during Encryption of image.’

Below is the video which depicts the functionality of the above programs using a given image and a key.

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up