-
Notifications
You must be signed in to change notification settings - Fork 2
/
testQkd.py
93 lines (69 loc) · 2.81 KB
/
testQkd.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
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
from nacl import utils, secret
from qkd import generateQK
# Generate Alice and Bob keys
# The symmetric key for both of them are produced by the quantum circuit
print('[*]Generating 256 bits quantum symmetric keys: without spy')
qkeyarray = generateQK(256)
print('Key error counter: ', qkeyarray['errorCounter'], 'isValid?', qkeyarray['valid'])
alice_key = qkeyarray.get('A')
bob_key = qkeyarray.get('B')
# print(alice_key)
# print(bob_key)
qkey=int(alice_key, 2).to_bytes((len(alice_key)+7) // 8, byteorder='little')
qkeyb=int(bob_key, 2).to_bytes((len(bob_key)+7) // 8, byteorder='little')
print('Alice key: '+str(qkey))
print('Bob key: '+str(qkeyb))
print('================================================')
# Secret Box (Salsa20withPoly1305 algorithm)
# In this step both Alice and Bob should have simultaneously the symmetric key
box = secret.SecretBox(qkey)
boxb = secret.SecretBox(qkeyb)
# Maybe the nonce can also be generated by a quantum circuit (Team11)
nonce = utils.random(secret.SecretBox.NONCE_SIZE)
# print(nonce)
# Message to be encrypted and decrypted
message = b"CryptoQ"
print('[*] Message to be encrypted: ')
print(message)
# Alice would encrypt her message
encrypted = box.encrypt(message, nonce)
ctext = encrypted.ciphertext
print('[*] Encrypted message by A: ')
print(ctext)
# Bob would decrypt Alice's message
plain = boxb.decrypt(ctext, nonce)
print('[*] Plaintext decrypted by B: ')
print(plain)
print('\n***********************************************\n')
print('[*]Generating 256 bits quantum symmetric keys: with eavsdropper')
qkeyarray = generateQK(256, 0.5, 0.5)
print('Key error counter: ', qkeyarray['errorCounter'], 'isValid?', qkeyarray['valid'])
alice_key = qkeyarray.get('A')
bob_key = qkeyarray.get('B')
# print(alice_key)
# print(bob_key)
qkey=int(alice_key, 2).to_bytes((len(alice_key)+7) // 8, byteorder='little')
qkeyb=int(bob_key, 2).to_bytes((len(bob_key)+7) // 8, byteorder='little')
print('Alice key: '+str(qkey))
print('Bob key: '+str(qkeyb))
print('================================================')
# Secret Box (Salsa20withPoly1305 algorithm)
# In this step both Alice and Bob should have simultaneously the symmetric key
box = secret.SecretBox(qkey)
boxb = secret.SecretBox(qkeyb)
# Maybe the nonce can also be generated by a quantum circuit (Team11)
nonce = utils.random(secret.SecretBox.NONCE_SIZE)
# print(nonce)
# Message to be encrypted and decrypted
message = b"CryptoQ"
print('[*] Message to be encrypted: ')
print(message)
# Alice would encrypt her message
encrypted = box.encrypt(message, nonce)
ctext = encrypted.ciphertext
print('[*] Encrypted message by A: ')
print(ctext)
# Bob would decrypt Alice's message
plain = boxb.decrypt(ctext, nonce)
print('[*] Plaintext decrypted by B: ')
print(plain)