Added MiFare Classic 4K support
This commit is contained in:
79
write.py
79
write.py
@@ -8,9 +8,6 @@ DEFAULT_KEY = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
|
|||||||
# Selecting key
|
# Selecting key
|
||||||
KEY = DEFAULT_KEY
|
KEY = DEFAULT_KEY
|
||||||
|
|
||||||
MainLoop = True
|
|
||||||
WaitingForTag = True
|
|
||||||
|
|
||||||
def uid_to_num(uid):
|
def uid_to_num(uid):
|
||||||
n = 0
|
n = 0
|
||||||
for i in range(0, 5):
|
for i in range(0, 5):
|
||||||
@@ -25,52 +22,66 @@ print "Info: Leave the sector field empty to exit.\n"
|
|||||||
# Get tag size if available
|
# Get tag size if available
|
||||||
(status, TagSize) = RFID.Request(RFID.PICC_REQIDL)
|
(status, TagSize) = RFID.Request(RFID.PICC_REQIDL)
|
||||||
|
|
||||||
while MainLoop:
|
while True:
|
||||||
if TagSize > 0:
|
if TagSize > 0:
|
||||||
message = "Sector [1 - %s]: " % TagSize
|
message = "Sector [1 - %s]: " % (TagSize - 1)
|
||||||
else:
|
else:
|
||||||
message = "Sector: "
|
message = "Sector: "
|
||||||
|
|
||||||
try:
|
try:
|
||||||
sector = input(message)
|
Sector = input(message)
|
||||||
except:
|
except:
|
||||||
MainLoop = False
|
|
||||||
continue
|
|
||||||
else:
|
|
||||||
try:
|
|
||||||
text = raw_input("Data: ")
|
|
||||||
except:
|
|
||||||
continue
|
|
||||||
else:
|
|
||||||
if status != RFID.MI_OK:
|
|
||||||
print "Waiting for Tag...\n"
|
|
||||||
else:
|
|
||||||
print ""
|
print ""
|
||||||
WaitingForTag = True
|
break
|
||||||
|
else:
|
||||||
|
if TagSize > 0:
|
||||||
|
if Sector >= 32:
|
||||||
|
MaxChars = 16 * 15
|
||||||
|
else:
|
||||||
|
MaxChars = 16 * 3
|
||||||
|
message = "Data [max %s chars]: " % MaxChars
|
||||||
|
else:
|
||||||
|
message = "Data: "
|
||||||
|
try:
|
||||||
|
text = raw_input(message)
|
||||||
|
except:
|
||||||
|
print "\n"
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
print "Waiting for Tag...\n"
|
||||||
|
|
||||||
while WaitingForTag:
|
while True:
|
||||||
(status, TagSize) = RFID.Request(RFID.PICC_REQIDL)
|
(status, TagSize) = RFID.Request(RFID.PICC_REQIDL)
|
||||||
|
|
||||||
if status != RFID.MI_OK:
|
if status != RFID.MI_OK:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if sector < 1 or sector > (TagSize - 1):
|
if Sector < 1 or Sector > (TagSize - 1):
|
||||||
print "Sector out of range (1 - %s)\n" % (TagSize - 1)
|
print "Sector out of range (1 - %s)\n" % (TagSize - 1)
|
||||||
WaitingForTag = False
|
break
|
||||||
continue
|
|
||||||
|
|
||||||
(status, UID) = RFID.Anticoll()
|
|
||||||
|
|
||||||
if status != RFID.MI_OK:
|
|
||||||
continue
|
|
||||||
|
|
||||||
RFID.SelectTag(UID)
|
|
||||||
|
|
||||||
# Selecting blocks
|
# Selecting blocks
|
||||||
BlockAddrs = [ (sector * 4), (sector * 4 + 1), (sector * 4 + 2) ]
|
BaseBlockLength = 4
|
||||||
TrailerBlockAddr = (sector * 4 + 3)
|
if Sector < 32:
|
||||||
|
BlockLength = BaseBlockLength
|
||||||
|
StartAddr = Sector * BlockLength
|
||||||
|
else:
|
||||||
|
BlockLength = 16
|
||||||
|
StartAddr = 32 * BaseBlockLength + (Sector - 32) * BlockLength
|
||||||
|
|
||||||
|
BlockAddrs = []
|
||||||
|
for i in range(0, (BlockLength - 1)):
|
||||||
|
BlockAddrs.append((StartAddr + i))
|
||||||
|
TrailerBlockAddr = (StartAddr + (BlockLength - 1))
|
||||||
|
|
||||||
|
# Initializing tag
|
||||||
|
(Status, UID) = RFID.Anticoll()
|
||||||
|
|
||||||
|
if Status != RFID.MI_OK:
|
||||||
|
break
|
||||||
|
|
||||||
# Writing data
|
# Writing data
|
||||||
|
RFID.SelectTag(UID)
|
||||||
status = RFID.Auth(RFID.PICC_AUTHENT1A, TrailerBlockAddr, KEY, UID)
|
status = RFID.Auth(RFID.PICC_AUTHENT1A, TrailerBlockAddr, KEY, UID)
|
||||||
if status == RFID.MI_OK:
|
if status == RFID.MI_OK:
|
||||||
data = bytearray()
|
data = bytearray()
|
||||||
@@ -82,9 +93,9 @@ while MainLoop:
|
|||||||
print "UID: ", uid_to_num(UID)
|
print "UID: ", uid_to_num(UID)
|
||||||
print "Data: ", text[0:(len(BlockAddrs) * 16)], "\n"
|
print "Data: ", text[0:(len(BlockAddrs) * 16)], "\n"
|
||||||
else:
|
else:
|
||||||
print "Can't access sector", sector, "!\n"
|
print "Can't access sector", Sector, "!\n"
|
||||||
RFID.StopCrypto1()
|
RFID.StopCrypto1()
|
||||||
|
break
|
||||||
|
|
||||||
WaitingForTag = False
|
RFID.AntennaOff()
|
||||||
|
|
||||||
GPIO.cleanup()
|
GPIO.cleanup()
|
||||||
Reference in New Issue
Block a user