lyimo commited on
Commit
71b03a4
·
verified ·
1 Parent(s): c40fa0b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -3
app.py CHANGED
@@ -1,8 +1,10 @@
1
  import gradio as gr
2
  import os
 
3
  from PIL import Image
4
  from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
5
  from cryptography.hazmat.backends import default_backend
 
6
 
7
  class CBCEncryption:
8
  def __init__(self, key, iv):
@@ -22,9 +24,22 @@ class CBCEncryption:
22
  def finalize_decrypt(self):
23
  return self.decryptor.finalize()
24
 
 
 
 
 
 
 
 
 
 
 
25
  def EncryptImage(encryption, image, key):
26
  image = Image.open(image)
27
- image.save('temp.bmp')
 
 
 
28
  with open('temp.bmp', 'rb') as reader:
29
  with open('encrypted.bmp', 'wb') as writer:
30
  image_data = reader.read()
@@ -42,7 +57,10 @@ def DecryptImage(decryption, image, key):
42
  header, body = image_data[:54], image_data[54:]
43
  body = decryption.decrypt(body)
44
  unpadded_body = unpad_data(body)
45
- writer.write(header + unpadded_body)
 
 
 
46
  return 'decrypted.bmp'
47
 
48
  def unpad_data(data):
@@ -78,7 +96,7 @@ def main():
78
  action, # Single function to wrap
79
  ["image", "text", "text"], # Input types: image, key, action_type
80
  "image", # Output type
81
- title="AES CBC Encryption/Decryption",
82
  description="Upload an image, a key, and choose Encrypt or Decrypt to process the image."
83
  )
84
 
 
1
  import gradio as gr
2
  import os
3
+ import numpy as np
4
  from PIL import Image
5
  from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
6
  from cryptography.hazmat.backends import default_backend
7
+ import cv2
8
 
9
  class CBCEncryption:
10
  def __init__(self, key, iv):
 
24
  def finalize_decrypt(self):
25
  return self.decryptor.finalize()
26
 
27
+ def add_gaussian_noise(image, mean=0, var=0.01):
28
+ noise = np.random.normal(mean, var**0.5, image.shape)
29
+ noisy_image = image + noise
30
+ return noisy_image
31
+
32
+ def remove_gaussian_noise(image, mean=0, var=0.01):
33
+ # This is a simplified approach to remove noise.
34
+ # In practice, you might need a more sophisticated method to estimate the noise statistics.
35
+ return image - np.random.normal(mean, var**0.5, image.shape)
36
+
37
  def EncryptImage(encryption, image, key):
38
  image = Image.open(image)
39
+ image_np = np.array(image)
40
+ noisy_image_np = add_gaussian_noise(image_np)
41
+ noisy_image = Image.fromarray(noisy_image_np.astype(np.uint8))
42
+ noisy_image.save('temp.bmp')
43
  with open('temp.bmp', 'rb') as reader:
44
  with open('encrypted.bmp', 'wb') as writer:
45
  image_data = reader.read()
 
57
  header, body = image_data[:54], image_data[54:]
58
  body = decryption.decrypt(body)
59
  unpadded_body = unpad_data(body)
60
+ decrypted_image_np = np.frombuffer(unpadded_body, dtype=np.uint8).reshape(image.shape)
61
+ original_image_np = remove_gaussian_noise(decrypted_image_np)
62
+ original_image = Image.fromarray(original_image_np.astype(np.uint8))
63
+ original_image.save('decrypted.bmp')
64
  return 'decrypted.bmp'
65
 
66
  def unpad_data(data):
 
96
  action, # Single function to wrap
97
  ["image", "text", "text"], # Input types: image, key, action_type
98
  "image", # Output type
99
+ title="AES CBC Encryption/Decryption with Gaussian Noise",
100
  description="Upload an image, a key, and choose Encrypt or Decrypt to process the image."
101
  )
102