Skip to content

Commit d524476

Browse files
committed
Move to Cryptographically Secure RNG
Ensured all randomness used in encryption follows best security practices.
1 parent e93417d commit d524476

File tree

6 files changed

+22
-33
lines changed

6 files changed

+22
-33
lines changed

.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ Temporary Items
3737
### PyCharm ###
3838
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
3939
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
40-
41-
# User-specific stuff
4240
.idea/
4341

4442
# CMake
@@ -292,7 +290,7 @@ pyrightconfig.json
292290
# Ignore all files except .gitkeep
293291
# The .gitkeep file is used to ensure the empty folder is committed to the repository.
294292
scripts/images/output/*
295-
!script/images/output/.gitkeep
293+
!scripts/images/output/.gitkeep
296294

297295
web_app/static/output/*
298296
!web_app/static/output/.gitkeep

README.md

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,21 @@
44
</a>
55
</p>
66

7-
<h1 align="center">VisualCrypto</h1>
87

9-
<p align="center">A framework for image-based secret sharing</p>
108

11-
<p align="center">
12-
<!-- Python Version -->
13-
<a href="https://www.python.org/downloads/">
14-
<img src="https://img.shields.io/badge/Python-3.8%2B-blue" alt="Python 3.8+" />
15-
</a>
9+
<div align="center">
1610

17-
<!-- API Documentation -->
18-
<a href="https://coduri.github.io/VisualCrypto/">
19-
<img src="https://img.shields.io/badge/Documentation-Available-green" alt="Documentation Available" />
20-
</a>
11+
[![Python 3.8+](https://img.shields.io/badge/Python-3.8%2B-blue)](https://www.python.org/downloads/)
12+
[![Cryptographic Secure](https://img.shields.io/badge/Cryptographic%20Secure-secrets.py-blueviolet)](https://docs.python.org/3/library/secrets.html)
13+
[![Documentation Available](https://img.shields.io/badge/Documentation-Available-green)](https://coduri.github.io/VisualCrypto/)
14+
[![Contributions Welcome](https://img.shields.io/badge/Contributions-Welcome-orange)](https://coduri.github.io/VisualCrypto/pages/contributing/)
15+
[![License](https://img.shields.io/badge/License-MIT-red)](https://github.com/coduri/VisualCrypto/blob/main/LICENSE.txt)
16+
17+
18+
</div>
2119

22-
<!-- Contributions -->
23-
<a href="https://coduri.github.io/VisualCrypto/pages/contributing/">
24-
<img src="https://img.shields.io/badge/Contributions-Welcome-orange" alt="Contributions Welcome" />
25-
</a>
2620

27-
<!-- License -->
28-
<a href="https://github.com/coduri/VisualCrypto/LICENSE.txt">
29-
<img src="https://img.shields.io/badge/License-MIT-red" alt="License" />
30-
</a>
3121

32-
</p>
3322

3423
---
3524

scripts/images/output/.gitkeep

Whitespace-only changes.

scripts/random_grid/rg_grayscale_additive_SS.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import numpy as np
22
from PIL import Image
3+
import secrets
34

45

56
# Returns a dictionary containing function mappings and metadata for the algorithm (used by the web interface).
@@ -51,7 +52,7 @@ def create_random_grid(size):
5152
Returns:
5253
numpy.ndarray: A grid filled with random integer values between 0 and 255.
5354
"""
54-
grid = np.random.randint(0, 256, size=size)
55+
grid = np.array([[secrets.randbelow(256) for _ in range(size[1])] for _ in range(size[0])], dtype=np.uint8)
5556
return grid
5657

5758

@@ -103,13 +104,13 @@ def encrypt(image):
103104
"""
104105
img_array = np.array(image).astype(int) # Convert PIL Image to numpy array and convert to int type
105106

106-
# Create the first random grid
107+
# Create the first random grid and convert it to PIL Image
107108
grid1 = create_random_grid(img_array.shape)
108-
grid1_image = Image.fromarray(grid1.astype(np.uint8)) # Convert grid to PIL Image
109+
grid1_image = Image.fromarray(grid1)
109110

110-
# Create the second grid (difference grid)
111+
# Create the second random grid and convert it to PIL Image
111112
grid2 = create_difference_grid(img_array, grid1)
112-
grid2_image = Image.fromarray(grid2.astype(np.uint8)) # Convert grid to PIL Image
113+
grid2_image = Image.fromarray(grid2.astype(np.uint8)) # np.uint8 causes wrapping (modulo 256) for negative values
113114

114115
return grid1_image, grid2_image
115116

scripts/random_grid/rg_grayscale_halftone.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from PIL import Image
22
import numpy as np
3+
import secrets
34

45

56
# Returns a dictionary containing function mappings and metadata for the algorithm (used by the web interface).
@@ -62,7 +63,7 @@ def create_first_random_grid(size):
6263
Returns:
6364
numpy.ndarray: A random binary grid (0s and 1s) of the specified size.
6465
"""
65-
grid = np.random.randint(2, size=size)
66+
grid = np.array([[secrets.randbelow(2) for _ in range(size[1])] for _ in range(size[0])])
6667
return grid
6768

6869

scripts/visual_cryptography/vc_grayscale_halftone.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from PIL import Image
2-
import random
32
import copy
4-
3+
import secrets
54

65
# Returns a dictionary containing function mappings and metadata for the algorithm (used by the web interface).
76
def get_config():
@@ -98,9 +97,10 @@ def random_col_permutation(matrix):
9897
n_cols = len(matrix[0])
9998
new_matrix = copy.deepcopy(matrix)
10099

101-
# Generate a random permutation of column indices
100+
# Generate a random permutation of column indices.
101+
# Note that: secrets.SystemRandom is a class that uses the system's cryptographic random number generator
102102
col_indices = list(range(n_cols))
103-
random.shuffle(col_indices)
103+
secrets.SystemRandom().shuffle(col_indices)
104104

105105
# Apply the column permutation to the matrix
106106
for i in range(n_rows):

0 commit comments

Comments
 (0)