Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -62,20 +62,46 @@ def encrypt_image(img_array, seed):
|
|
| 62 |
|
| 63 |
return doubly_encrypted_array
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
# Streamlit App
|
| 66 |
def main():
|
| 67 |
st.title("Enhanced Chaotic Logistic Map Image Encryption")
|
| 68 |
-
st.write("Upload an image to encrypt using advanced chaotic logistic map methods.")
|
| 69 |
|
| 70 |
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
| 71 |
if uploaded_file is not None:
|
| 72 |
-
with st.spinner("Processing upload..."):
|
| 73 |
-
# Simulate a loading delay for upload progress
|
| 74 |
-
progress_bar = st.progress(0)
|
| 75 |
-
for percent_complete in range(0, 101, 20):
|
| 76 |
-
time.sleep(0.1) # Simulate progress
|
| 77 |
-
progress_bar.progress(percent_complete)
|
| 78 |
-
|
| 79 |
# Load the uploaded image
|
| 80 |
input_image = Image.open(uploaded_file)
|
| 81 |
|
|
@@ -93,13 +119,6 @@ def main():
|
|
| 93 |
|
| 94 |
if st.button("Encrypt Image"):
|
| 95 |
with st.spinner("Encrypting image..."):
|
| 96 |
-
# Simulate a loading delay for encryption progress
|
| 97 |
-
progress_bar = st.progress(0)
|
| 98 |
-
for percent_complete in range(0, 101, 20):
|
| 99 |
-
time.sleep(0.1) # Simulate progress
|
| 100 |
-
progress_bar.progress(percent_complete)
|
| 101 |
-
|
| 102 |
-
# Encrypt the image
|
| 103 |
encrypted_array = encrypt_image(img_array, key_seed)
|
| 104 |
encrypted_image = Image.fromarray(encrypted_array)
|
| 105 |
|
|
@@ -119,5 +138,13 @@ def main():
|
|
| 119 |
mime="image/png"
|
| 120 |
)
|
| 121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
if __name__ == "__main__":
|
| 123 |
main()
|
|
|
|
| 62 |
|
| 63 |
return doubly_encrypted_array
|
| 64 |
|
| 65 |
+
# Decrypt function
|
| 66 |
+
def decrypt_image(encrypted_array, seed, original_shape):
|
| 67 |
+
h, w, c = original_shape
|
| 68 |
+
flat_image = encrypted_array.flatten()
|
| 69 |
+
|
| 70 |
+
# Generate the second chaotic key
|
| 71 |
+
chaotic_key_2 = generate_key(seed * 1.1, len(flat_image))
|
| 72 |
+
|
| 73 |
+
# Reverse the second XOR encryption
|
| 74 |
+
xor_reversed_flat = [pixel ^ chaotic_key_2[i] for i, pixel in enumerate(flat_image)]
|
| 75 |
+
xor_reversed_array = np.array(xor_reversed_flat, dtype=np.uint8).reshape(h, w, c)
|
| 76 |
+
|
| 77 |
+
# Reverse pixel shuffling
|
| 78 |
+
shuffled_array = xor_reversed_array
|
| 79 |
+
num_pixels = h * w
|
| 80 |
+
flattened = shuffled_array.reshape(-1, c)
|
| 81 |
+
indices = np.arange(num_pixels)
|
| 82 |
+
|
| 83 |
+
random.seed(seed)
|
| 84 |
+
random.shuffle(indices) # Reuse the same shuffle logic
|
| 85 |
+
unshuffled = np.zeros_like(flattened)
|
| 86 |
+
unshuffled[indices] = flattened
|
| 87 |
+
unshuffled_array = unshuffled.reshape(h, w, c)
|
| 88 |
+
|
| 89 |
+
# Generate the first chaotic key
|
| 90 |
+
chaotic_key_1 = generate_key(seed, len(flat_image))
|
| 91 |
+
|
| 92 |
+
# Reverse the first XOR encryption
|
| 93 |
+
decrypted_flat = [pixel ^ chaotic_key_1[i] for i, pixel in enumerate(unshuffled_array.flatten())]
|
| 94 |
+
decrypted_array = np.array(decrypted_flat, dtype=np.uint8).reshape(h, w, c)
|
| 95 |
+
|
| 96 |
+
return decrypted_array
|
| 97 |
+
|
| 98 |
# Streamlit App
|
| 99 |
def main():
|
| 100 |
st.title("Enhanced Chaotic Logistic Map Image Encryption")
|
| 101 |
+
st.write("Upload an image to encrypt and decrypt using advanced chaotic logistic map methods.")
|
| 102 |
|
| 103 |
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
| 104 |
if uploaded_file is not None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
# Load the uploaded image
|
| 106 |
input_image = Image.open(uploaded_file)
|
| 107 |
|
|
|
|
| 119 |
|
| 120 |
if st.button("Encrypt Image"):
|
| 121 |
with st.spinner("Encrypting image..."):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
encrypted_array = encrypt_image(img_array, key_seed)
|
| 123 |
encrypted_image = Image.fromarray(encrypted_array)
|
| 124 |
|
|
|
|
| 138 |
mime="image/png"
|
| 139 |
)
|
| 140 |
|
| 141 |
+
if st.button("Decrypt Image"):
|
| 142 |
+
with st.spinner("Decrypting image..."):
|
| 143 |
+
decrypted_array = decrypt_image(encrypted_array, key_seed, img_array.shape)
|
| 144 |
+
decrypted_image = Image.fromarray(decrypted_array)
|
| 145 |
+
|
| 146 |
+
# Display decrypted image
|
| 147 |
+
st.image(decrypted_image, caption="Decrypted Image", use_container_width=True)
|
| 148 |
+
|
| 149 |
if __name__ == "__main__":
|
| 150 |
main()
|