Typically, most people have seen social media companies market their applications as end-to-end encryption or encrypted communication. Now, a layman would never understand this term as it is quite ambiguous. We can infer that the term refers to some advanced security techniques, as that is the case in this scenario. This concept is under the foundation of cyber security, dealing with all kinds of security techniques. In this Answer, we will be taking a look at the encryption and decryption, and the code implementation of that on an image.
Encryption and decryption are cryptographic processes used to secure data and protect it from unwanted access during transmission or storage. This process involves converting data into
Encryption, as mentioned before, is the process of converting original data into ciphertext to make it unreadable to unauthorized users. It is used to protect sensitive information during communication or storage. The encryption process requires an encryption algorithm and a secret key. The algorithm takes the original data and the key as input and transforms it into a scrambled form (ciphertext), which appears as random characters and is unreadable without the decryption key.
The goal of encryption is to ensure data confidentiality, meaning that even if someone gets a hold on to the encrypted data, they cannot understand its original content. Modern encryption methods are designed to be computationally secure, making it practically impossible to decrypt the ciphertext.
Decryption is the reverse process of encryption. It is the process of converting ciphertext back into its original form using a decryption algorithm and the correct key. Decryption is essential for authorized users who need to access and read the encrypted data.
The combination of encryption and decryption enables secure communication and storage of sensitive information. By using encryption, data can be transmitted or stored securely, ensuring that only the intended recipients or authorized users can access and understand the information. This is particularly crucial in protecting sensitive data such as passwords, financial transactions, personal information, and other confidential data from unauthorized access and potential security breaches.
In this code, we are implementing a simple encryption and decryption technique known as XOR encryption. XOR encryption is a symmetric encryption algorithm that uses the bitwise XOR operation to scramble the original data, making it unreadable without the correct key. The process involves converting the input image and the encryption key into NumPy arrays for efficient element-wise operations. The key is randomly generated to match the shape of the image, ensuring that the XOR operation can be performed on corresponding pixel values. In this program, we are going to use multiple libraries, which are:
cv2
: Used for implementing the functionality of OpenCV.
pip install opencv-python
numpy
: Used for numerical computations and array manipulation.
pip install numpy
import cv2 import numpy as np def xor_encrypt_decrypt(image, key): image_np = np.array(image) key_np = np.array(key) key_np = np.resize(key_np, image_np.shape) result = np.bitwise_xor(image_np, key_np) return result # Load the image image_path = 'image_1.png' image = cv2.imread(image_path) if image is None: print("Error: Unable to load the image.") else: # Define a secret key (should be the same length as the number of pixels in the image) key = np.random.randint(0, 256, size=image.shape, dtype=np.uint8) encrypted_image = xor_encrypt_decrypt(image, key) decrypted_image = xor_encrypt_decrypt(encrypted_image, key) combined_images = np.hstack((image, encrypted_image, decrypted_image)) cv2.imshow('Original | Encrypted | Decrypted', combined_images) cv2.waitKey(0) cv2.destroyAllWindows()
Line 1 – 2: Import the OpenCV and NumPy library.
Line 4: Define a function named xor_encrypt_decrypt
that takes two parameters: image
and key
. This function will perform the XOR encryption and decryption operations on the input image using the given key.
Line 5 – 6: Convert the image
and key
input into a NumPy array and store it in a variable.
Line 8: Resize the key_np
array to match the shape of the image_np
array using NumPy's resize
function. This ensures that the key has the same dimensions as the image for the bitwise XOR operation.
Line 10: Perform the bitwise XOR operation on the converted NumPy arrays using NumPy's bitwise_xor
function, and store the result in a variable.
Line 14: Define the path of the image file to be loaded and store it in a variable.
Line 15: Use OpenCV's imread()
function to load the image from the specified path.
Line 19: Generate a random key using NumPy's random.randint()
function with values between 0 and 255 (inclusive) for each pixel in the image.
Line 21: Encrypt the image
using the XOR encryption function.
Line 23: Decrypt the encrypted_image
using the same key with the XOR encryption function.
Line 25 – 28: This section of code deals with all the methods used to display the output.
Free Resources