Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import base64
|
3 |
+
from Crypto.Cipher import AES
|
4 |
+
from Crypto.Util.Padding import pad, unpad
|
5 |
+
from Crypto.Random import get_random_bytes
|
6 |
+
import hashlib
|
7 |
+
|
8 |
+
st.title("🔒 Text Encryptor/Decryptor")
|
9 |
+
st.markdown("Secure text encryption using multiple algorithms")
|
10 |
+
|
11 |
+
# Encryption functions
|
12 |
+
def caesar_encrypt(text, shift):
|
13 |
+
result = ""
|
14 |
+
for char in text:
|
15 |
+
if char.isalpha():
|
16 |
+
ascii_offset = 65 if char.isupper() else 97
|
17 |
+
result += chr((ord(char) + shift - ascii_offset) % 26 + ascii_offset)
|
18 |
+
else:
|
19 |
+
result += char
|
20 |
+
return result
|
21 |
+
|
22 |
+
def caesar_decrypt(text, shift):
|
23 |
+
return caesar_encrypt(text, -shift)
|
24 |
+
|
25 |
+
def vigenere_encrypt(text, key):
|
26 |
+
key = key.upper()
|
27 |
+
key_len = len(key)
|
28 |
+
encrypted = []
|
29 |
+
for i, char in enumerate(text):
|
30 |
+
if char.isalpha():
|
31 |
+
offset = 65 if char.isupper() else 97
|
32 |
+
key_char = key[i % key_len]
|
33 |
+
shift = ord(key_char) - 65
|
34 |
+
encrypted_char = chr((ord(char) + shift - offset) % 26 + offset)
|
35 |
+
encrypted.append(encrypted_char)
|
36 |
+
else:
|
37 |
+
encrypted.append(char)
|
38 |
+
return "".join(encrypted)
|
39 |
+
|
40 |
+
def vigenere_decrypt(text, key):
|
41 |
+
key = key.upper()
|
42 |
+
key_len = len(key)
|
43 |
+
decrypted = []
|
44 |
+
for i, char in enumerate(text):
|
45 |
+
if char.isalpha():
|
46 |
+
offset = 65 if char.isupper() else 97
|
47 |
+
key_char = key[i % key_len]
|
48 |
+
shift = ord(key_char) - 65
|
49 |
+
decrypted_char = chr((ord(char) - shift - offset) % 26 + offset)
|
50 |
+
decrypted.append(decrypted_char)
|
51 |
+
else:
|
52 |
+
decrypted.append(char)
|
53 |
+
return "".join(decrypted)
|
54 |
+
|
55 |
+
def aes_encrypt(text, password):
|
56 |
+
salt = get_random_bytes(16)
|
57 |
+
key = hashlib.scrypt(password.encode(), salt=salt, n=16384, r=8, p=1, dklen=32)
|
58 |
+
iv = get_random_bytes(16)
|
59 |
+
cipher = AES.new(key, AES.MODE_CBC, iv)
|
60 |
+
ciphertext = cipher.encrypt(pad(text.encode(), AES.block_size))
|
61 |
+
return base64.b64encode(salt + iv + ciphertext).decode()
|
62 |
+
|
63 |
+
def aes_decrypt(encrypted, password):
|
64 |
+
encrypted = base64.b64decode(encrypted)
|
65 |
+
salt = encrypted[:16]
|
66 |
+
iv = encrypted[16:32]
|
67 |
+
ciphertext = encrypted[32:]
|
68 |
+
key = hashlib.scrypt(password.encode(), salt=salt, n=16384, r=8, p=1, dklen=32)
|
69 |
+
cipher = AES.new(key, AES.MODE_CBC, iv)
|
70 |
+
decrypted = unpad(cipher.decrypt(ciphertext), AES.block_size)
|
71 |
+
return decrypted.decode()
|
72 |
+
|
73 |
+
# Interface
|
74 |
+
operation = st.radio("Select Operation:", ["Encrypt", "Decrypt"])
|
75 |
+
method = st.selectbox("Select Encryption Method:",
|
76 |
+
["Caesar Cipher", "Vigenère Cipher", "AES-256"])
|
77 |
+
|
78 |
+
text = st.text_area("Enter Text:", height=200)
|
79 |
+
password = st.text_input("Enter Key/Password:", type="password")
|
80 |
+
|
81 |
+
if st.button(f"Perform {operation}"):
|
82 |
+
if text and password:
|
83 |
+
try:
|
84 |
+
if method == "Caesar Cipher":
|
85 |
+
if not password.isdigit():
|
86 |
+
raise ValueError("Caesar cipher requires numeric key")
|
87 |
+
shift = int(password) % 26
|
88 |
+
if operation == "Encrypt":
|
89 |
+
result = caesar_encrypt(text, shift)
|
90 |
+
else:
|
91 |
+
result = caesar_decrypt(text, shift)
|
92 |
+
|
93 |
+
elif method == "Vigenère Cipher":
|
94 |
+
if operation == "Encrypt":
|
95 |
+
result = vigenere_encrypt(text, password)
|
96 |
+
else:
|
97 |
+
result = vigenere_decrypt(text, password)
|
98 |
+
|
99 |
+
elif method == "AES-256":
|
100 |
+
if operation == "Encrypt":
|
101 |
+
result = aes_encrypt(text, password)
|
102 |
+
else:
|
103 |
+
result = aes_decrypt(text, password)
|
104 |
+
|
105 |
+
st.success(f"Result:")
|
106 |
+
st.code(result)
|
107 |
+
|
108 |
+
st.download_button(
|
109 |
+
label="Download Result",
|
110 |
+
data=result,
|
111 |
+
file_name="encrypted.txt" if operation == "Encrypt" else "decrypted.txt",
|
112 |
+
mime="text/plain"
|
113 |
+
)
|
114 |
+
|
115 |
+
except Exception as e:
|
116 |
+
st.error(f"Error: {str(e)}")
|
117 |
+
else:
|
118 |
+
st.warning("Please enter both text and key/password")
|
119 |
+
|
120 |
+
st.sidebar.markdown("## Instructions")
|
121 |
+
st.sidebar.markdown("""
|
122 |
+
1. Select encrypt/decrypt operation
|
123 |
+
2. Choose encryption method
|
124 |
+
3. Enter text and key/password
|
125 |
+
4. Click the action button
|
126 |
+
|
127 |
+
**Key Requirements:**
|
128 |
+
- Caesar: Numeric key (1-25)
|
129 |
+
- Vigenère: Text key (letters only)
|
130 |
+
- AES-256: Strong password (any characters)
|
131 |
+
""")
|