-
Notifications
You must be signed in to change notification settings - Fork 0
/
read.py
48 lines (32 loc) · 977 Bytes
/
read.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
import base64
import nfc.snep
import serial
from nacl.public import PublicKey, PrivateKey, Box
print('Waiting for NFC tag')
clf = nfc.ContactlessFrontend('usb')
tag = clf.connect(rdwr={'on-connect': lambda t: False})
print('Reading')
uid = None
prv = None
for record in tag.ndef.records:
if record.name == 'touchkey:uid':
uid = record.text
elif record.name == 'touchkey:prv':
prv = PrivateKey(base64.b64decode(record.text))
if uid is None or prv is None:
print('Error: Invalid tag')
exit()
print('Key ID: %s' % uid)
print('Loading key')
with open('./keys/%s.pub' % uid, 'rb') as reader:
pub = PublicKey(reader.read())
with open('./keys/%s.key' % uid, 'rb') as reader:
key = reader.read()
box = Box(prv, pub)
print('Decrypting')
plain = box.decrypt(key)
print('Sending to Arduino')
connection = serial.Serial('/dev/ttyS0', 9600)
connection.write(('%s\n' % (plain.decode())).encode())
connection.close()
print('All done!')