lyimo commited on
Commit
c40fa0b
·
verified ·
1 Parent(s): 03fc4fc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -23
app.py CHANGED
@@ -52,7 +52,7 @@ def unpad_data(data):
52
  return unpadded_data
53
 
54
  def main():
55
- def action_encrypt(image, key):
56
  if len(key) > 32:
57
  return 'Key is too long. Maximum key length is 32 characters.'
58
  key = key.encode('utf-8')
@@ -65,33 +65,21 @@ def main():
65
  iv = bytes(iv)
66
 
67
  AesCbc = CBCEncryption(key, iv)
68
- output_path = EncryptImage(AesCbc, image, key)
69
- return output_path
70
-
71
- def action_decrypt(image, key):
72
- if len(key) > 32:
73
- return 'Key is too long. Maximum key length is 32 characters.'
74
- key = key.encode('utf-8')
75
- key = key.ljust(32, b'\x35')
76
-
77
- iv = key[:16]
78
- iv = bytearray(iv)
79
- for i in range(16):
80
- iv[i] = iv[i] ^ 0x35
81
- iv = bytes(iv)
82
-
83
- AesCbc = CBCEncryption(key, iv)
84
- output_path = DecryptImage(AesCbc, image, key)
85
  return output_path
86
 
87
  # Define the Gradio interface
88
  iface = gr.Interface(
89
- [action_encrypt, action_decrypt], # Functions to wrap
90
- ["image", "text"], # Input types
91
- ["image", "text"], # Output types
92
- ["Encrypt", "Decrypt"], # Button labels
93
  title="AES CBC Encryption/Decryption",
94
- description="Upload an image and a key to encrypt or decrypt it."
95
  )
96
 
97
  iface.launch()
 
52
  return unpadded_data
53
 
54
  def main():
55
+ def action(image, key, action_type):
56
  if len(key) > 32:
57
  return 'Key is too long. Maximum key length is 32 characters.'
58
  key = key.encode('utf-8')
 
65
  iv = bytes(iv)
66
 
67
  AesCbc = CBCEncryption(key, iv)
68
+ if action_type == 'Encrypt':
69
+ output_path = EncryptImage(AesCbc, image, key)
70
+ elif action_type == 'Decrypt':
71
+ output_path = DecryptImage(AesCbc, image, key)
72
+ else:
73
+ return 'Invalid action type. Choose either Encrypt or Decrypt.'
 
 
 
 
 
 
 
 
 
 
 
74
  return output_path
75
 
76
  # Define the Gradio interface
77
  iface = gr.Interface(
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
 
85
  iface.launch()