Spaces:
Sleeping
Sleeping
Update tab/tab1to3_unified.py
Browse files- tab/tab1to3_unified.py +26 -39
tab/tab1to3_unified.py
CHANGED
|
@@ -18,7 +18,9 @@ def generate_qkd_key(length=128):
|
|
| 18 |
bob_basis = random.randint(0, 1)
|
| 19 |
if alice_basis == bob_basis:
|
| 20 |
shared_key += str(alice_bit)
|
| 21 |
-
debug_lines.append(
|
|
|
|
|
|
|
| 22 |
return shared_key, "\n".join(debug_lines)
|
| 23 |
|
| 24 |
# ---------------- Convert QKD to Fernet Key ----------------
|
|
@@ -44,7 +46,7 @@ def generate_qr_image(data):
|
|
| 44 |
buf.seek(0)
|
| 45 |
return Image.open(buf)
|
| 46 |
|
| 47 |
-
# ---------------- QR Code Decoder ----------------
|
| 48 |
def decode_qr_image(pil_img):
|
| 49 |
img = cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGB2BGR)
|
| 50 |
detector = cv2.QRCodeDetector()
|
|
@@ -84,52 +86,48 @@ def quantum_decrypt(encrypted_message, key):
|
|
| 84 |
def get_tab1to3_unified():
|
| 85 |
with gr.Tab("π Quantum Key | Encrypt | Decrypt"):
|
| 86 |
|
| 87 |
-
#
|
| 88 |
with gr.Row():
|
| 89 |
with gr.Column():
|
| 90 |
gr.Markdown("""<div style="text-align: center;">
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
</p>
|
| 95 |
-
</div>""")
|
| 96 |
key_length = gr.Slider(72, 512, step=8, value=256, label="QKD Key Length (Recommended β₯ 256)")
|
| 97 |
qkd_btn = gr.Button("Generate QKD Key")
|
| 98 |
qkd_output = gr.Textbox(label="Generated QKD Key", lines=2)
|
| 99 |
debug_log = gr.Textbox(label="Debug Log", visible=True, lines=8)
|
| 100 |
gr.Accordion("π Show BB84 Debug Log", open=False).append(debug_log)
|
|
|
|
| 101 |
gr.Markdown("<br>")
|
| 102 |
|
| 103 |
-
#
|
| 104 |
with gr.Row():
|
| 105 |
with gr.Column():
|
| 106 |
gr.Markdown("""<div style="text-align: center;">
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
</p>
|
| 111 |
-
</div>""")
|
| 112 |
msg_input = gr.Textbox(label="Message to Encrypt")
|
| 113 |
encrypt_btn = gr.Button("Encrypt")
|
| 114 |
encrypted_output = gr.Textbox(label="Encrypted Message", lines=2)
|
| 115 |
key_used_output = gr.Textbox(label="Fernet Key Used", lines=2)
|
| 116 |
with gr.Column():
|
| 117 |
gr.Markdown("""<div style="text-align: center;">
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
</p>
|
| 122 |
-
</div>""")
|
| 123 |
qr_code_output = gr.Image(label="", height=378)
|
|
|
|
| 124 |
gr.Markdown("<br>")
|
| 125 |
|
| 126 |
-
#
|
| 127 |
with gr.Row():
|
| 128 |
with gr.Column():
|
| 129 |
gr.Markdown("""<div style="text-align: center;">
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
qr_upload = gr.Image(type="pil", label="Upload QR Code Image")
|
| 134 |
auto_msg = gr.Textbox(label="Encrypted Message")
|
| 135 |
auto_key = gr.Textbox(label="Fernet Key")
|
|
@@ -138,19 +136,8 @@ def get_tab1to3_unified():
|
|
| 138 |
|
| 139 |
gr.Markdown("<br>")
|
| 140 |
|
| 141 |
-
#
|
| 142 |
-
qkd_btn.click(fn=generate_qkd_key,
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
encrypt_btn.click(fn=quantum_encrypt,
|
| 147 |
-
inputs=[msg_input, qkd_output],
|
| 148 |
-
outputs=[encrypted_output, key_used_output, qr_code_output])
|
| 149 |
-
|
| 150 |
-
qr_upload.change(fn=decode_qr_image,
|
| 151 |
-
inputs=[qr_upload],
|
| 152 |
-
outputs=[auto_msg, auto_key])
|
| 153 |
-
|
| 154 |
-
decrypt_btn.click(fn=quantum_decrypt,
|
| 155 |
-
inputs=[auto_msg, auto_key],
|
| 156 |
-
outputs=[decrypted_output])
|
|
|
|
| 18 |
bob_basis = random.randint(0, 1)
|
| 19 |
if alice_basis == bob_basis:
|
| 20 |
shared_key += str(alice_bit)
|
| 21 |
+
debug_lines.append(
|
| 22 |
+
f"Alice bit: {alice_bit}, Alice basis: {alice_basis}, Bob basis: {bob_basis}, Match: {alice_basis == bob_basis}"
|
| 23 |
+
)
|
| 24 |
return shared_key, "\n".join(debug_lines)
|
| 25 |
|
| 26 |
# ---------------- Convert QKD to Fernet Key ----------------
|
|
|
|
| 46 |
buf.seek(0)
|
| 47 |
return Image.open(buf)
|
| 48 |
|
| 49 |
+
# ---------------- QR Code Decoder using OpenCV ----------------
|
| 50 |
def decode_qr_image(pil_img):
|
| 51 |
img = cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGB2BGR)
|
| 52 |
detector = cv2.QRCodeDetector()
|
|
|
|
| 86 |
def get_tab1to3_unified():
|
| 87 |
with gr.Tab("π Quantum Key | Encrypt | Decrypt"):
|
| 88 |
|
| 89 |
+
# QKD Section
|
| 90 |
with gr.Row():
|
| 91 |
with gr.Column():
|
| 92 |
gr.Markdown("""<div style="text-align: center;">
|
| 93 |
+
<h4 style="font-size: 22px; color: #a3c14a;">π QKD Key Generator</h4>
|
| 94 |
+
<p style="font-size: 16px; color: #cccccc;">Generate a secure quantum key using BB84 protocol.</p>
|
| 95 |
+
</div>""")
|
|
|
|
|
|
|
| 96 |
key_length = gr.Slider(72, 512, step=8, value=256, label="QKD Key Length (Recommended β₯ 256)")
|
| 97 |
qkd_btn = gr.Button("Generate QKD Key")
|
| 98 |
qkd_output = gr.Textbox(label="Generated QKD Key", lines=2)
|
| 99 |
debug_log = gr.Textbox(label="Debug Log", visible=True, lines=8)
|
| 100 |
gr.Accordion("π Show BB84 Debug Log", open=False).append(debug_log)
|
| 101 |
+
|
| 102 |
gr.Markdown("<br>")
|
| 103 |
|
| 104 |
+
# Encryption Section
|
| 105 |
with gr.Row():
|
| 106 |
with gr.Column():
|
| 107 |
gr.Markdown("""<div style="text-align: center;">
|
| 108 |
+
<h4 style="font-size: 22px; color: #a3c14a;">π§ Encrypt Message using QKD</h4>
|
| 109 |
+
<p style="font-size: 16px; color: #cccccc;">Encrypt messages using the QKD key with Fernet encryption.</p>
|
| 110 |
+
</div>""")
|
|
|
|
|
|
|
| 111 |
msg_input = gr.Textbox(label="Message to Encrypt")
|
| 112 |
encrypt_btn = gr.Button("Encrypt")
|
| 113 |
encrypted_output = gr.Textbox(label="Encrypted Message", lines=2)
|
| 114 |
key_used_output = gr.Textbox(label="Fernet Key Used", lines=2)
|
| 115 |
with gr.Column():
|
| 116 |
gr.Markdown("""<div style="text-align: center;">
|
| 117 |
+
<h4 style="font-size: 22px; color: #a3c14a;">π¦ QR Code (Key + Cipher)</h4>
|
| 118 |
+
<p style="font-size: 16px; color: #cccccc;">Generate a QR containing the Fernet key and encrypted message.</p>
|
| 119 |
+
</div>""")
|
|
|
|
|
|
|
| 120 |
qr_code_output = gr.Image(label="", height=378)
|
| 121 |
+
|
| 122 |
gr.Markdown("<br>")
|
| 123 |
|
| 124 |
+
# QR Upload + Decryption Section
|
| 125 |
with gr.Row():
|
| 126 |
with gr.Column():
|
| 127 |
gr.Markdown("""<div style="text-align: center;">
|
| 128 |
+
<h4 style="font-size: 22px; color: #a3c14a;">π€ Upload QR to Autofill</h4>
|
| 129 |
+
<p style="font-size: 16px; color: #cccccc;">Upload a QR image. Message + Key will autofill below.</p>
|
| 130 |
+
</div>""")
|
| 131 |
qr_upload = gr.Image(type="pil", label="Upload QR Code Image")
|
| 132 |
auto_msg = gr.Textbox(label="Encrypted Message")
|
| 133 |
auto_key = gr.Textbox(label="Fernet Key")
|
|
|
|
| 136 |
|
| 137 |
gr.Markdown("<br>")
|
| 138 |
|
| 139 |
+
# Callback Logic
|
| 140 |
+
qkd_btn.click(fn=generate_qkd_key, inputs=[key_length], outputs=[qkd_output, debug_log])
|
| 141 |
+
encrypt_btn.click(fn=quantum_encrypt, inputs=[msg_input, qkd_output], outputs=[encrypted_output, key_used_output, qr_code_output])
|
| 142 |
+
qr_upload.change(fn=decode_qr_image, inputs=[qr_upload], outputs=[auto_msg, auto_key])
|
| 143 |
+
decrypt_btn.click(fn=quantum_decrypt, inputs=[auto_msg, auto_key], outputs=[decrypted_output])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|