Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
-
import tkinter as tk
|
| 3 |
-
from tkinter import filedialog
|
| 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 gradio as gr
|
| 8 |
|
| 9 |
class CBCEncryption:
|
| 10 |
def __init__(self, key, iv):
|
|
@@ -24,28 +22,28 @@ class CBCEncryption:
|
|
| 24 |
def finalize_decrypt(self):
|
| 25 |
return self.decryptor.finalize()
|
| 26 |
|
| 27 |
-
def EncryptImage(encryption,
|
| 28 |
-
image = Image.open(
|
| 29 |
image.save('temp.bmp')
|
| 30 |
with open('temp.bmp', 'rb') as reader:
|
| 31 |
-
with open(
|
| 32 |
image_data = reader.read()
|
| 33 |
header, body = image_data[:54], image_data[54:]
|
| 34 |
body += b'\x35' * (16 - (len(body) % 16))
|
| 35 |
body = encryption.encrypt(body) + encryption.finalize_encrypt()
|
| 36 |
writer.write(header + body)
|
| 37 |
os.remove('temp.bmp')
|
|
|
|
| 38 |
|
| 39 |
-
def DecryptImage(decryption,
|
| 40 |
-
with open(
|
| 41 |
-
with open(
|
| 42 |
image_data = reader.read()
|
| 43 |
header, body = image_data[:54], image_data[54:]
|
| 44 |
body = decryption.decrypt(body)
|
| 45 |
unpadded_body = unpad_data(body)
|
| 46 |
writer.write(header + unpadded_body)
|
| 47 |
-
|
| 48 |
-
decrypted_image.show()
|
| 49 |
|
| 50 |
def unpad_data(data):
|
| 51 |
padding_byte = data[-1]
|
|
@@ -54,14 +52,49 @@ def unpad_data(data):
|
|
| 54 |
return unpadded_data
|
| 55 |
|
| 56 |
def main():
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
)
|
| 63 |
|
| 64 |
-
|
| 65 |
|
| 66 |
if __name__ == '__main__':
|
| 67 |
-
main()
|
|
|
|
| 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 |
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()
|
| 31 |
header, body = image_data[:54], image_data[54:]
|
| 32 |
body += b'\x35' * (16 - (len(body) % 16))
|
| 33 |
body = encryption.encrypt(body) + encryption.finalize_encrypt()
|
| 34 |
writer.write(header + body)
|
| 35 |
os.remove('temp.bmp')
|
| 36 |
+
return 'encrypted.bmp'
|
| 37 |
|
| 38 |
+
def DecryptImage(decryption, image, key):
|
| 39 |
+
with open(image, 'rb') as reader:
|
| 40 |
+
with open('decrypted.bmp', 'wb') as writer:
|
| 41 |
image_data = reader.read()
|
| 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):
|
| 49 |
padding_byte = data[-1]
|
|
|
|
| 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')
|
| 59 |
+
key = key.ljust(32, b'\x35')
|
| 60 |
+
|
| 61 |
+
iv = key[:16]
|
| 62 |
+
iv = bytearray(iv)
|
| 63 |
+
for i in range(16):
|
| 64 |
+
iv[i] = iv[i] ^ 0x35
|
| 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()
|
| 98 |
|
| 99 |
if __name__ == '__main__':
|
| 100 |
+
main()
|