-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver_cli.py
More file actions
132 lines (88 loc) · 2.76 KB
/
server_cli.py
File metadata and controls
132 lines (88 loc) · 2.76 KB
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
import argparse
parser = argparse.ArgumentParser()
backend = parser.add_mutually_exclusive_group()
backend.add_argument("-1", dest='b1', action='store_true', help='Use qmachine1')
backend.add_argument("-2", dest='b2', action='store_true', help='Use qmachine2')
parser.add_argument("-g", dest='gui', action='store_true', help='For GUI')
parser.add_argument("-s", dest='show', action='store_true', help='Show Qcircuit')
parser.add_argument("-p", dest='plot', action='store_true', help='Plot Qcir result')
args = parser.parse_args()
print("I> Loading Modules ...")
import sys
import random
import signal
from quantum import *
from crypt0graphy import *
from server import *
print("I> All Modules Loaded")
### loading IBM Q account
if args.b1 or args.b2:
print("I> Loading IBM Q account ...")
qiskit_.qaccount = IBMQ.load_account()
qiskit_.qbackend1 = qiskit_.qaccount.get_backend('ibmq_bogota')
qiskit_.qbackend2 = qiskit_.qaccount.get_backend('ibmq_belem')
print("I> IBM Q account loaded")
### quantum code
qcir = get_qcir()
if args.show:
show(qcir) # matplotlib window
if args.b1:
r = output(qcir, 1)
elif args.b2:
r = output(qcir, 2)
else:
r = output(qcir)
if args.plot:
plot(r) # matplotlib window
if args.show or args.plot:
#def exception(*a): raise Exception
#signal.signal(signal.SIGALRM, exception)
#signal.alarm(2)
try: plt.show()
except: print("I> Matplotlib Windows shown")
else: print("I> Matplotlib windows shown")
print("I> Quantum Code done")
### seeding randomess
seed = U.long_to_bytes(num(r))
random.seed(seed)
print("Seed :", seed)
print("I> Seeding randomess done")
### server-client
print("I> Waiting for client to connect ...")
c = client()
print("I> Connected to client")
### sharing pure random key for AES
print("I> Sharing pure randomness ...")
crypto.pubrsakey = './RSAkey/client/public'
crypto.prirsakey = './RSAkey/server/private'
c.sendall(DHs1())
rk = DHs2(c.recv(1024))
print("Shared Key :", U.long_to_bytes(rk))
print("I> Shared by diffie-hellman")
key = SHA256.new(U.long_to_bytes(rk))
#### GUI
if args.gui:
import server_gui
server_gui.main()
s.close()
c.close()
plt.close()
sys.exit()
#### CLI
while True:
edata = c.recv(1024)
nonce, edata = edata[:16], edata[16:]
aes = AES.new(key.digest(), AES.MODE_EAX, nonce=nonce)
data = aes.decrypt(edata).decode()
if data.strip() == "Exit":
break
print("Data from client :", data.strip())
data = input("Response to client : ").encode()
aes = AES.new(key.digest(), AES.MODE_EAX)
c.sendall(aes.nonce+aes.encrypt(data))
if data.strip() == b'Exit': break
s.close()
c.close()
plt.close()
print("I> Socket closed")
print("I> Quiting...")