Update app.py
Browse files
app.py
CHANGED
|
@@ -2,56 +2,55 @@ import streamlit as st
|
|
| 2 |
import random
|
| 3 |
import string
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
for
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
encrypted_text = ""
|
| 23 |
-
for char in text.lower():
|
| 24 |
-
if char in cipher:
|
| 25 |
-
encrypted_text += cipher[char]
|
| 26 |
else:
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
#
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import random
|
| 3 |
import string
|
| 4 |
|
| 5 |
+
# Helper functions for substitution cipher
|
| 6 |
+
def categorize_letters():
|
| 7 |
+
vowels = "AEIOUYaeiouy"
|
| 8 |
+
consonants = "".join([c for c in string.ascii_letters if c not in vowels])
|
| 9 |
+
return vowels, consonants
|
| 10 |
+
|
| 11 |
+
def generate_ciphertext(plaintext):
|
| 12 |
+
vowels, consonants = categorize_letters()
|
| 13 |
+
symbol_pool = string.ascii_letters + string.digits + string.punctuation
|
| 14 |
+
cipher_map = {}
|
| 15 |
+
|
| 16 |
+
# Create substitution map for each letter in the plaintext
|
| 17 |
+
for letter in set(plaintext):
|
| 18 |
+
if letter in vowels:
|
| 19 |
+
cipher_map[letter] = random.choice([c for c in symbol_pool if c in vowels])
|
| 20 |
+
elif letter in consonants:
|
| 21 |
+
cipher_map[letter] = random.choice([c for c in symbol_pool if c in consonants])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
else:
|
| 23 |
+
cipher_map[letter] = letter # keep spaces and non-alphabetic characters as is
|
| 24 |
+
|
| 25 |
+
# Create the encrypted text using the cipher map
|
| 26 |
+
ciphertext = "".join([cipher_map.get(char, char) for char in plaintext])
|
| 27 |
+
return ciphertext, cipher_map
|
| 28 |
+
|
| 29 |
+
def generate_helper_phrase(ciphertext, cipher_map):
|
| 30 |
+
# Generate a helper phrase that repeats letters from ciphertext
|
| 31 |
+
helper_words = []
|
| 32 |
+
for letter in set(ciphertext):
|
| 33 |
+
if letter.isalpha():
|
| 34 |
+
word = letter * random.randint(2, 5) # repeat the letter to give hints
|
| 35 |
+
helper_words.append(word)
|
| 36 |
+
return " ".join(helper_words)
|
| 37 |
+
|
| 38 |
+
# Streamlit App
|
| 39 |
+
st.title("Chiffrement par Substitution")
|
| 40 |
+
st.write("Ce système remplace chaque lettre d'un texte par un caractère aléatoire de la même catégorie (voyelle/consonne).")
|
| 41 |
+
|
| 42 |
+
# User input
|
| 43 |
+
plaintext = st.text_input("Entrez le texte à chiffrer:", "Exemple de texte")
|
| 44 |
+
|
| 45 |
+
if st.button("Chiffrer"):
|
| 46 |
+
ciphertext, cipher_map = generate_ciphertext(plaintext)
|
| 47 |
+
helper_phrase = generate_helper_phrase(ciphertext, cipher_map)
|
| 48 |
+
|
| 49 |
+
# Display the encrypted text and helper phrase
|
| 50 |
+
st.write("Texte chiffré:")
|
| 51 |
+
st.code(ciphertext, language="")
|
| 52 |
+
|
| 53 |
+
st.write("Phrase d'aide:")
|
| 54 |
+
st.code(helper_phrase, language="")
|
| 55 |
+
|
| 56 |
+
st.write("Note: Utilisez la phrase d'aide pour déduire les correspondances des lettres.")
|