-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorithm.py
68 lines (46 loc) · 1.92 KB
/
algorithm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import pillow_avif
import pydicom as dcmread
from util.key_gen import generate_round_keys
from util.padding import insertar_datos_aleatorios, quitar_datos_aleatorios
from util.histo import mostrar_histogramas
from util.v1.cypher import encrypt_image, decrypt_image
#TODO: cambiar flujo: introducir texto, generar clave, cifrar, guardar imagen cifrada y luego introducir clave para descifrar
def read_dicom(path):
data = dcmread.dcmread(path)
image = data.pixel_array
if image.max() > 255:
print(f"El valor máximo es: {image.max()}")
image = (image / image.max() * 255).astype(np.uint8)
return image
if __name__ == "__main__":
image = Image.open('./images/fali.jpg').convert('L')
image = np.array(image)
image = read_dicom('./images/I1000000')
(x1, r1), (x2, r2) = generate_round_keys()
key1 = (x1, r1)
key2 = (x2, r2)
imagen_expandida = insertar_datos_aleatorios(image)
encrypted_image_1, encryption_data_1 = encrypt_image(imagen_expandida, key1)
encrypted_image_2, encryption_data_2 = encrypt_image(encrypted_image_1, key2)
decrypted_image_1 = decrypt_image(encrypted_image_2, key2, encryption_data_2)
decrypted_image_2 = decrypt_image(decrypted_image_1, key1, encryption_data_1)
decrypted_image_no_padding = quitar_datos_aleatorios(decrypted_image_2)
plt.figure(figsize=(15, 5))
plt.subplot(1, 3, 1)
plt.imshow(image, cmap='gray')
plt.title('Imagen Original')
plt.axis('off')
plt.subplot(1, 3, 2)
plt.imshow(encrypted_image_2, cmap='gray')
plt.title('Imagen Cifrada (2 rondas)')
plt.axis('off')
plt.subplot(1, 3, 3)
plt.imshow(decrypted_image_no_padding, cmap='gray')
plt.title('Imagen Descifrada')
plt.axis('off')
plt.tight_layout()
plt.show()
mostrar_histogramas(image, encrypted_image_2, decrypted_image_no_padding)