File size: 5,448 Bytes
0173bbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import gradio as gr
from src.bb84 import BB84Protocol
from src.e91 import E91Protocol
from src.circuit_viz import CircuitVisualizer
import numpy as np
import uuid

class QuantumCryptApp:
    """Interface gráfica para o QuantumCrypt usando Gradio."""
    
    def __init__(self):
        self.bb84 = BB84Protocol()
        self.e91 = E91Protocol()
        
    def create_interface(self):
        """Cria a interface Gradio."""
        
        with gr.Blocks(title="QuantumCrypt", css="footer{display:none !important}") as interface:
            gr.Markdown("# QuantumCrypt")
            
            with gr.Tabs():
                with gr.Tab("BB84"):
                    with gr.Row():
                        with gr.Column():
                            key_length = gr.Slider(
                                minimum=8, maximum=256, value=64,
                                label="Tamanho da Chave"
                            )
                            error_correction = gr.Checkbox(
                                label="Usar Correção de Erros",
                                visible=False
                            )
                            generate_btn = gr.Button("Gerar Chave BB84")

                        with gr.Column():
                            
                            output_text = gr.TextArea(label="Resultado")
                            circuit_viz = gr.Image(label="Visualização do Circuito", 
                                                   show_download_button=False, 
                                                   show_fullscreen_button=False,
                                                   show_share_button=False)
                
                    
                with gr.Tab("E91"):
                    with gr.Row():
                        with gr.Column():
                            key_length_e91 = gr.Slider(
                                minimum=8, maximum=256, value=64,
                                label="Tamanho da Chave"
                            )
                            error_correction_e91 = gr.Checkbox(
                                label="Usar Correção de Erros",
                                visible=False
                            )
                            generate_btn_e91 = gr.Button("Gerar Chave BB84")

                        with gr.Column():
                            
                            output_text_e91 = gr.TextArea(label="Resultado")
                            circuit_viz_e91 = gr.Image(label="Visualização do Circuito", 
                                                   show_download_button=False, 
                                                   show_fullscreen_button=False,
                                                   show_share_button=False)
                
                with gr.Accordion(label="About", open=False):
                        with open("README.md", "r") as readme_file:
                            gr.Markdown(
                            readme_file.read()
                            )
            
            def generate_bb84(length, use_correction):
                filename = f"data/{uuid.uuid4()}.png"
                protocol = BB84Protocol(
                    key_length=length,
                    error_correction=use_correction
                )
                result = protocol.generate_key()
                
                # Gerar visualização
                circuit_img = CircuitVisualizer.draw_circuit(
                    protocol.current_circuit,
                    filename=filename
                )
                
                # Formatar resultado
                output = (
                    f"Chave Alice: {''.join(map(str, result['alice']))}\n"
                    f"Chave Bob: {''.join(map(str, result['bob']))}\n"
                    f"Tamanho: {len(result['alice'])} bits\n"
                    f"Correção de Erros: {'Ativada' if use_correction else 'Desativada'}"
                )
                
                return output, filename
            
            def generate_e91(length):
                filename = f"data/{uuid.uuid4()}.png"
                protocol = E91Protocol(
                    key_length=length
                )
                result = protocol.generate_key()
                
                # Gerar visualização
                circuit_img = CircuitVisualizer.draw_circuit(
                    protocol.current_circuit,
                    filename=filename
                )
                
                # Formatar resultado
                output = (
                    f"Chave Alice: {''.join(map(str, result['alice']))}\n"
                    f"Chave Bob: {''.join(map(str, result['bob']))}\n"
                    f"Tamanho: {len(result['alice'])} bits\n"
                )
                
                return output, filename
            
            generate_btn_e91.click(
                generate_e91,
                inputs=[key_length_e91],
                outputs=[output_text_e91, circuit_viz_e91]
            )

            generate_btn.click(
                generate_bb84,
                inputs=[key_length, error_correction],
                outputs=[output_text, circuit_viz]
            )
            
        return interface

# Exemplo de uso
if __name__ == "__main__":
    app = QuantumCryptApp()
    interface = app.create_interface()
    interface.launch()