my_roject / app.py
Godfrey123's picture
Update app.py
0211a19 verified
import subprocess
import os
import streamlit as st
from PIL import Image
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
# Install altair if not already installed
try:
import altair
except ImportError:
subprocess.run(["pip", "install", "altair==4.1.0"])
import altair # Import again after installation
class CBCEncryption:
# Your CBCEncryption class code here...
def encrypt_image(encryption, image_path, output_path):
# Your encrypt_image function code here...
def decrypt_image(decryption, image_path, output_path):
# Your decrypt_image function code here...
def unpad_data(data):
# Your unpad_data function code here...
def main():
st.title("AES CBC Encryption/Decryption")
uploaded_file = st.file_uploader("Choose an image...", type=["bmp", "png", "jpg", "jpeg"])
key = st.text_input("Enter Key:", type="password")
if uploaded_file is not None and key:
st.write("") # Add space between encryption and decryption options
encrypt_button = st.button("Encrypt Image")
decrypt_button = st.button("Decrypt Image")
key = key.encode('utf-8')
key = key.ljust(32, b'\x35')
iv = key[:16]
iv = bytearray(iv)
for i in range(16):
iv[i] = iv[i] ^ 0x35
iv = bytes(iv)
AesCbc = CBCEncryption(key, iv)
image_path = 'uploaded_image.bmp'
output_path = 'output_image.bmp'
with open(image_path, 'wb') as f:
f.write(uploaded_file.getbuffer())
if encrypt_button:
encrypt_image(encryption=AesCbc, image_path=image_path, output_path=output_path)
st.success("Encryption done!")
with open(output_path, 'rb') as f:
st.download_button(label="Download Encrypted Image", data=f, file_name="encrypted_image.bmp")
elif decrypt_button:
decrypt_image(decryption=AesCbc, image_path=image_path, output_path=output_path)
st.success("Decryption done!")
decrypted_image = Image.open(output_path)
st.image(decrypted_image, caption="Decrypted Image")
if __name__ == '__main__':
main()