Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,6 +9,7 @@ import base64
|
|
| 9 |
import matplotlib.pyplot as plt
|
| 10 |
import io
|
| 11 |
from PIL import Image
|
|
|
|
| 12 |
|
| 13 |
# 1. Quantum Key Distribution (BB84 Protocol)
|
| 14 |
def generate_qkd_key(length=128):
|
|
@@ -33,19 +34,28 @@ def bb84_to_fernet_key(qkey_binary_str):
|
|
| 33 |
raise ValueError("Generated Fernet key is invalid.")
|
| 34 |
return fernet_key
|
| 35 |
|
| 36 |
-
# 3.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
def quantum_encrypt(message, qkd_key):
|
| 38 |
-
if not qkd_key or len(qkd_key) <
|
| 39 |
-
return "
|
| 40 |
try:
|
| 41 |
fernet_key = bb84_to_fernet_key(qkd_key)
|
| 42 |
f = Fernet(fernet_key)
|
| 43 |
encrypted_message = f.encrypt(message.encode())
|
| 44 |
-
|
|
|
|
| 45 |
except Exception as e:
|
| 46 |
return f"Encryption error: {str(e)}", "", None
|
| 47 |
|
| 48 |
-
#
|
| 49 |
def quantum_decrypt(encrypted_message, key):
|
| 50 |
try:
|
| 51 |
f = Fernet(key.encode())
|
|
@@ -54,7 +64,7 @@ def quantum_decrypt(encrypted_message, key):
|
|
| 54 |
except Exception as e:
|
| 55 |
return f"Decryption error: {str(e)}"
|
| 56 |
|
| 57 |
-
#
|
| 58 |
def quantum_intrusion_detection(data_stream):
|
| 59 |
dev = qml.device("default.qubit", wires=2)
|
| 60 |
|
|
@@ -68,11 +78,11 @@ def quantum_intrusion_detection(data_stream):
|
|
| 68 |
try:
|
| 69 |
x = [float(i) for i in data_stream.split(',')]
|
| 70 |
anomaly_score = abs(quantum_classifier(x))
|
| 71 |
-
return "
|
| 72 |
except:
|
| 73 |
-
return "
|
| 74 |
|
| 75 |
-
#
|
| 76 |
def quantum_random_number():
|
| 77 |
backend = Aer.get_backend('aer_simulator')
|
| 78 |
circuit = QuantumCircuit(1, 1)
|
|
@@ -128,59 +138,76 @@ def compare_original_vs_noisy(key_str):
|
|
| 128 |
# MAIN INTERFACE
|
| 129 |
def create_interface():
|
| 130 |
with gr.Blocks(title="Quantum Cybersecurity Suite") as demo:
|
| 131 |
-
gr.Markdown("#
|
| 132 |
|
| 133 |
qkd_key_state = gr.State("")
|
| 134 |
|
| 135 |
-
|
| 136 |
-
|
|
|
|
| 137 |
qkd_btn = gr.Button("Generate Quantum Key")
|
| 138 |
qkd_output = gr.Textbox(label="Generated QKD Key", interactive=True)
|
| 139 |
-
copy_qkd_btn = gr.Button("
|
| 140 |
|
| 141 |
def generate_and_store_key(length):
|
| 142 |
key = generate_qkd_key(length)
|
| 143 |
return key, key
|
| 144 |
-
|
| 145 |
qkd_btn.click(generate_and_store_key, inputs=[key_length], outputs=[qkd_output, qkd_key_state])
|
| 146 |
-
copy_qkd_btn.click(None,
|
|
|
|
| 147 |
|
| 148 |
-
|
|
|
|
| 149 |
msg_input = gr.Textbox(label="Message to Encrypt")
|
| 150 |
encrypt_btn = gr.Button("Encrypt with QKD Key")
|
| 151 |
encrypted_output = gr.Textbox(label="Encrypted Message", interactive=True)
|
| 152 |
key_used_output = gr.Textbox(label="Fernet Key Used", interactive=True)
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
encrypted_input = gr.Textbox(label="Encrypted Message")
|
| 162 |
key_input = gr.Textbox(label="Fernet Key")
|
| 163 |
decrypt_btn = gr.Button("Decrypt")
|
| 164 |
decrypted_output = gr.Textbox(label="Decrypted Message", interactive=True)
|
| 165 |
-
copy_dec_btn = gr.Button("
|
| 166 |
-
|
| 167 |
-
decrypt_btn.click(fn=quantum_decrypt,
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
data_input = gr.Textbox(label="Data Stream (e.g. 0.3,0.7)")
|
| 172 |
detect_btn = gr.Button("Analyze")
|
| 173 |
detection_output = gr.Textbox(label="Detection Result")
|
| 174 |
|
| 175 |
-
detect_btn.click(quantum_intrusion_detection,
|
|
|
|
|
|
|
| 176 |
|
| 177 |
-
|
|
|
|
| 178 |
binary_input = gr.Textbox(label="Enter QKD Key (binary)", lines=3)
|
| 179 |
visualize_btn = gr.Button("Plot Bit Distribution")
|
| 180 |
graph_output = gr.Image(label="Bit Graph")
|
| 181 |
visualize_btn.click(plot_key_bits, inputs=[binary_input], outputs=[graph_output])
|
| 182 |
|
| 183 |
-
|
|
|
|
| 184 |
original_input = gr.Textbox(label="Enter QKD Key (binary)", lines=3)
|
| 185 |
noise_btn = gr.Button("Simulate Eavesdropper")
|
| 186 |
noise_graph = gr.Image(label="Original vs Noisy Key")
|
|
@@ -188,6 +215,7 @@ def create_interface():
|
|
| 188 |
|
| 189 |
return demo
|
| 190 |
|
|
|
|
| 191 |
if __name__ == "__main__":
|
| 192 |
demo = create_interface()
|
| 193 |
-
demo.launch(share=True)
|
|
|
|
| 9 |
import matplotlib.pyplot as plt
|
| 10 |
import io
|
| 11 |
from PIL import Image
|
| 12 |
+
import qrcode
|
| 13 |
|
| 14 |
# 1. Quantum Key Distribution (BB84 Protocol)
|
| 15 |
def generate_qkd_key(length=128):
|
|
|
|
| 34 |
raise ValueError("Generated Fernet key is invalid.")
|
| 35 |
return fernet_key
|
| 36 |
|
| 37 |
+
# 3. Generate QR Image of the Key
|
| 38 |
+
def generate_qr_image(data):
|
| 39 |
+
qr = qrcode.QRCode(version=1, box_size=8, border=4)
|
| 40 |
+
qr.add_data(data)
|
| 41 |
+
qr.make(fit=True)
|
| 42 |
+
img = qr.make_image(fill_color="black", back_color="white")
|
| 43 |
+
return img
|
| 44 |
+
|
| 45 |
+
# 4. Encrypt message using QKD key
|
| 46 |
def quantum_encrypt(message, qkd_key):
|
| 47 |
+
if not qkd_key or len(qkd_key) < 72:
|
| 48 |
+
return "β QKD key too short. Minimum 72 bits required.", "", None
|
| 49 |
try:
|
| 50 |
fernet_key = bb84_to_fernet_key(qkd_key)
|
| 51 |
f = Fernet(fernet_key)
|
| 52 |
encrypted_message = f.encrypt(message.encode())
|
| 53 |
+
qr_img = generate_qr_image(fernet_key.decode())
|
| 54 |
+
return encrypted_message.decode(), fernet_key.decode(), qr_img
|
| 55 |
except Exception as e:
|
| 56 |
return f"Encryption error: {str(e)}", "", None
|
| 57 |
|
| 58 |
+
# 5. Decrypt message using QKD key
|
| 59 |
def quantum_decrypt(encrypted_message, key):
|
| 60 |
try:
|
| 61 |
f = Fernet(key.encode())
|
|
|
|
| 64 |
except Exception as e:
|
| 65 |
return f"Decryption error: {str(e)}"
|
| 66 |
|
| 67 |
+
# 6. Quantum Intrusion Detection using PennyLane
|
| 68 |
def quantum_intrusion_detection(data_stream):
|
| 69 |
dev = qml.device("default.qubit", wires=2)
|
| 70 |
|
|
|
|
| 78 |
try:
|
| 79 |
x = [float(i) for i in data_stream.split(',')]
|
| 80 |
anomaly_score = abs(quantum_classifier(x))
|
| 81 |
+
return "π¨ Intrusion Detected" if anomaly_score > 0.7 else "β
Normal Activity"
|
| 82 |
except:
|
| 83 |
+
return "β Invalid input. Please enter two comma-separated numbers like 0.3,0.7"
|
| 84 |
|
| 85 |
+
# 7. Quantum Random Number Generator
|
| 86 |
def quantum_random_number():
|
| 87 |
backend = Aer.get_backend('aer_simulator')
|
| 88 |
circuit = QuantumCircuit(1, 1)
|
|
|
|
| 138 |
# MAIN INTERFACE
|
| 139 |
def create_interface():
|
| 140 |
with gr.Blocks(title="Quantum Cybersecurity Suite") as demo:
|
| 141 |
+
gr.Markdown("# π‘οΈ Quantum Cybersecurity Suite")
|
| 142 |
|
| 143 |
qkd_key_state = gr.State("")
|
| 144 |
|
| 145 |
+
# Tab 1: QKD Key Generation
|
| 146 |
+
with gr.Tab("1οΈβ£ Quantum Key Distribution"):
|
| 147 |
+
key_length = gr.Slider(72, 512, step=8, value=128, label="Select QKD Key Length (β₯ 72 bits)")
|
| 148 |
qkd_btn = gr.Button("Generate Quantum Key")
|
| 149 |
qkd_output = gr.Textbox(label="Generated QKD Key", interactive=True)
|
| 150 |
+
copy_qkd_btn = gr.Button("π Copy QKD Key")
|
| 151 |
|
| 152 |
def generate_and_store_key(length):
|
| 153 |
key = generate_qkd_key(length)
|
| 154 |
return key, key
|
| 155 |
+
|
| 156 |
qkd_btn.click(generate_and_store_key, inputs=[key_length], outputs=[qkd_output, qkd_key_state])
|
| 157 |
+
copy_qkd_btn.click(None, inputs=[], outputs=[],
|
| 158 |
+
js="() => navigator.clipboard.writeText(document.querySelector('textarea[aria-label=\"Generated QKD Key\"]').value)")
|
| 159 |
|
| 160 |
+
# Tab 2: Quantum Encryption
|
| 161 |
+
with gr.Tab("2οΈβ£ Quantum Encryption"):
|
| 162 |
msg_input = gr.Textbox(label="Message to Encrypt")
|
| 163 |
encrypt_btn = gr.Button("Encrypt with QKD Key")
|
| 164 |
encrypted_output = gr.Textbox(label="Encrypted Message", interactive=True)
|
| 165 |
key_used_output = gr.Textbox(label="Fernet Key Used", interactive=True)
|
| 166 |
+
qr_output = gr.Image(label="QR of Fernet Key")
|
| 167 |
+
copy_enc_btn = gr.Button("π Copy Encrypted Message")
|
| 168 |
+
copy_key_btn = gr.Button("π Copy Fernet Key")
|
| 169 |
+
|
| 170 |
+
encrypt_btn.click(quantum_encrypt,
|
| 171 |
+
inputs=[msg_input, qkd_key_state],
|
| 172 |
+
outputs=[encrypted_output, key_used_output, qr_output])
|
| 173 |
+
copy_enc_btn.click(None, inputs=[], outputs=[],
|
| 174 |
+
js="() => navigator.clipboard.writeText(document.querySelector('textarea[aria-label=\"Encrypted Message\"]').value)")
|
| 175 |
+
copy_key_btn.click(None, inputs=[], outputs=[],
|
| 176 |
+
js="() => navigator.clipboard.writeText(document.querySelector('textarea[aria-label=\"Fernet Key Used\"]').value)")
|
| 177 |
+
|
| 178 |
+
# Tab 3: Quantum Decryption
|
| 179 |
+
with gr.Tab("3οΈβ£ Quantum Decryption"):
|
| 180 |
encrypted_input = gr.Textbox(label="Encrypted Message")
|
| 181 |
key_input = gr.Textbox(label="Fernet Key")
|
| 182 |
decrypt_btn = gr.Button("Decrypt")
|
| 183 |
decrypted_output = gr.Textbox(label="Decrypted Message", interactive=True)
|
| 184 |
+
copy_dec_btn = gr.Button("π Copy Decrypted Message")
|
| 185 |
+
|
| 186 |
+
decrypt_btn.click(fn=quantum_decrypt,
|
| 187 |
+
inputs=[encrypted_input, key_input],
|
| 188 |
+
outputs=[decrypted_output])
|
| 189 |
+
copy_dec_btn.click(None, inputs=[], outputs=[],
|
| 190 |
+
js="() => navigator.clipboard.writeText(document.querySelector('textarea[aria-label=\"Decrypted Message\"]').value)")
|
| 191 |
+
|
| 192 |
+
# Tab 4: Intrusion Detection
|
| 193 |
+
with gr.Tab("4οΈβ£ Quantum Intrusion Detection"):
|
| 194 |
data_input = gr.Textbox(label="Data Stream (e.g. 0.3,0.7)")
|
| 195 |
detect_btn = gr.Button("Analyze")
|
| 196 |
detection_output = gr.Textbox(label="Detection Result")
|
| 197 |
|
| 198 |
+
detect_btn.click(quantum_intrusion_detection,
|
| 199 |
+
inputs=[data_input],
|
| 200 |
+
outputs=[detection_output])
|
| 201 |
|
| 202 |
+
# Tab 5: QKD Key Bit Distribution
|
| 203 |
+
with gr.Tab("π QKD Key Randomness Visualizer"):
|
| 204 |
binary_input = gr.Textbox(label="Enter QKD Key (binary)", lines=3)
|
| 205 |
visualize_btn = gr.Button("Plot Bit Distribution")
|
| 206 |
graph_output = gr.Image(label="Bit Graph")
|
| 207 |
visualize_btn.click(plot_key_bits, inputs=[binary_input], outputs=[graph_output])
|
| 208 |
|
| 209 |
+
# Tab 6: Eavesdropper Noise Simulation
|
| 210 |
+
with gr.Tab("π Eavesdropper Noise Simulation"):
|
| 211 |
original_input = gr.Textbox(label="Enter QKD Key (binary)", lines=3)
|
| 212 |
noise_btn = gr.Button("Simulate Eavesdropper")
|
| 213 |
noise_graph = gr.Image(label="Original vs Noisy Key")
|
|
|
|
| 215 |
|
| 216 |
return demo
|
| 217 |
|
| 218 |
+
# Launch
|
| 219 |
if __name__ == "__main__":
|
| 220 |
demo = create_interface()
|
| 221 |
+
demo.launch(share=True)
|