File size: 3,854 Bytes
d5a27a0
 
 
 
1ae392f
 
 
 
 
50e8391
1ae392f
 
 
50e8391
1ae392f
50e8391
1ae392f
 
 
50e8391
14caaa7
2a64a28
14caaa7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2a64a28
 
bb51d3c
 
2a64a28
14caaa7
bb51d3c
 
 
 
 
 
14caaa7
9c9218c
 
60db3e8
1ae392f
 
9c9218c
 
50e8391
 
 
 
 
 
 
 
 
1ae392f
 
bb51d3c
50e8391
 
 
 
 
 
 
 
1ae392f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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.")