Spaces:
Runtime error
Runtime error
Commit
·
acc3e02
1
Parent(s):
66a9572
main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from cryptography.hazmat.primitives import padding
|
| 3 |
+
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
| 4 |
+
from cryptography.hazmat.backends import default_backend
|
| 5 |
+
import base64
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# Generate a random 256-bit AES key (you can use different key sizes if needed)
|
| 9 |
+
key = b'Sixteen byte key'
|
| 10 |
+
|
| 11 |
+
def aes_encrypt(plaintext):
|
| 12 |
+
backend = default_backend()
|
| 13 |
+
|
| 14 |
+
# Generate a random IV (Initialization Vector)
|
| 15 |
+
iv = os.urandom(16)
|
| 16 |
+
|
| 17 |
+
cipher = Cipher(algorithms.AES(key), modes.CFB(iv), backend=backend)
|
| 18 |
+
encryptor = cipher.encryptor()
|
| 19 |
+
|
| 20 |
+
padder = padding.PKCS7(128).padder()
|
| 21 |
+
padded_data = padder.update(plaintext) + padder.finalize()
|
| 22 |
+
|
| 23 |
+
ciphertext = encryptor.update(padded_data) + encryptor.finalize()
|
| 24 |
+
return base64.b64encode(iv + ciphertext)
|
| 25 |
+
|
| 26 |
+
def aes_decrypt(ciphertext):
|
| 27 |
+
backend = default_backend()
|
| 28 |
+
|
| 29 |
+
decoded_ciphertext = base64.b64decode(ciphertext)
|
| 30 |
+
iv = decoded_ciphertext[:16]
|
| 31 |
+
ciphertext = decoded_ciphertext[16:]
|
| 32 |
+
|
| 33 |
+
cipher = Cipher(algorithms.AES(key), modes.CFB(iv), backend=backend)
|
| 34 |
+
decryptor = cipher.decryptor()
|
| 35 |
+
|
| 36 |
+
decrypted_data = decryptor.update(ciphertext) + decryptor.finalize()
|
| 37 |
+
|
| 38 |
+
unpadder = padding.PKCS7(128).unpadder()
|
| 39 |
+
unpadded_data = unpadder.update(decrypted_data) + unpadder.finalize()
|
| 40 |
+
|
| 41 |
+
return unpadded_data
|
| 42 |
+
|
| 43 |
+
def main():
|
| 44 |
+
st.title("AES Encryption and Decryption")
|
| 45 |
+
|
| 46 |
+
operation = st.radio("Select operation:", ("Encrypt", "Decrypt"))
|
| 47 |
+
|
| 48 |
+
if operation == "Encrypt":
|
| 49 |
+
plaintext = st.text_area("Enter plaintext:", "")
|
| 50 |
+
if st.button("Encrypt"):
|
| 51 |
+
encrypted_data = aes_encrypt(plaintext.encode('utf-8'))
|
| 52 |
+
st.success("Encryption successful!")
|
| 53 |
+
st.text("Encrypted data:")
|
| 54 |
+
st.write(encrypted_data.decode('utf-8'))
|
| 55 |
+
else:
|
| 56 |
+
ciphertext = st.text_area("Enter ciphertext:", "")
|
| 57 |
+
if st.button("Decrypt"):
|
| 58 |
+
decrypted_data = aes_decrypt(ciphertext.encode('utf-8'))
|
| 59 |
+
st.success("Decryption successful!")
|
| 60 |
+
st.text("Decrypted data:")
|
| 61 |
+
st.write(decrypted_data.decode('utf-8'))
|
| 62 |
+
|
| 63 |
+
if __name__ == "__main__":
|
| 64 |
+
main()
|