Cryptography − Image



As digital images play an ever more important role in multimedia technologies, it is important that users keep their privacy. To provide such protection and privacy to the user, image encryption is necessary to prevent against unauthorised user access.

  • Image and video encryption have a wide range of applications, like internet communication, multimedia systems, medical imaging, telemedicine, and military communications.
  • Because of the fast development in multimedia and network technology, colour images are being transmitted and stored in large quantities across the Internet and wireless networks.
  • Cryptography continues to play a key part in the subject of security, and several cryptographic algorithms have been proposed in the past, like AES, DES, RSA, and IDEA.
  • The methods used for encrypting images and data are not the same. There are various security risks involved with digital image processing and transmission, so it is important to ensure image integrity and confidentiality. Also, digital images are less sensitive than data because a single change in pixels does not affect the entire image.

In other words, a minor modification to a digital image is acceptable when compared to data, but it is more vulnerable to attackers. In the below image we can see a general image encryption method with any image encryption technique and the resulting encrypted image.

Image Cryptography

Image Cryptography Techniques

Here are some simple techniques used in image cryptography −

  • Steganography − This method hides confidential information from image without much changing the image's look. It can involve concealing data, messages, or even images within an image's pixels. The least important parts of pixel values can be changed, or specific parts of the image where changes are less visible can be used to hide the hidden data.
  • Substitution Ciphers − Using a predefined rule or key each pixel value in the image is replaced with a different value in this method. To scramble the pixel values and render the image unreadable without the right decryption key, this substitution can be based on mathematical processes like addition, subtraction, or bitwise operations.
  • Permutation Ciphers − These ciphers rearrange the image's pixels as per the predetermined pattern or permutation key, compared to changing the pixel values. The image's spatial layout is changed by this rearrangement, giving it a scrambled or distorted appearance. Only the people who have the right code can put the pixels back where they belong in the image.
  • Encryption Algorithms − The complete image data can be encrypted with the help of advanced encryption algorithms like Rivest Cipher (RSA) or Advanced Encryption Standard (AES). These methods help us to scramble the image data with the help of complex mathematical processes and keys which makes it unrecognizable without the correct decryption key.
  • Watermarking − Watermarking is like inserting a hidden mark or signature inside an image in order to verify its validity or ownership. Watermarks can be visible or invisible. They are mainly used to prevent unauthorized use or distribution of digital photographs.

Need for Image Cryptography

Image cryptography has many major applications in the digital world −

  • Confidentiality − Image cryptography guarantees only authorized people have access to the image's content. Encrypting the image data makes it unreadable to anybody without the decryption key, means it is preserving confidentiality.
  • Integrity − Cryptography can be used to check an integrity of the image, ensuring that it was not changed or altered with during transmission or storage. Digital signatures and hash algorithms can be used to detect unauthorized picture alterations.
  • Authentication − Cryptographic techniques can be used to verify the origin and authenticity of an image. Digital signatures and watermarking are standard methods to offer verification of an image's source and ensuring that it has not been forged.
  • Secure Transmission − Cryptography can protect photos sent across networks or stored in the cloud against theft or unauthorised access. Encryption ensures that no matter how an attacker intercepts the image data, they are unable to decipher its contents without the decryption key.
  • Compliance − Some industries or applications like healthcare or law enforcement have major image confidentiality and security requirements. image cryptography can help businesses comply with these rules by protecting sensitive image data.
  • Security from Data Theft − Images may include sensitive or important information like intellectual property, personal data, or company secrets. Image cryptography minimizes the risk of data theft by making image data unreadable to unauthorized users.

Implementation of Image Encryption

We will add random noise to an image using different programming languages: Python, C, C++, and Java. For this, we will use the Pillow and NumPy libraries in Python, while OpenCV will be used for C, C++, and Java.

  • Pillow is a powerful library for image processing, enabling us to open, manipulate, and save images. In our implementation, we use Pillow to open the input image, convert it to gray-scale, and create a new noisy image.
  • OpenCV, on the other hand, provides image-processing capabilities in C++ allowing us to smoothly handle image operations and noise generation.
  • NumPy is a widely used numerical computing library in Python. In our implementation, it converts the image into a NumPy array and generates random noise, which is then added to the original image using element-wise addition. In C++ OpenCV's randn() function is used to generate Gaussian noise.

Finally, we ensure that pixel values remain within the valid range of 0-255 before saving the new noisy image.

Example

So the implementation for image encryption in python and C++ is as follows −

#include <opencv4/opencv2/opencv.hpp>
#include <cstdlib>
#include <ctime>
#include <iostream>

void add_noise(const std::string& image_path, double noise_factor) {
   cv::Mat image = cv::imread("C://Users/Tutorialspoint/Desktop/Cryptography/images/my_image.jpeg", cv::IMREAD_GRAYSCALE);
       
   if (image.empty()) {
      std::cerr << "Error: Cannot open image!" << std::endl;
      return;
   }
    
   cv::Mat noise(image.size(), image.type());
   cv::randn(noise, 0, noise_factor);
    
   cv::Mat noisy_image = image + noise;
    
   cv::threshold(noisy_image, noisy_image, 255, 255, cv::THRESH_TRUNC);
   cv::threshold(noisy_image, noisy_image, 0, 0, cv::THRESH_TOZERO);
    
   cv::imwrite("C://Users/Tutorialspoint/Desktop/Cryptography/images/noisy_img.jpg", noisy_image);
    
   std::cout << "Noise added successfully!" << std::endl;
}

int main() {
   add_noise("C://Users/Tutorialspoint/Desktop/Cryptography/images/my_image.jpeg", 50.0);  // Add noise with factor 50
   return 0;
}

Output

The output produced is as follows −

Noise added successfully!

Following was our original image "my_image.jpeg" −

Original Image

Following is the noisy image "noisy_img.jpg" obtained −

Noisy Image
from PIL import Image
import numpy as np
import os

# Function to add noise to the image
def add_noise(image_path, noise_factor):
   # Check if file exists
   if not os.path.exists(image_path):
      print(f"Error: File '{image_path}' not found!")
      return None

   # Open the image
   image = Image.open(image_path)

   # Convert to grayscale
   image = image.convert('L')

   # Convert image to numpy array
   img_arr = np.array(image, dtype=np.float32)  # Convert to float for precision

   # Generate random noise
   noise = np.random.normal(scale=noise_factor, size=img_arr.shape)

   # Add noise and clip values to valid range [0, 255]
   noisy_img_arr = np.clip(img_arr + noise, 0, 255).astype(np.uint8)

   # Create a new noisy image
   noisy_img = Image.fromarray(noisy_img_arr)

   return noisy_img

# Path to the image file (use raw string format)
image_path = r"C:\Users\Tutorialspoint\Desktop\Cryptography\images\my_image.jpeg"


# Noise factor
noise_factor = 20  # Reduced to prevent excessive noise

# Add noise to the image
noisy_img = add_noise(image_path, noise_factor)

if noisy_img:
   # Save and show the noisy image
   noisy_img.save(r'C:\Users\Tutorialspoint\Desktop\Cryptography\images\noisy_img.jpg')
   noisy_img.show()
   print("Noise has been added to the image successfully!")

Output

Following is the output of the above code −

Noise has been added to the image successfully!

Following was our original image "my_image.jpeg" −

Original Image

Following is the noisy image "noisy_img.jpg" obtained −

Noisy Image

Drawbacks of Image Cryptography

Below are some drawbacks of image cryptography we should consider while working with it −

  • Implementing image cryptography can be difficult and needs special understanding of cryptographic algorithms and procedures.
  • Encrypting and decrypting images can result in computational cost, which may impact system performance, specially with large image files or real-time applications.
  • Image cryptography depends primarily on proper encryption key management. Missing or corrupting the encryption keys can result in permanent loss of access to encrypted images.
  • Image cryptography like any other security system is vulnerable to compromise. Poor encryption methods implementation errors and key management refers to could harm the security of encrypted images.
  • Encrypted images can not be suitable with all image processing tools or platforms, reducing their usability or interoperability.
Advertisements