danhtran2mind commited on
Commit
e421e00
·
verified ·
1 Parent(s): f1687a9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +128 -27
app.py CHANGED
@@ -7,6 +7,89 @@ from Crypto.Hash import SHA256
7
  import base64
8
  import gradio as gr
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  def generate_random_key(length=32):
11
  """Generate a random key with digits, uppercase, lowercase, and special characters."""
12
  characters = string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation
@@ -26,7 +109,6 @@ def derive_key(custom_key, key_size):
26
  hasher = SHA256.new()
27
  hasher.update(custom_key.encode('utf-8'))
28
  full_hash = hasher.digest()
29
- # Convert key_size to integer before division
30
  return full_hash[:int(key_size) // 8]
31
 
32
  def encrypt_string(plain_text, key, key_size):
@@ -68,41 +150,60 @@ def generate_and_display_key():
68
  return generate_random_key()
69
 
70
  # Gradio interface
71
- with gr.Blocks() as demo:
72
- gr.Markdown("# AES Encryption/Decryption App")
73
- gr.Markdown("Encrypt or decrypt text using AES (128, 192, or 256-bit keys).")
 
 
 
 
74
 
75
- with gr.Row():
 
76
  key_size = gr.Radio(
77
  choices=["128", "192", "256"],
78
  label="AES Key Size (bits)",
79
- value="256"
80
- )
81
-
82
- with gr.Row():
83
- custom_key = gr.Textbox(
84
- label="Custom Key",
85
- placeholder="Enter your key or generate one",
86
- lines=1
87
  )
88
- generate_key_btn = gr.Button("Generate Random Key")
 
 
 
 
 
 
 
 
89
 
90
- with gr.Row():
 
91
  input_text = gr.Textbox(
92
  label="Input Text",
93
- placeholder="Enter text to encrypt",
94
- lines=3
95
  )
96
-
97
- with gr.Row():
98
- encrypt_btn = gr.Button("Encrypt")
99
- decrypt_btn = gr.Button("Decrypt")
100
-
101
- output_text = gr.Textbox(
102
- label="Output",
103
- placeholder="Result will appear here",
104
- lines=3,
105
- interactive=False
 
 
 
 
 
 
 
 
 
 
 
106
  )
107
 
108
  # Event handlers
 
7
  import base64
8
  import gradio as gr
9
 
10
+ # Custom CSS for modern styling
11
+ custom_css = """
12
+ body {
13
+ font-family: 'Inter', sans-serif;
14
+ background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
15
+ }
16
+
17
+ .gradio-container {
18
+ max-width: 800px;
19
+ margin: 20px auto;
20
+ background: rgba(255, 255, 255, 0.95);
21
+ border-radius: 16px;
22
+ box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
23
+ padding: 24px;
24
+ }
25
+
26
+ h1, .gr-markdown h1 {
27
+ color: #1e3c72;
28
+ font-weight: 700;
29
+ text-align: center;
30
+ margin-bottom: 16px;
31
+ }
32
+
33
+ .gr-button {
34
+ border-radius: 8px;
35
+ padding: 12px 24px;
36
+ font-weight: 600;
37
+ transition: all 0.3s ease;
38
+ }
39
+
40
+ .gr-button-primary {
41
+ background: #2a5298;
42
+ color: white;
43
+ border: none;
44
+ }
45
+
46
+ .gr-button-primary:hover {
47
+ background: #1e3c72;
48
+ transform: translateY(-2px);
49
+ }
50
+
51
+ .gr-button-secondary {
52
+ background: #f1f3f5;
53
+ color: #2a5298;
54
+ border: 1px solid #2a5298;
55
+ }
56
+
57
+ .gr-button-secondary:hover {
58
+ background: #e9ecef;
59
+ transform: translateY(-2px);
60
+ }
61
+
62
+ .gr-textbox, .gr-radio {
63
+ border-radius: 8px;
64
+ border: 1px solid #dee2e6;
65
+ background: #f8f9fa;
66
+ }
67
+
68
+ .gr-textbox:focus {
69
+ border-color: #2a5298;
70
+ box-shadow: 0 0 0 3px rgba(42, 82, 152, 0.2);
71
+ }
72
+
73
+ .gr-row {
74
+ gap: 16px;
75
+ }
76
+
77
+ .card {
78
+ background: white;
79
+ border-radius: 12px;
80
+ padding: 16px;
81
+ box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
82
+ margin-bottom: 16px;
83
+ }
84
+
85
+ .footer {
86
+ text-align: center;
87
+ color: #6c757d;
88
+ font-size: 0.9em;
89
+ margin-top: 24px;
90
+ }
91
+ """
92
+
93
  def generate_random_key(length=32):
94
  """Generate a random key with digits, uppercase, lowercase, and special characters."""
95
  characters = string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation
 
109
  hasher = SHA256.new()
110
  hasher.update(custom_key.encode('utf-8'))
111
  full_hash = hasher.digest()
 
112
  return full_hash[:int(key_size) // 8]
113
 
114
  def encrypt_string(plain_text, key, key_size):
 
150
  return generate_random_key()
151
 
152
  # Gradio interface
153
+ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
154
+ gr.Markdown(
155
+ """
156
+ # 🔒 AES Encryption/Decryption App
157
+ Securely encrypt or decrypt text using AES with 128, 192, or 256-bit keys.
158
+ """
159
+ )
160
 
161
+ with gr.Column(elem_classes="card"):
162
+ gr.Markdown("### Configuration")
163
  key_size = gr.Radio(
164
  choices=["128", "192", "256"],
165
  label="AES Key Size (bits)",
166
+ value="256",
167
+ elem_classes="radio"
 
 
 
 
 
 
168
  )
169
+
170
+ with gr.Row():
171
+ custom_key = gr.Textbox(
172
+ label="Custom Key",
173
+ placeholder="Enter your key or generate one",
174
+ lines=1,
175
+ show_copy_button=True
176
+ )
177
+ generate_key_btn = gr.Button("Generate Key", variant="secondary")
178
 
179
+ with gr.Column(elem_classes="card"):
180
+ gr.Markdown("### Input")
181
  input_text = gr.Textbox(
182
  label="Input Text",
183
+ placeholder="Enter text to encrypt or encrypted text to decrypt",
184
+ lines=4
185
  )
186
+
187
+ with gr.Row():
188
+ encrypt_btn = gr.Button("🔐 Encrypt", variant="primary")
189
+ decrypt_btn = gr.Button("🔓 Decrypt", variant="secondary")
190
+
191
+ with gr.Column(elem_classes="card"):
192
+ gr.Markdown("### Output")
193
+ output_text = gr.Textbox(
194
+ label="Result",
195
+ placeholder="Result will appear here",
196
+ lines=4,
197
+ interactive=False,
198
+ show_copy_button=True
199
+ )
200
+
201
+ gr.Markdown(
202
+ """
203
+ <div class="footer">
204
+ Powered by Gradio & PyCrypto | © 2025 SecureApp
205
+ </div>
206
+ """
207
  )
208
 
209
  # Event handlers