Enoder commited on
Commit
50e8391
·
verified ·
1 Parent(s): 660d9c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -52
app.py CHANGED
@@ -2,56 +2,55 @@ 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)
 
 
 
 
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.")