Spaces:
Sleeping
Sleeping
Add image encryption and decryption functionality
Browse files- app.py +118 -0
- image1.png +0 -0
- requirements.txt +6 -0
- styles.css +97 -0
app.py
ADDED
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from image_cipher import ImageCipher
|
3 |
+
from PIL import Image
|
4 |
+
import tempfile
|
5 |
+
import os
|
6 |
+
|
7 |
+
|
8 |
+
def save_uploaded_file_to_temp(uploaded_file):
|
9 |
+
temp_dir = tempfile.mkdtemp()
|
10 |
+
temp_file_path = os.path.join(temp_dir, uploaded_file.name)
|
11 |
+
with open(temp_file_path, "wb") as f:
|
12 |
+
f.write(uploaded_file.getbuffer())
|
13 |
+
return temp_file_path
|
14 |
+
|
15 |
+
|
16 |
+
def convert_to_png(image_path):
|
17 |
+
img = Image.open(image_path)
|
18 |
+
png_image_path = f"{os.path.splitext(image_path)[0]}.png"
|
19 |
+
img.save(png_image_path, "PNG")
|
20 |
+
return png_image_path
|
21 |
+
|
22 |
+
|
23 |
+
def encrypt_image(image_path, message, encrypt):
|
24 |
+
cipher = ImageCipher()
|
25 |
+
encoded_image_path = cipher.encode(image_path, message, encrypt=encrypt)
|
26 |
+
return encoded_image_path, cipher.key
|
27 |
+
|
28 |
+
|
29 |
+
def decrypt_image(image_path, key):
|
30 |
+
cipher = ImageCipher()
|
31 |
+
decoded_message = cipher.decode(image_path, key)
|
32 |
+
return decoded_message
|
33 |
+
|
34 |
+
|
35 |
+
def main():
|
36 |
+
st.sidebar.image("image1.png", use_column_width=True)
|
37 |
+
with open("styles.css") as f:
|
38 |
+
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
|
39 |
+
st.sidebar.markdown(
|
40 |
+
"""<a href='https://github.com/samadpls/ImageCipher'>\
|
41 |
+
<img src='https://img.shields.io/github/stars/samadpls/ImageCipher?\
|
42 |
+
color=red&label=star%20me&logoColor=red&style=social'></a>""",
|
43 |
+
unsafe_allow_html=True,
|
44 |
+
)
|
45 |
+
st.title("Image Cipher")
|
46 |
+
app_mode = st.sidebar.radio(
|
47 |
+
"Choose the app mode", ["Encrypt", "Decrypt", "About Us"]
|
48 |
+
)
|
49 |
+
|
50 |
+
if app_mode == "Encrypt":
|
51 |
+
st.header("Encrypt Image")
|
52 |
+
uploaded_image = st.file_uploader(
|
53 |
+
"Choose an image...", type=["png"]
|
54 |
+
)
|
55 |
+
message = st.text_area("Enter your message")
|
56 |
+
use_encryption = st.checkbox("Use Encryption")
|
57 |
+
|
58 |
+
if st.button("Encrypt"):
|
59 |
+
if uploaded_image and message:
|
60 |
+
temp_image_path = save_uploaded_file_to_temp(uploaded_image)
|
61 |
+
if uploaded_image.type in ["image/jpeg", "image/jpg"]:
|
62 |
+
temp_image_path = convert_to_png(temp_image_path)
|
63 |
+
encoded_image_path, key = encrypt_image(
|
64 |
+
temp_image_path, message, use_encryption
|
65 |
+
)
|
66 |
+
if use_encryption:
|
67 |
+
st.markdown("### Encryption Key:")
|
68 |
+
st.markdown(f"```\n{key.decode()}\n```")
|
69 |
+
with open(encoded_image_path, "rb") as file:
|
70 |
+
st.download_button(
|
71 |
+
"Download Encrypted Image",
|
72 |
+
file,
|
73 |
+
file_name="encrypted_image.png",
|
74 |
+
)
|
75 |
+
st.image(encoded_image_path, caption="Encrypted Image")
|
76 |
+
else:
|
77 |
+
st.error(
|
78 |
+
"Please upload an image and enter a message\
|
79 |
+
to encrypt."
|
80 |
+
)
|
81 |
+
|
82 |
+
elif app_mode == "Decrypt":
|
83 |
+
st.header("Decrypt Image")
|
84 |
+
uploaded_image = st.file_uploader(
|
85 |
+
"Choose an image...", type=["png"]
|
86 |
+
)
|
87 |
+
key = st.text_input("Enter key (if any)")
|
88 |
+
|
89 |
+
if st.button("Decrypt"):
|
90 |
+
try:
|
91 |
+
if uploaded_image:
|
92 |
+
temp_image_path = save_uploaded_file_to_temp(uploaded_image)
|
93 |
+
if uploaded_image.type in ["image/jpeg", "image/jpg"]:
|
94 |
+
temp_image_path = convert_to_png(temp_image_path)
|
95 |
+
decrypted_message = decrypt_image(temp_image_path, key)
|
96 |
+
st.text_area(
|
97 |
+
"Decrypted Message",
|
98 |
+
value=decrypted_message,
|
99 |
+
height=200
|
100 |
+
)
|
101 |
+
else:
|
102 |
+
st.error("Please upload an image to decrypt.")
|
103 |
+
except Exception as e:
|
104 |
+
st.error(e)
|
105 |
+
|
106 |
+
elif app_mode == "About Us":
|
107 |
+
st.header("About Us")
|
108 |
+
st.markdown(
|
109 |
+
"This project is developed by <ul> <li>(Abdul Samad)\
|
110 |
+
[https://github.com/samadpls/] </li>\
|
111 |
+
<li> [Maira Usman](https://github.com/Myrausman/)</li> \
|
112 |
+
</ul>",
|
113 |
+
unsafe_allow_html=True,
|
114 |
+
)
|
115 |
+
|
116 |
+
|
117 |
+
if __name__ == "__main__":
|
118 |
+
main()
|
image1.png
ADDED
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Pillow
|
2 |
+
cryptography
|
3 |
+
pytest
|
4 |
+
flake8
|
5 |
+
streamlit
|
6 |
+
imagecipher
|
styles.css
ADDED
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/* Main container */
|
2 |
+
body {
|
3 |
+
background-color: #181d27;
|
4 |
+
color: #ffbf00;
|
5 |
+
}
|
6 |
+
|
7 |
+
/* Sidebar */
|
8 |
+
.css-1d391kg {
|
9 |
+
background-color: #4f535a;
|
10 |
+
}
|
11 |
+
|
12 |
+
.css-1lcbmhc, .css-1d2azem {
|
13 |
+
color: #ffbf00;
|
14 |
+
}
|
15 |
+
|
16 |
+
.css-1v8qudd {
|
17 |
+
background-color: #2c374d;
|
18 |
+
color: #ffbf00;
|
19 |
+
}
|
20 |
+
|
21 |
+
/* Header */
|
22 |
+
.css-1siy2j7 {
|
23 |
+
color: #ffbf00;
|
24 |
+
}
|
25 |
+
|
26 |
+
/* Radio buttons and text input */
|
27 |
+
.css-1ex6qaa {
|
28 |
+
background-color: #2c374d;
|
29 |
+
color: #ffbf00;
|
30 |
+
}
|
31 |
+
|
32 |
+
.css-1ex6qaa input[type="radio"] {
|
33 |
+
background-color: #2c374d;
|
34 |
+
color: #ffbf00;
|
35 |
+
}
|
36 |
+
|
37 |
+
.css-1ex6qaa input[type="text"] {
|
38 |
+
background-color: #2c374d;
|
39 |
+
color: #ffbf00;
|
40 |
+
}
|
41 |
+
|
42 |
+
.css-1ex6qaa textarea {
|
43 |
+
background-color: #2c374d;
|
44 |
+
color: #ffbf00;
|
45 |
+
}
|
46 |
+
|
47 |
+
/* Buttons */
|
48 |
+
.css-1df45cr, .css-1f1iwnp, .css-1ktajzc {
|
49 |
+
background-color: #152544;
|
50 |
+
color: #ffbf00;
|
51 |
+
}
|
52 |
+
|
53 |
+
.css-1df45cr:hover, .css-1f1iwnp:hover, .css-1ktajzc:hover {
|
54 |
+
background-color: #152544;
|
55 |
+
color: #ffffff;
|
56 |
+
}
|
57 |
+
|
58 |
+
/* Download button */
|
59 |
+
.css-10trblm {
|
60 |
+
background-color: #152544;
|
61 |
+
color: #ffbf00;
|
62 |
+
}
|
63 |
+
|
64 |
+
.css-10trblm:hover {
|
65 |
+
background-color: #152544;
|
66 |
+
color: #ffffff;
|
67 |
+
}
|
68 |
+
|
69 |
+
/* Image */
|
70 |
+
.css-11v1u3b img {
|
71 |
+
border: 2px solid #ffbf00;
|
72 |
+
}
|
73 |
+
|
74 |
+
/* Text */
|
75 |
+
.css-qrbaxs p {
|
76 |
+
color: #ffbf00;
|
77 |
+
}
|
78 |
+
|
79 |
+
/* Error messages */
|
80 |
+
.css-19hj99j {
|
81 |
+
background-color: #2c374d;
|
82 |
+
color: #ffbf00;
|
83 |
+
}
|
84 |
+
|
85 |
+
/* Markdown */
|
86 |
+
.css-1uix8eu h1, .css-1uix8eu h2, .css-1uix8eu h3, .css-1uix8eu h4, .css-1uix8eu h5, .css-1uix8eu h6, .css-1uix8eu p {
|
87 |
+
color: #ffbf00;
|
88 |
+
}
|
89 |
+
|
90 |
+
/* Links */
|
91 |
+
a {
|
92 |
+
color: #ffbf00;
|
93 |
+
}
|
94 |
+
|
95 |
+
a:hover {
|
96 |
+
color: #ffffff;
|
97 |
+
}
|