Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from Crypto.Cipher import AES
|
| 3 |
+
from Crypto.Random import get_random_bytes
|
| 4 |
+
import base64
|
| 5 |
+
import io
|
| 6 |
+
|
| 7 |
+
def pad(data):
|
| 8 |
+
pad_len = AES.block_size - len(data) % AES.block_size
|
| 9 |
+
return data + bytes([pad_len] * pad_len)
|
| 10 |
+
|
| 11 |
+
def unpad(data):
|
| 12 |
+
return data[:-data[-1]]
|
| 13 |
+
|
| 14 |
+
def encrypt_image(image, password):
|
| 15 |
+
key = password.encode('utf-8').ljust(32, b'\0')[:32]
|
| 16 |
+
img_bytes = image.read()
|
| 17 |
+
img_padded = pad(img_bytes)
|
| 18 |
+
|
| 19 |
+
cipher = AES.new(key, AES.MODE_CBC)
|
| 20 |
+
ciphertext = cipher.encrypt(img_padded)
|
| 21 |
+
|
| 22 |
+
encrypted_data = base64.b64encode(cipher.iv + ciphertext).decode('utf-8')
|
| 23 |
+
return encrypted_data
|
| 24 |
+
|
| 25 |
+
def decrypt_image(encrypted_text, password):
|
| 26 |
+
try:
|
| 27 |
+
key = password.encode('utf-8').ljust(32, b'\0')[:32]
|
| 28 |
+
encrypted_data = base64.b64decode(encrypted_text)
|
| 29 |
+
iv = encrypted_data[:16]
|
| 30 |
+
ciphertext = encrypted_data[16:]
|
| 31 |
+
|
| 32 |
+
cipher = AES.new(key, AES.MODE_CBC, iv=iv)
|
| 33 |
+
decrypted = unpad(cipher.decrypt(ciphertext))
|
| 34 |
+
|
| 35 |
+
return decrypted
|
| 36 |
+
except Exception as e:
|
| 37 |
+
return f"Decryption failed: {str(e)}"
|
| 38 |
+
|
| 39 |
+
def encrypt_ui(image, password):
|
| 40 |
+
if image is None or password.strip() == "":
|
| 41 |
+
return "Please upload image and enter a password."
|
| 42 |
+
return encrypt_image(image, password)
|
| 43 |
+
|
| 44 |
+
def decrypt_ui(encrypted_text, password):
|
| 45 |
+
result = decrypt_image(encrypted_text, password)
|
| 46 |
+
if isinstance(result, bytes):
|
| 47 |
+
return gr.Image.update(value=io.BytesIO(result))
|
| 48 |
+
return result
|
| 49 |
+
|
| 50 |
+
footer_note = "๐ Made by Kratu Pandey, B.Tech CSE"
|
| 51 |
+
|
| 52 |
+
encrypt_tab = gr.Interface(
|
| 53 |
+
fn=encrypt_ui,
|
| 54 |
+
inputs=[
|
| 55 |
+
gr.File(label="Upload Image"),
|
| 56 |
+
gr.Text(label="Password", type="password")
|
| 57 |
+
],
|
| 58 |
+
outputs="text",
|
| 59 |
+
title="๐ Encrypt Image",
|
| 60 |
+
description=footer_note
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
decrypt_tab = gr.Interface(
|
| 64 |
+
fn=decrypt_ui,
|
| 65 |
+
inputs=[
|
| 66 |
+
gr.Text(label="Encrypted Text"),
|
| 67 |
+
gr.Text(label="Password", type="password")
|
| 68 |
+
],
|
| 69 |
+
outputs="image",
|
| 70 |
+
title="๐ Decrypt Image",
|
| 71 |
+
description=footer_note
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
gr.TabbedInterface(
|
| 75 |
+
[encrypt_tab, decrypt_tab],
|
| 76 |
+
tab_names=["Encrypt Image", "Decrypt Image"]
|
| 77 |
+
).launch()
|