kosmoscpp commited on
Commit
b6b5db2
ยท
verified ยท
1 Parent(s): 482ecdb

Create app.py

Browse files

Encrypt and decrypt messages using a key-based substitution cipher with deterministic secret alphabets.

Files changed (1) hide show
  1. app.py +146 -0
app.py ADDED
@@ -0,0 +1,146 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import string
2
+ import random
3
+ import gradio as ui
4
+
5
+ def generate_secret_alphabet(key: str) -> str:
6
+ """Generates a deterministic shuffled alphabet based on the provided key."""
7
+ standard_alphabet = list(string.ascii_uppercase)
8
+ # Using random.Random with a seed ensures determinism across runs
9
+ seeded_random = random.Random(key)
10
+ seeded_random.shuffle(standard_alphabet)
11
+ return "".join(standard_alphabet)
12
+
13
+ def validate_inputs(key: str, text: str):
14
+ """Validates that key and text are not empty or just whitespace."""
15
+ if not key.strip():
16
+ raise ui.Error("Key cannot be empty! Please enter a valid key.")
17
+ if not text.strip():
18
+ raise ui.Error("Text cannot be empty! Please enter some text.")
19
+
20
+ def encrypt_text(key: str, plain_text: str):
21
+ validate_inputs(key, plain_text)
22
+
23
+ secret_alphabet = generate_secret_alphabet(key)
24
+ standard_alphabet = string.ascii_uppercase
25
+
26
+ # Create mapping dictionaries for fast lookup
27
+ upper_map = {src: dst for src, dst in zip(standard_alphabet, secret_alphabet)}
28
+ lower_map = {src.lower(): dst.lower() for src, dst in zip(standard_alphabet, secret_alphabet)}
29
+
30
+ encrypted_chars = []
31
+ for char in plain_text:
32
+ if char.isupper():
33
+ encrypted_chars.append(upper_map.get(char, char))
34
+ elif char.islower():
35
+ encrypted_chars.append(lower_map.get(char, char))
36
+ else:
37
+ encrypted_chars.append(char)
38
+
39
+ # Format alphabet display for the user
40
+ alphabet_display = f"Standard: {standard_alphabet}\nSecret: {secret_alphabet}"
41
+ return "".join(encrypted_chars), alphabet_display
42
+
43
+ def decrypt_text(key: str, cipher_text: str):
44
+ validate_inputs(key, cipher_text)
45
+
46
+ secret_alphabet = generate_secret_alphabet(key)
47
+ standard_alphabet = string.ascii_uppercase
48
+
49
+ # Reverse mapping for decryption
50
+ upper_map = {src: dst for src, dst in zip(secret_alphabet, standard_alphabet)}
51
+ lower_map = {src.lower(): dst.lower() for src, dst in zip(secret_alphabet, standard_alphabet)}
52
+
53
+ decrypted_chars = []
54
+ for char in cipher_text:
55
+ if char.isupper():
56
+ decrypted_chars.append(upper_map.get(char, char))
57
+ elif char.islower():
58
+ decrypted_chars.append(lower_map.get(char, char))
59
+ else:
60
+ decrypted_chars.append(char)
61
+
62
+ alphabet_display = f"Standard: {standard_alphabet}\nSecret: {secret_alphabet}"
63
+ return "".join(decrypted_chars), alphabet_display
64
+
65
+ # Build the Gradio Interface
66
+ with ui.Blocks(title="Cipherer", theme=ui.themes.Soft()) as demo:
67
+ ui.Markdown("# ๐Ÿ” Cipherer")
68
+ ui.Markdown(
69
+ "Encrypt and decrypt text using a deterministic, key-based substitution cipher. "
70
+ "The same key will always generate the exact same secret alphabet mapping."
71
+ )
72
+
73
+ with ui.Tabs():
74
+ # --- ENCRYPT TAB ---
75
+ with ui.TabItem("โš™๏ธ Encrypt"):
76
+ with ui.Row():
77
+ with ui.Column():
78
+ encrypt_key = ui.Textbox(
79
+ label="Secret Key",
80
+ placeholder="e.g., TOMATO",
81
+ max_lines=1
82
+ )
83
+ plain_input = ui.Textbox(
84
+ label="Plain Text",
85
+ placeholder="Enter text to encrypt here...",
86
+ lines=5
87
+ )
88
+ encrypt_btn = ui.Button("Encrypt Text", variant="primary")
89
+
90
+ with ui.Column():
91
+ encrypt_output = ui.Textbox(
92
+ label="Encrypted Output",
93
+ interactive=False,
94
+ lines=5,
95
+ show_copy_button=True
96
+ )
97
+ encrypt_mapping = ui.Textbox(
98
+ label="Generated Alphabet Mapping",
99
+ interactive=False,
100
+ lines=2
101
+ )
102
+
103
+ encrypt_btn.click(
104
+ fn=encrypt_text,
105
+ inputs=[encrypt_key, plain_input],
106
+ outputs=[encrypt_output, encrypt_mapping]
107
+ )
108
+
109
+ # --- DECRYPT TAB ---
110
+ with ui.TabItem("๐Ÿ”“ Decrypt"):
111
+ with ui.Row():
112
+ with ui.Column():
113
+ decrypt_key = ui.Textbox(
114
+ label="Secret Key",
115
+ placeholder="e.g., TOMATO",
116
+ max_lines=1
117
+ )
118
+ cipher_input = ui.Textbox(
119
+ label="Cipher Text",
120
+ placeholder="Enter text to decrypt here...",
121
+ lines=5
122
+ )
123
+ decrypt_btn = ui.Button("Decrypt Text", variant="primary")
124
+
125
+ with ui.Column():
126
+ decrypt_output = ui.Textbox(
127
+ label="Decrypted Output",
128
+ interactive=False,
129
+ lines=5,
130
+ show_copy_button=True
131
+ )
132
+ decrypt_mapping = ui.Textbox(
133
+ label="Generated Alphabet Mapping",
134
+ interactive=False,
135
+ lines=2
136
+ )
137
+
138
+ decrypt_btn.click(
139
+ fn=decrypt_text,
140
+ inputs=[decrypt_key, cipher_input],
141
+ outputs=[decrypt_output, decrypt_mapping]
142
+ )
143
+
144
+ if __name__ == "__main__":
145
+ demo.launch()
146
+