GCS HOW TO
Stealth Data: Encrypt and Embed Messages in Images with Python
Author - GCS :NEION-4L
Published- 28 July 2024
In the realm of cybersecurity, the importance of protection has grown significantly with the advent of AI and the use of visible watermarks.This is an image, simple vector, and/or repeating text.These watermark was good as it protected the act of the owner but faded soon enough as the visible of watermark never help.It made the image with the watermark look unattractive reducing overall quality.So companies like Google Microsoft and other image industry started using a new imperceptable type called imperceptible watermark that is hidden in the the document.
It was a digital file, text or even if cryptography code(binary, hexadecimal,etc).For example, Microsoft Designer owner by Microsoft allows users to generate AI images and add information about its company like date and information on generation so when they come across it again it can be verified. This imperceptible/digital watermark particularly got popular when people started AI to generate fake images of celebrity and event. But companies have taken care of that.Both types has advantage over each other. More documentation at Medium
Secure Image Using Python
Companies protect their via digital watermark by adding a signature such as a text and encryting and hiddimg in image.There are several techniques used.Such as:
- Least Significant Bit (LSB) Modification
- Discrete Cosine Transform (DCT)
- Discrete Wavelet Transform (DWT)
- Principal Component Analysis (PCA)
- Singular Value Decomposition (SVD)
- Spread Spectrum
- Patchwork
- Region-Based Techniques
- Hybrid Techniques
But what will be used in this tutorial I s the LSB as it is simple to implement to vulnerable to noise and compression.
So first, we install the necessary library:
pip install cryptography pillow numpy
Then we get our image, this will be our image:
Now how it will hide the signature is like this:
- Load the image
- Convert to a suitable format like a NumPy Array
- Ask for a signature ket/text/random digit and encrypt with Fernet
- Convert text to binary
- Modify the LEAST SIGNIFICANT BIT of the image to embed the key
Extracting is done in the opposite way
Watermark Image with Encrypted Data
Now all your library and image it set up, we embed the digital watermark:
from PIL import Image
import numpy as np
from cryptography.fernet import Fernet
import base64
# Helper functions
def generate_key(password):
key = base64.urlsafe_b64encode(password.encode('utf-8').ljust(32)[:32])
return key
def encrypt_text(plain_text, password):
key = generate_key(password)
cipher_suite = Fernet(key)
cipher_text = cipher_suite.encrypt(plain_text.encode('utf-8'))
return cipher_text
def decrypt_text(cipher_text, password):
key = generate_key(password)
cipher_suite = Fernet(key)
plain_text = cipher_suite.decrypt(cipher_text)
return plain_text.decode('utf-8')
def text_to_binary(text):
return ''.join(format(c, '08b') for c in text)
def binary_to_text(binary):
text = ''.join(chr(int(binary[i:i+8], 2)) for i in range(0, len(binary), 8))
return text
def embed_data(image_path, output_path, data, password):
image = Image.open(image_path)
image_array = np.array(image)
encrypted_data = encrypt_text(data, password)
binary_data = text_to_binary(encrypted_data)
binary_data += '1111111111111110' # Delimiter to indicate end of data
data_index = 0
for i in range(image_array.shape[0]):
for j in range(image_array.shape[1]):
for k in range(image_array.shape[2]):
if data_index < len(binary_data):
image_array[i, j, k] = (image_array[i, j, k] & 254) | int(binary_data[data_index])
data_index += 1
result_image = Image.fromarray(image_array)
result_image.save(output_path)
print(f"Data embedded in {output_path}")
def extract_data(image_path, password):
image = Image.open(image_path)
image_array = np.array(image)
binary_data = ""
for i in range(image_array.shape[0]):
for j in range(image_array.shape[1]):
for k in range(image_array.shape[2]):
binary_data += str(image_array[i, j, k] & 1)
if binary_data.endswith('1111111111111110'):
encrypted_data = binary_to_text(binary_data[:-16])
return decrypt_text(encrypted_data, password)
# Embedding data
data = "****"
password = "*****"
input_image_path = "/path/to/image"
output_image_path = "/output/to/image"
embed_data(input_image_path, output_image_path, data, password)
Now here we used the signature GCS(NEION-4L) and password for encryption as Aorjc3i*#.Now with our image, when executed, gave us this:
No watermarked
Watermarked
If the digital watermark were to be extracted we use Python
def extract_data(image_path, password):
image = Image.open(image_path)
image_array = np.array(image)
binary_data = ""
for i in range(image_array.shape[0]):
for j in range(image_array.shape[1]):
for k in range(image_array.shape[2]):
binary_data += str(image_array[i, j, k] & 1)
if binary_data.endswith('1111111111111110'):
encrypted_data = binary_to_text(binary_data[:-16])
return decrypt_text(encrypted_data, password)
# Usage
password = "Aorjc3i*#"
extracted_data = extract_data('output_image.jpeg', password)
print(f"Extracted Data: {extracted_data}");
And it will still give us the same data GCS(NEION-4L)
The potential of digital imperceptible watermarks is transformative, offering a blend of innovation and security that seamlessly integrates into our digital lives. Imagine a world where the authenticity of every image, video, or piece of audio can be verified effortlessly, preserving the integrity of creative works without altering their aesthetic essence. This technology opens new horizons for content creators, ensuring that their intellectual property remains protected and respected in an increasingly digital age. It also fosters trust, as consumers can confidently engage with content knowing its origin and authenticity are guaranteed. Embracing digital imperceptible watermarks is a step towards a future where creativity and security coexist harmoniously, empowering both creators and users in the digital landscape.