Added MiFare Classic 4K support

This commit is contained in:
2018-07-29 03:31:55 +02:00
parent d6e6db0f85
commit e7513c25ad

74
read.py
View File

@@ -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):
@@ -23,51 +20,64 @@ print "# RFID Reader\n"
print "Info: Leave the sector field empty to exit.\n" 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 True:
while MainLoop:
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 print ""
continue break
else: else:
if status != RFID.MI_OK: print "Waiting for Tag...\n"
print "Waiting for Tag...\n"
else:
print ""
WaitingForTag = True
while WaitingForTag: while True:
(status, TagSize) = RFID.Request(RFID.PICC_REQIDL)
if status != RFID.MI_OK: (Status, TagSize) = RFID.Request(RFID.PICC_REQIDL)
if Status != RFID.MI_OK:
continue continue
if sector < 1 or sector > (TagSize - 1): if TagSize < 1:
print("Can't read tag properly!")
break
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() # Selecting blocks
BaseBlockLength = 4
if Sector < 32:
BlockLength = BaseBlockLength
StartAddr = Sector * BlockLength
else:
BlockLength = 16
StartAddr = 32 * BaseBlockLength + (Sector - 32) * BlockLength
if status != RFID.MI_OK: BlockAddrs = []
continue 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
# Reading sector
RFID.SelectTag(UID) RFID.SelectTag(UID)
Status = RFID.Auth(RFID.PICC_AUTHENT1A, TrailerBlockAddr, KEY, UID)
# Reading data
BlockAddrs = [ (sector * 4), (sector * 4 + 1), (sector * 4 + 2) ]
TrailerBlockAddr = (sector * 4 + 3)
status = RFID.Auth(RFID.PICC_AUTHENT1A, TrailerBlockAddr, KEY, UID)
data = [] data = []
text_read = "" text_read = ""
if status == RFID.MI_OK: if Status == RFID.MI_OK:
for block_num in BlockAddrs: for block_num in BlockAddrs:
block = RFID.Read(block_num) block = RFID.Read(block_num)
if block: if block:
@@ -77,9 +87,9 @@ while MainLoop:
print "UID: ", uid_to_num(UID) print "UID: ", uid_to_num(UID)
print "Data: ", text_read,"\n" print "Data: ", text_read,"\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()