Skip to content

Commit 90fe813

Browse files
committed
Add new color random grid scheme
1 parent 69a0596 commit 90fe813

7 files changed

Lines changed: 113 additions & 6 deletions

File tree

docs/assets/readme/web_dec1.png

3.57 KB
Loading

docs/assets/readme/web_dec2.png

386 KB
Loading

docs/assets/readme/web_enc1.png

15.3 KB
Loading

docs/assets/readme/web_enc2.png

1.19 MB
Loading
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import numpy as np
2+
from PIL import Image
3+
from scripts.random_grid.rg_grayscale_additive_SS import create_random_grid, create_difference_grid
4+
5+
6+
# Returns a dictionary containing function mappings and metadata for the algorithm (used by the web interface).
7+
def get_config():
8+
return {
9+
"name": "RG - Color (RGB) Additive Secret Sharing",
10+
"description": get_description(),
11+
"requirements": get_requirements(),
12+
"encrypt": encrypt,
13+
"decrypt": decrypt,
14+
"extension": "png",
15+
"image_type": "RGB"
16+
}
17+
18+
19+
# Defines the expected inputs for encryption/decryption (number of images and additional parameters)
20+
def get_requirements():
21+
return {
22+
"encryption": {
23+
"num_images": 1,
24+
"parameters": {} # No extra parameters needed
25+
},
26+
"decryption": {
27+
"num_images": 2,
28+
"parameters": {} # No extra parameters needed
29+
}
30+
}
31+
32+
33+
# Returns a dictionary containing the description and reference links for the algorithm.
34+
def get_description():
35+
return {
36+
"text": "This (2,2) Visual Secret Sharing Scheme extends the general additive secret sharing scheme to handle RGB color images.",
37+
"links": [
38+
{"text": "Kafri & Keren",
39+
"url": "https://doi.org/10.1364/ol.12.000377"}
40+
]
41+
}
42+
43+
44+
# Function to encrypt an image by generating two shares using random grids and difference grids
45+
def encrypt(image):
46+
"""
47+
Encrypts an image by generating two shares using random grids and difference grids.
48+
49+
Parameters:
50+
image (PIL.Image.Image): The input image (PIL Image object) to be encrypted.
51+
52+
Returns:
53+
tuple: A tuple containing two PIL.Image.Image objects (grid1_image and grid2_image),
54+
representing the encrypted shares of the original image.
55+
"""
56+
img_array = np.array(image).astype(int) # Convert PIL Image to numpy array and convert to int type
57+
58+
# Create the first random grid for each channel and stack them
59+
grid1 = np.stack([create_random_grid(img_array.shape[:2]) for _ in range(3)], axis=-1)
60+
grid1_image = Image.fromarray(grid1)
61+
62+
# Create the second grid using modular subtraction
63+
grid2 = create_difference_grid(img_array, grid1)
64+
grid2_image = Image.fromarray(grid2.astype(np.uint8))
65+
66+
return grid1_image, grid2_image
67+
68+
69+
# Function to decrypt two images by overlaying the grids
70+
def decrypt(image1, image2):
71+
"""
72+
Combines two grids by adding the second grid to the first.
73+
74+
Parameters:
75+
image1 (PIL.Image.Image): The first image (PIL Image object), to be converted to numpy array and processed.
76+
image2 (PIL.Image.Image): The second image (PIL Image object), to be converted to numpy array and processed.
77+
78+
Returns:
79+
PIL.Image.Image: The resulting image (PIL Image object) after adding the two grids.
80+
"""
81+
img1_array = np.array(image1.convert('RGB')) # Convert PIL Image to numpy array (RGB)
82+
img2_array = np.array(image2.convert('RGB')) # Convert PIL Image to numpy array (RGB)
83+
84+
# Combine the grids using modular addition
85+
decrypted = img1_array + img2_array
86+
return Image.fromarray(decrypted.astype(np.uint8)) # Convert numpy array back to PIL Image
87+
88+
89+
if __name__ == '__main__':
90+
image_path = '../images/test.png'
91+
output_path = '../images/output/'
92+
93+
# Load and convert the input image to RGB
94+
image = Image.open(image_path).convert('RGB')
95+
96+
# ENCRYPT: Generate shares
97+
share1, share2 = encrypt(image)
98+
share1.save(output_path + "RG1.png")
99+
share2.save(output_path + "RG2.png")
100+
101+
# DECRYPT: Overlay the grids
102+
img_share1 = Image.open(output_path + "RG1.png") # PIL Image (RGB)
103+
img_share2 = Image.open(output_path + "RG2.png") # PIL Image (RGB)
104+
105+
out = decrypt(img_share1, img_share2)
106+
out.save(output_path + "decrypted_image.png")

scripts/random_grid/rg_grayscale_additive_SS.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def create_random_grid(size):
5858
# Function to create a difference grid by subtracting the image from the random grid
5959
def create_difference_grid(image, grid):
6060
"""
61-
Creates a difference grid by subtracting the pixel values of the input image from the random grid.
61+
Creates a difference grid by subtracting the pixel values of the random grid image from the input image.
6262
6363
Parameters:
6464
image (numpy.ndarray): The binary image array (as a numpy array) to subtract from the grid.
@@ -73,7 +73,7 @@ def create_difference_grid(image, grid):
7373
# Function to overlay two grids by performing subtraction
7474
def decrypt(image1, image2):
7575
"""
76-
Combines two grids by subtracting the second grid from the first.
76+
Combines two grids by adding the second grid to the first.
7777
7878
Parameters:
7979
image1 (PIL.Image.Image): The first image (PIL Image object), to be converted to numpy array and processed.

web_app/algo_interface.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from scripts.visual_cryptography import vc_grayscale_halftone, vc_color_cmyk
2-
from scripts.random_grid import rg_grayscale_halftone, rg_grayscale_bitplane, rg_grayscale_additive_SS
2+
from scripts.random_grid import rg_grayscale_halftone, rg_grayscale_bitplane, rg_grayscale_additive_SS, rg_color_additive_SS
33

44
# Using get_config() to retrieve the dictionaries with function mappings
55
ALGORITHM_MODULES = {
6-
"vc_grayscale_halftone": vc_grayscale_halftone.get_config(),
7-
"vc_color_cmyk": vc_color_cmyk.get_config(),
8-
6+
"rg_color_additive_SS": rg_color_additive_SS.get_config(),
97
"rg_grayscale_additive_SS": rg_grayscale_additive_SS.get_config(),
108
"rg_grayscale_halftone": rg_grayscale_halftone.get_config(),
119
"rg_grayscale_bitplane": rg_grayscale_bitplane.get_config(),
10+
11+
"vc_grayscale_halftone": vc_grayscale_halftone.get_config(),
12+
"vc_color_cmyk": vc_color_cmyk.get_config(),
1213
}

0 commit comments

Comments
 (0)