Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import random
|
| 3 |
+
import string
|
| 4 |
+
|
| 5 |
+
# Define vowels and consonants for character substitution
|
| 6 |
+
VOWELS = "aeiou"
|
| 7 |
+
CONSONANTS = "".join(set(string.ascii_lowercase) - set(VOWELS))
|
| 8 |
+
|
| 9 |
+
# Helper function to generate a random substitution cipher
|
| 10 |
+
def generate_cipher():
|
| 11 |
+
cipher = {}
|
| 12 |
+
shuffled_vowels = random.sample(VOWELS, len(VOWELS))
|
| 13 |
+
shuffled_consonants = random.sample(CONSONANTS, len(CONSONANTS))
|
| 14 |
+
for v, sv in zip(VOWELS, shuffled_vowels):
|
| 15 |
+
cipher[v] = sv
|
| 16 |
+
for c, sc in zip(CONSONANTS, shuffled_consonants):
|
| 17 |
+
cipher[c] = sc
|
| 18 |
+
return cipher
|
| 19 |
+
|
| 20 |
+
# Encrypt text using the substitution cipher
|
| 21 |
+
def encrypt_text(text, cipher):
|
| 22 |
+
encrypted_text = ""
|
| 23 |
+
for char in text.lower():
|
| 24 |
+
if char in cipher:
|
| 25 |
+
encrypted_text += cipher[char]
|
| 26 |
+
else:
|
| 27 |
+
encrypted_text += char # Non-alphabet characters stay the same
|
| 28 |
+
return encrypted_text
|
| 29 |
+
|
| 30 |
+
# Generate a helper phrase for decryption
|
| 31 |
+
def generate_helper_phrase(cipher):
|
| 32 |
+
words = ["hello", "world", "streamlit", "cipher", "decrypt", "help"]
|
| 33 |
+
helper_text = []
|
| 34 |
+
for word in words:
|
| 35 |
+
helper_word = "".join(cipher[char] if char in cipher else char for char in word)
|
| 36 |
+
helper_text.append(helper_word)
|
| 37 |
+
return " ".join(helper_text)
|
| 38 |
+
|
| 39 |
+
# Main Streamlit app
|
| 40 |
+
st.title("Substitution Cipher Encryption/Decryption")
|
| 41 |
+
|
| 42 |
+
# Input text
|
| 43 |
+
input_text = st.text_input("Enter text to encrypt:")
|
| 44 |
+
|
| 45 |
+
# Generate cipher and encrypt text
|
| 46 |
+
if input_text:
|
| 47 |
+
cipher = generate_cipher()
|
| 48 |
+
encrypted_text = encrypt_text(input_text, cipher)
|
| 49 |
+
helper_phrase = generate_helper_phrase(cipher)
|
| 50 |
+
|
| 51 |
+
# Display results
|
| 52 |
+
st.write("**Encrypted Text:**", encrypted_text)
|
| 53 |
+
st.write("**Helper Phrase for Decryption:**", helper_phrase)
|
| 54 |
+
|
| 55 |
+
# Display cipher dictionary for testing (optional)
|
| 56 |
+
if st.checkbox("Show Cipher (for testing purposes)"):
|
| 57 |
+
st.write(cipher)
|