Spaces:
Runtime error
Runtime error
Godfrey123
commited on
Commit
•
0211a19
1
Parent(s):
eae09cd
Update app.py
Browse files
app.py
CHANGED
@@ -24,76 +24,17 @@ def decrypt_image(decryption, image_path, output_path):
|
|
24 |
def unpad_data(data):
|
25 |
# Your unpad_data function code here...
|
26 |
|
27 |
-
def main():
|
28 |
-
# Your main function code here...
|
29 |
-
|
30 |
-
import subprocess
|
31 |
-
subprocess.run(["pip", "install", "altair==4.1.0"])
|
32 |
-
import os
|
33 |
-
import streamlit as st
|
34 |
-
from PIL import Image
|
35 |
-
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
36 |
-
from cryptography.hazmat.backends import default_backend
|
37 |
-
|
38 |
-
class CBCEncryption:
|
39 |
-
def __init__(self, key, iv):
|
40 |
-
self.cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
|
41 |
-
self.encryptor = self.cipher.encryptor()
|
42 |
-
self.decryptor = self.cipher.decryptor()
|
43 |
-
|
44 |
-
def encrypt(self, image):
|
45 |
-
return self.encryptor.update(image)
|
46 |
-
|
47 |
-
def decrypt(self, image):
|
48 |
-
return self.decryptor.update(image)
|
49 |
-
|
50 |
-
def finalize_encrypt(self):
|
51 |
-
return self.encryptor.finalize()
|
52 |
-
|
53 |
-
def finalize_decrypt(self):
|
54 |
-
return self.decryptor.finalize()
|
55 |
-
|
56 |
-
def encrypt_image(encryption, image_path, output_path):
|
57 |
-
image = Image.open(image_path)
|
58 |
-
image.save('temp.bmp')
|
59 |
-
with open('temp.bmp', 'rb') as reader:
|
60 |
-
with open(output_path, 'wb') as writer:
|
61 |
-
image_data = reader.read()
|
62 |
-
header, body = image_data[:54], image_data[54:]
|
63 |
-
body += b'\x35' * (16 - (len(body) % 16))
|
64 |
-
body = encryption.encrypt(body) + encryption.finalize_encrypt()
|
65 |
-
writer.write(header + body)
|
66 |
-
os.remove('temp.bmp')
|
67 |
-
|
68 |
-
def decrypt_image(decryption, image_path, output_path):
|
69 |
-
with open(image_path, 'rb') as reader:
|
70 |
-
with open(output_path, 'wb') as writer:
|
71 |
-
image_data = reader.read()
|
72 |
-
header, body = image_data[:54], image_data[54:]
|
73 |
-
body = decryption.decrypt(body)
|
74 |
-
unpadded_body = unpad_data(body)
|
75 |
-
writer.write(header + unpadded_body)
|
76 |
-
decrypted_image = Image.open(output_path)
|
77 |
-
st.image(decrypted_image, caption="Decrypted Image")
|
78 |
-
|
79 |
-
def unpad_data(data):
|
80 |
-
padding_byte = data[-1]
|
81 |
-
padding_length = padding_byte if padding_byte < 16 else 16
|
82 |
-
unpadded_data = data[:-padding_length]
|
83 |
-
return unpadded_data
|
84 |
-
|
85 |
def main():
|
86 |
st.title("AES CBC Encryption/Decryption")
|
87 |
|
88 |
uploaded_file = st.file_uploader("Choose an image...", type=["bmp", "png", "jpg", "jpeg"])
|
89 |
key = st.text_input("Enter Key:", type="password")
|
90 |
-
action = st.radio("Choose Action:", ("Encrypt", "Decrypt"))
|
91 |
|
92 |
if uploaded_file is not None and key:
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
key = key.encode('utf-8')
|
98 |
key = key.ljust(32, b'\x35')
|
99 |
iv = key[:16]
|
@@ -110,16 +51,17 @@ def main():
|
|
110 |
with open(image_path, 'wb') as f:
|
111 |
f.write(uploaded_file.getbuffer())
|
112 |
|
113 |
-
if
|
114 |
encrypt_image(encryption=AesCbc, image_path=image_path, output_path=output_path)
|
115 |
st.success("Encryption done!")
|
116 |
with open(output_path, 'rb') as f:
|
117 |
st.download_button(label="Download Encrypted Image", data=f, file_name="encrypted_image.bmp")
|
118 |
|
119 |
-
elif
|
120 |
decrypt_image(decryption=AesCbc, image_path=image_path, output_path=output_path)
|
121 |
st.success("Decryption done!")
|
|
|
|
|
122 |
|
123 |
if __name__ == '__main__':
|
124 |
main()
|
125 |
-
|
|
|
24 |
def unpad_data(data):
|
25 |
# Your unpad_data function code here...
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
def main():
|
28 |
st.title("AES CBC Encryption/Decryption")
|
29 |
|
30 |
uploaded_file = st.file_uploader("Choose an image...", type=["bmp", "png", "jpg", "jpeg"])
|
31 |
key = st.text_input("Enter Key:", type="password")
|
|
|
32 |
|
33 |
if uploaded_file is not None and key:
|
34 |
+
st.write("") # Add space between encryption and decryption options
|
35 |
+
encrypt_button = st.button("Encrypt Image")
|
36 |
+
decrypt_button = st.button("Decrypt Image")
|
37 |
+
|
38 |
key = key.encode('utf-8')
|
39 |
key = key.ljust(32, b'\x35')
|
40 |
iv = key[:16]
|
|
|
51 |
with open(image_path, 'wb') as f:
|
52 |
f.write(uploaded_file.getbuffer())
|
53 |
|
54 |
+
if encrypt_button:
|
55 |
encrypt_image(encryption=AesCbc, image_path=image_path, output_path=output_path)
|
56 |
st.success("Encryption done!")
|
57 |
with open(output_path, 'rb') as f:
|
58 |
st.download_button(label="Download Encrypted Image", data=f, file_name="encrypted_image.bmp")
|
59 |
|
60 |
+
elif decrypt_button:
|
61 |
decrypt_image(decryption=AesCbc, image_path=image_path, output_path=output_path)
|
62 |
st.success("Decryption done!")
|
63 |
+
decrypted_image = Image.open(output_path)
|
64 |
+
st.image(decrypted_image, caption="Decrypted Image")
|
65 |
|
66 |
if __name__ == '__main__':
|
67 |
main()
|
|