Download app.py from enoderr/enCoderr: direct link, hf CLI and curl.
- Browser
- Download file 3.85 kB
-
https://huggingface.co/spaces/enoderr/enCoderr/resolve/main/app.py
- Command line
-
hf download hf://spaces/enoderr/enCoderr/app.py
-
curl -L -o app.py https://huggingface.co/spaces/enoderr/enCoderr/resolve/main/app.py
3.85 kB
| import streamlit as st | |
| import random | |
| import string | |
| # Generate a unique random character for each letter of the alphabet | |
| def generate_variable_mapping(): | |
| letters = string.ascii_letters # Includes both lowercase and uppercase letters | |
| shuffled_characters = list(set(string.ascii_letters + string.digits + string.punctuation)) | |
| random.shuffle(shuffled_characters) | |
| variable_mapping = {} | |
| for letter in letters: | |
| variable_mapping[letter] = shuffled_characters.pop() | |
| return variable_mapping | |
| def encrypt_text(plaintext, mapping): | |
| # Encrypt the plaintext using the variable mapping | |
| return "".join(mapping.get(char, char) for char in plaintext) | |
| # Helper words list with common French words for each alphabet letter | |
| helper_words = { | |
| 'A': ["salade", "banane", "flamme", "chaise", "carafe"], | |
| 'B': ["bison", "cabine", "blouse", "abri", "bonbon"], | |
| 'C': ["cerise", "cactus", "canard", "chaud", "cirque"], | |
| 'D': ["dinde", "décor", "donner", "douleur", "début"], | |
| 'E': ["pêche", "mercredi", "effet", "plage", "épice"], | |
| 'F': ["fête", "fleur", "framboise", "frère", "façade"], | |
| 'G': ["groupe", "gagne", "galerie", "grille", "guerre"], | |
| 'H': ["histoire", "hasard", "hiver", "homme", "horizon"], | |
| 'I': ["étudier", "milieu", "jardin", "écrire", "grill"], | |
| 'J': ["jaune", "jupe", "joli", "jambe", "jouet"], | |
| 'K': ["kilo", "kiwi", "kangourou", "kermesse", "kayak"], | |
| 'L': ["lumière", "lien", "lire", "loup", "lait"], | |
| 'M': ["mère", "mignon", "monde", "musique", "moulin"], | |
| 'N': ["nuage", "nuit", "nommer", "neige", "nourrir"], | |
| 'O': ["orange", "origine", "opéra", "objet", "ombrelle"], | |
| 'P': ["poire", "piano", "pluie", "parc", "porte"], | |
| 'Q': ["quatre", "qualité", "quitte", "queue", "quart"], | |
| 'R': ["rose", "rire", "réveil", "route", "réponse"], | |
| 'S': ["soleil", "salle", "sourire", "sombre", "salut"], | |
| 'T': ["table", "téléphone", "tigre", "tarte", "triste"], | |
| 'U': ["univers", "unité", "unique", "urgent", "utile"], | |
| 'V': ["vache", "valise", "visage", "vitrine", "vendre"], | |
| 'W': ["wagon", "weekend", "whisky", "web", "watt"], | |
| 'X': ["xénon", "exemple", "toxique", "fox", "lux"], | |
| 'Y': ["yeux", "yo-yo", "yacht", "yeti", "yogourt"], | |
| 'Z': ["zèbre", "zone", "zinc", "zodiac", "zigzag"] | |
| } | |
| def generate_helper_phrase(plaintext, mapping): | |
| unique_letters = set(char for char in plaintext if char.isalpha()) # Get unique letters from plaintext | |
| helper_phrase = [] | |
| # Generate five words for each unique letter in the plaintext | |
| for letter in unique_letters: | |
| letter_upper = letter.upper() | |
| if letter_upper in helper_words: | |
| selected_words = random.sample(helper_words[letter_upper], 5) # Select 5 random words | |
| helper_phrase.extend(selected_words) | |
| # Join the helper phrase into a single string | |
| helper_phrase_str = " ".join(helper_phrase) | |
| # Encrypt the entire helper phrase using the same variable mapping | |
| encrypted_helper_phrase = encrypt_text(helper_phrase_str, mapping) | |
| return encrypted_helper_phrase | |
| # Streamlit App | |
| st.title("Chiffrement par Substitution") | |
| 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).") | |
| # User input | |
| plaintext = st.text_input("Entrez le texte à chiffrer:", "Exemple de texte") | |
| if st.button("Chiffrer"): | |
| mapping = generate_variable_mapping() | |
| ciphertext = encrypt_text(plaintext, mapping) | |
| helper_phrase = generate_helper_phrase(plaintext, mapping) | |
| # Display the encrypted text and helper phrase | |
| st.write("Texte chiffré:") | |
| st.code(ciphertext, language="") | |
| st.write("Phrase d'aide:") | |
| st.code(helper_phrase, language="") | |
| st.write("Note: Utilisez la phrase d'aide pour déduire les correspondances des lettres.") | |