Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import pennylane as qml
|
| 4 |
+
from qiskit import QuantumCircuit
|
| 5 |
+
from qiskit_aer import Aer
|
| 6 |
+
from qiskit.primitives import Sampler
|
| 7 |
+
from cryptography.fernet import Fernet
|
| 8 |
+
import random
|
| 9 |
+
import base64
|
| 10 |
+
|
| 11 |
+
# 1. Quantum Key Distribution (BB84 Protocol)
|
| 12 |
+
def generate_qkd_key(length=8):
|
| 13 |
+
alice_bits = [random.randint(0, 1) for _ in range(length)]
|
| 14 |
+
alice_bases = [random.randint(0, 1) for _ in range(length)]
|
| 15 |
+
bob_bases = [random.randint(0, 1) for _ in range(length)]
|
| 16 |
+
|
| 17 |
+
shared_key = ""
|
| 18 |
+
for i in range(length):
|
| 19 |
+
if alice_bases[i] == bob_bases[i]:
|
| 20 |
+
shared_key += str(alice_bits[i])
|
| 21 |
+
return shared_key
|
| 22 |
+
|
| 23 |
+
# 2. Convert QKD binary key to Fernet key
|
| 24 |
+
def bb84_to_fernet_key(qkey_binary_str):
|
| 25 |
+
padded = qkey_binary_str.ljust(128, '0') # pad to 128 bits
|
| 26 |
+
int_val = int(padded, 2)
|
| 27 |
+
byte_val = int_val.to_bytes(16, 'big') # 128 bits = 16 bytes
|
| 28 |
+
fernet_key = base64.urlsafe_b64encode(byte_val)
|
| 29 |
+
return fernet_key
|
| 30 |
+
|
| 31 |
+
# 3. Encryption using QKD key
|
| 32 |
+
def quantum_encrypt(message, qkd_key):
|
| 33 |
+
if not qkd_key or len(qkd_key) < 8:
|
| 34 |
+
return "QKD key not generated or too short", ""
|
| 35 |
+
try:
|
| 36 |
+
fernet_key = bb84_to_fernet_key(qkd_key)
|
| 37 |
+
f = Fernet(fernet_key)
|
| 38 |
+
encrypted_message = f.encrypt(message.encode())
|
| 39 |
+
return encrypted_message.decode(), fernet_key.decode()
|
| 40 |
+
except Exception as e:
|
| 41 |
+
return f"Encryption error: {str(e)}", ""
|
| 42 |
+
|
| 43 |
+
# 4. Decryption using QKD key
|
| 44 |
+
def quantum_decrypt(encrypted_message, key):
|
| 45 |
+
try:
|
| 46 |
+
f = Fernet(key.encode())
|
| 47 |
+
decrypted_message = f.decrypt(encrypted_message.encode())
|
| 48 |
+
return decrypted_message.decode()
|
| 49 |
+
except Exception as e:
|
| 50 |
+
return f"Decryption error: {str(e)}"
|
| 51 |
+
|
| 52 |
+
# 5. Quantum Intrusion Detection
|
| 53 |
+
def quantum_intrusion_detection(data_stream):
|
| 54 |
+
dev = qml.device("default.qubit", wires=2)
|
| 55 |
+
|
| 56 |
+
@qml.qnode(dev)
|
| 57 |
+
def quantum_classifier(x):
|
| 58 |
+
qml.RY(x[0], wires=0)
|
| 59 |
+
qml.RY(x[1], wires=1)
|
| 60 |
+
qml.CNOT(wires=[0, 1])
|
| 61 |
+
return qml.expval(qml.PauliZ(0))
|
| 62 |
+
|
| 63 |
+
try:
|
| 64 |
+
x = [float(i) for i in data_stream.split(',')]
|
| 65 |
+
anomaly_score = abs(quantum_classifier(x))
|
| 66 |
+
return "Intrusion Detected" if anomaly_score > 0.7 else "Normal Activity"
|
| 67 |
+
except:
|
| 68 |
+
return "Invalid input. Please enter two comma-separated numbers like 0.3,0.7"
|
| 69 |
+
|
| 70 |
+
# 6. Gradio Interface
|
| 71 |
+
def create_interface():
|
| 72 |
+
with gr.Blocks(title="Quantum Cybersecurity Suite") as demo:
|
| 73 |
+
gr.Markdown("# 🔐 Quantum Computing for Cyber Defense")
|
| 74 |
+
|
| 75 |
+
# State to store QKD key across tabs
|
| 76 |
+
qkd_key_state = gr.State("")
|
| 77 |
+
|
| 78 |
+
# Tab 1: QKD
|
| 79 |
+
with gr.Tab("1️⃣ Quantum Key Distribution"):
|
| 80 |
+
key_length = gr.Slider(minimum=8, maximum=32, step=8, label="Select Key Length")
|
| 81 |
+
qkd_btn = gr.Button("Generate Quantum Key")
|
| 82 |
+
qkd_output = gr.Textbox(label="Generated QKD Key")
|
| 83 |
+
|
| 84 |
+
def generate_and_store_key(length):
|
| 85 |
+
key = generate_qkd_key(length)
|
| 86 |
+
return key, key # update both output and state
|
| 87 |
+
|
| 88 |
+
qkd_btn.click(fn=generate_and_store_key, inputs=[key_length],
|
| 89 |
+
outputs=[qkd_output, qkd_key_state])
|
| 90 |
+
|
| 91 |
+
# Tab 2: Quantum Encryption
|
| 92 |
+
with gr.Tab("2️⃣ Quantum Encryption"):
|
| 93 |
+
msg_input = gr.Textbox(label="Message to Encrypt")
|
| 94 |
+
encrypt_btn = gr.Button("Encrypt with QKD Key")
|
| 95 |
+
encrypted_output = gr.Textbox(label="Encrypted Message")
|
| 96 |
+
key_used_output = gr.Textbox(label="Fernet Key Used")
|
| 97 |
+
|
| 98 |
+
encrypt_btn.click(fn=quantum_encrypt,
|
| 99 |
+
inputs=[msg_input, qkd_key_state],
|
| 100 |
+
outputs=[encrypted_output, key_used_output])
|
| 101 |
+
|
| 102 |
+
# Tab 3: Quantum Decryption
|
| 103 |
+
with gr.Tab("3️⃣ Quantum Decryption"):
|
| 104 |
+
encrypted_input = gr.Textbox(label="Encrypted Message")
|
| 105 |
+
key_input = gr.Textbox(label="Fernet Key")
|
| 106 |
+
decrypt_btn = gr.Button("Decrypt")
|
| 107 |
+
decrypted_output = gr.Textbox(label="Decrypted Message")
|
| 108 |
+
|
| 109 |
+
decrypt_btn.click(fn=quantum_decrypt,
|
| 110 |
+
inputs=[encrypted_input, key_input],
|
| 111 |
+
outputs=[decrypted_output])
|
| 112 |
+
|
| 113 |
+
# Tab 4: Quantum Intrusion Detection
|
| 114 |
+
with gr.Tab("4️⃣ Quantum Intrusion Detection"):
|
| 115 |
+
data_input = gr.Textbox(label="Data Stream (comma-separated, e.g. 0.3,0.7)")
|
| 116 |
+
detect_btn = gr.Button("Analyze")
|
| 117 |
+
detection_output = gr.Textbox(label="Detection Result")
|
| 118 |
+
|
| 119 |
+
detect_btn.click(quantum_intrusion_detection,
|
| 120 |
+
inputs=[data_input],
|
| 121 |
+
outputs=[detection_output])
|
| 122 |
+
|
| 123 |
+
return demo
|
| 124 |
+
|
| 125 |
+
# 7. Launch
|
| 126 |
+
if __name__ == "__main__":
|
| 127 |
+
demo = create_interface()
|
| 128 |
+
demo.launch(share=True)
|