-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrite.py
More file actions
58 lines (45 loc) · 1.47 KB
/
write.py
File metadata and controls
58 lines (45 loc) · 1.47 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
import nxppy
import time
import ndef
import sys
import time
#All lengths are in bytes
NTAG_BLOCK_LENGTH = 4
if len(sys.argv) > 1:
content = ""
pwd = ""
hexAddr = sys.argv[1]
addr = int(hexAddr, 16)
if len(sys.argv) > 2:
try:
content = str(sys.argv[2]).decode("hex")
except TypeError:
print 'Invalid content'
exit()
if len(sys.argv) > 3:
pwd = str(sys.argv[3])
mifare = nxppy.Mifare()
try:
uid = mifare.select()
print 'UID: ' + uid
if pwd != "":
mifare.pwd_auth(pwd)
for x in xrange(len(content) / NTAG_BLOCK_LENGTH):
data = content[x * NTAG_BLOCK_LENGTH:(x + 1) * NTAG_BLOCK_LENGTH]
mifare.write_block(addr + x, data)
except nxppy.SelectError:
print 'No card detected'
pass
except nxppy.AuthError:
# SelectError is raised if authentication was unsuccessful
print 'Authentication unsuccessful'
pass
except nxppy.WriteError:
# SelectError is raised if authentication was unsuccessful
print 'Write error. May be caused by invalid password'
pass
else:
print 'Usage: python ' + sys.argv[0] + ' <startingAddr> [<content> [<pwd>]]'
print 'startingAddr: Starting address in hex'
print 'content: Write content to tag. Input hex string.'
print 'pwd: Password'