Spaces:
Sleeping
Sleeping
Create tab1to3_unified.py
Browse files- tab/tab1to3_unified.py +84 -0
tab/tab1to3_unified.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from cryptography.fernet import Fernet
|
| 3 |
+
import qrcode
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
import base64
|
| 7 |
+
import random
|
| 8 |
+
|
| 9 |
+
# Function to simulate QKD key (random bits)
|
| 10 |
+
def generate_qkd_key(length):
|
| 11 |
+
return ''.join(random.choice(['0', '1']) for _ in range(length))
|
| 12 |
+
|
| 13 |
+
# Function to create a Fernet key from QKD bits
|
| 14 |
+
def qkd_bits_to_fernet_key(qkd_bits):
|
| 15 |
+
# Pad/truncate to 256 bits (32 bytes)
|
| 16 |
+
bits_256 = qkd_bits[:256].ljust(256, '0')
|
| 17 |
+
# Convert to bytes and then base64 encode for Fernet
|
| 18 |
+
byte_arr = int(bits_256, 2).to_bytes(32, byteorder='big')
|
| 19 |
+
return base64.urlsafe_b64encode(byte_arr)
|
| 20 |
+
|
| 21 |
+
# Encrypt message with Fernet key
|
| 22 |
+
def encrypt_message(msg, qkd_bits):
|
| 23 |
+
key = qkd_bits_to_fernet_key(qkd_bits)
|
| 24 |
+
f = Fernet(key)
|
| 25 |
+
encrypted = f.encrypt(msg.encode()).decode()
|
| 26 |
+
qr = qrcode.make(key.decode())
|
| 27 |
+
return encrypted, key.decode(), qr
|
| 28 |
+
|
| 29 |
+
# Decrypt message
|
| 30 |
+
def decrypt_message(encrypted_msg, key):
|
| 31 |
+
try:
|
| 32 |
+
f = Fernet(key.encode())
|
| 33 |
+
decrypted = f.decrypt(encrypted_msg.encode()).decode()
|
| 34 |
+
return decrypted
|
| 35 |
+
except Exception as e:
|
| 36 |
+
return f"β Decryption Failed: {str(e)}"
|
| 37 |
+
|
| 38 |
+
# Unified Gradio Tab
|
| 39 |
+
def get_tab1to3_unified():
|
| 40 |
+
with gr.Tab("π Quantum Encryption Suite"):
|
| 41 |
+
gr.Markdown("""
|
| 42 |
+
## π‘οΈ QuantumCrypt: Complete Encryption + Decryption Suite
|
| 43 |
+
This tab unifies:
|
| 44 |
+
- π QKD-based Quantum Key Generation
|
| 45 |
+
- π Message Encryption
|
| 46 |
+
- π Message Decryption
|
| 47 |
+
""")
|
| 48 |
+
|
| 49 |
+
# Shared state to hold QKD key
|
| 50 |
+
qkd_key_state = gr.State("")
|
| 51 |
+
|
| 52 |
+
with gr.Accordion("π 1. Generate Quantum Key (QKD)", open=True):
|
| 53 |
+
key_slider = gr.Slider(minimum=72, maximum=512, value=128, step=8, label="Select QKD Key Length (bits)")
|
| 54 |
+
gen_key_btn = gr.Button("π² Generate QKD Key")
|
| 55 |
+
qkd_key_output = gr.Textbox(label="Generated QKD Key", lines=1)
|
| 56 |
+
|
| 57 |
+
with gr.Accordion("π 2. Encrypt a Message", open=False):
|
| 58 |
+
msg_input = gr.Textbox(label="Message to Encrypt", placeholder="Enter your secret message")
|
| 59 |
+
encrypt_btn = gr.Button("π Encrypt with QKD Key")
|
| 60 |
+
encrypted_output = gr.Textbox(label="Encrypted Message")
|
| 61 |
+
fernet_output = gr.Textbox(label="Fernet Key Used (Base64)")
|
| 62 |
+
qr_output = gr.Image(label="QR Code of Key", type="pil")
|
| 63 |
+
|
| 64 |
+
with gr.Accordion("π 3. Decrypt a Message", open=False):
|
| 65 |
+
encrypted_input = gr.Textbox(label="Encrypted Message")
|
| 66 |
+
decrypt_key_input = gr.Textbox(label="Fernet Key")
|
| 67 |
+
decrypt_btn = gr.Button("π Decrypt")
|
| 68 |
+
decrypted_output = gr.Textbox(label="Decrypted Message")
|
| 69 |
+
|
| 70 |
+
# Event: Generate QKD Key
|
| 71 |
+
def handle_qkd(length):
|
| 72 |
+
key = generate_qkd_key(length)
|
| 73 |
+
return key, key
|
| 74 |
+
|
| 75 |
+
gen_key_btn.click(fn=handle_qkd, inputs=[key_slider], outputs=[qkd_key_output, qkd_key_state])
|
| 76 |
+
|
| 77 |
+
# Event: Encrypt Message
|
| 78 |
+
encrypt_btn.click(fn=encrypt_message, inputs=[msg_input, qkd_key_state],
|
| 79 |
+
outputs=[encrypted_output, fernet_output, qr_output])
|
| 80 |
+
|
| 81 |
+
# Event: Decrypt Message
|
| 82 |
+
decrypt_btn.click(fn=decrypt_message,
|
| 83 |
+
inputs=[encrypted_input, decrypt_key_input],
|
| 84 |
+
outputs=[decrypted_output])
|