-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbob.py
64 lines (51 loc) · 1.75 KB
/
bob.py
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
from bb84.bb84 import *
from BitVector import BitVector
enc_key = None
"""
with CQCConnection("Bob") as Bob:
print("Connection made")
# Receive lenth and initialize varaiables
length = int.from_bytes(Bob.recvClassical(), byteorder="big")
key_length = length / 3
Bob.sendClassical("Alice", length)
print(length)
qubits = [None] * length
for i in range(length):
qubits[i] = Bob.recvQubit()
key, bases = measure_random(qubits)
print("Key: {}".format(bin(key)))
print("Bases: {}".format(bin(bases.int_val())))
# Since we can only send indiviual numbers from 0-256,
# we have to split this up into a list of digits.
# Use slice to extract list from bitvector
Bob.sendClassical("Alice", bases[:])
correct_bases = BitVector(bitlist=Bob.recvClassical())
print("Correct: {}".format(bin(int(correct_bases))))
# Remove all incorrectly measured bits
key = truncate_key(key, length, correct_bases)
# Break into verification bits and final key
verification_bits, key = break_into_parts(key, key_length)
Bob.sendClassical("Alice", verification_bits[:])
enc_key = key
print("awaiting OK")
response = Bob.recvClassical()
if response == bytes(OK):
print("Key OK to use")
elif response == bytes(TAMPERED):
print("Key compromised!")
pass
print("Message recieved")
Bob.sendClassical("Alice", OK)
"""
print("Calling target keygen")
key = target_keygen()
print(key)
# Test decrypt message
with CQCConnection("Bob") as Bob:
print("Awaiting encrypted message")
# capture message header
encrypted = Bob.recvClassical()
print("Message recieved")
message = decrypt(encrypted, int(key))
print("Message: ")
print(message)