Enoder commited on
Commit
1ae392f
·
verified ·
1 Parent(s): 2c093c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -29
app.py CHANGED
@@ -2,31 +2,21 @@ import streamlit as st
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_cipher_map(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
- return cipher_map
26
-
27
- def generate_ciphertext(plaintext, cipher_map):
28
- # Create the encrypted text using the cipher map
29
- return "".join([cipher_map.get(char, char) for char in plaintext])
30
 
31
  # Helper words list with common French words for each alphabet letter
32
  helper_words = {
@@ -58,7 +48,7 @@ helper_words = {
58
  'Z': ["zèbre", "zone", "zinc", "zodiac", "zigzag"]
59
  }
60
 
61
- def generate_helper_phrase(ciphertext, cipher_map):
62
  helper_phrase = []
63
 
64
  # Generate three words for each letter in the ciphertext
@@ -71,8 +61,8 @@ def generate_helper_phrase(ciphertext, cipher_map):
71
  # Join the helper phrase into a single string
72
  helper_phrase_str = " ".join(helper_phrase)
73
 
74
- # Encrypt the entire helper phrase using the same cipher map
75
- encrypted_helper_phrase = generate_ciphertext(helper_phrase_str, cipher_map)
76
 
77
  return encrypted_helper_phrase
78
 
@@ -84,9 +74,9 @@ st.write("Ce système remplace chaque lettre d'un texte par un caractère aléat
84
  plaintext = st.text_input("Entrez le texte à chiffrer:", "Exemple de texte")
85
 
86
  if st.button("Chiffrer"):
87
- cipher_map = generate_cipher_map(plaintext)
88
- ciphertext = generate_ciphertext(plaintext, cipher_map)
89
- helper_phrase = generate_helper_phrase(ciphertext, cipher_map)
90
 
91
  # Display the encrypted text and helper phrase
92
  st.write("Texte chiffré:")
@@ -95,4 +85,4 @@ if st.button("Chiffrer"):
95
  st.write("Phrase d'aide:")
96
  st.code(helper_phrase, language="")
97
 
98
- st.write("Note: Utilisez la phrase d'aide pour déduire les correspondances des lettres.")
 
2
  import random
3
  import string
4
 
5
+ # Generate a unique random character for each letter of the alphabet
6
+ def generate_variable_mapping():
7
+ letters = string.ascii_letters # Includes both lowercase and uppercase letters
8
+ shuffled_characters = list(set(string.ascii_letters + string.digits + string.punctuation))
9
+ random.shuffle(shuffled_characters)
10
 
11
+ variable_mapping = {}
12
+ for letter in letters:
13
+ variable_mapping[letter] = shuffled_characters.pop()
 
14
 
15
+ return variable_mapping
 
 
 
 
 
 
 
16
 
17
+ def encrypt_text(plaintext, mapping):
18
+ # Encrypt the plaintext using the variable mapping
19
+ return "".join(mapping.get(char, char) for char in plaintext)
 
 
20
 
21
  # Helper words list with common French words for each alphabet letter
22
  helper_words = {
 
48
  'Z': ["zèbre", "zone", "zinc", "zodiac", "zigzag"]
49
  }
50
 
51
+ def generate_helper_phrase(ciphertext, mapping):
52
  helper_phrase = []
53
 
54
  # Generate three words for each letter in the ciphertext
 
61
  # Join the helper phrase into a single string
62
  helper_phrase_str = " ".join(helper_phrase)
63
 
64
+ # Encrypt the entire helper phrase using the same variable mapping
65
+ encrypted_helper_phrase = encrypt_text(helper_phrase_str, mapping)
66
 
67
  return encrypted_helper_phrase
68
 
 
74
  plaintext = st.text_input("Entrez le texte à chiffrer:", "Exemple de texte")
75
 
76
  if st.button("Chiffrer"):
77
+ mapping = generate_variable_mapping()
78
+ ciphertext = encrypt_text(plaintext, mapping)
79
+ helper_phrase = generate_helper_phrase(ciphertext, mapping)
80
 
81
  # Display the encrypted text and helper phrase
82
  st.write("Texte chiffré:")
 
85
  st.write("Phrase d'aide:")
86
  st.code(helper_phrase, language="")
87
 
88
+ st.write("Note: Utilisez la phrase d'aide pour déduire les correspondances des lettres.")