Tiz Faver
Tiz Faver

Reputation: 11

Python read text from NFC ACR122U

I successfully could read the UID of a NFC card, using the reader ACR122U and a simple badge bought on aliexpress.

The problem is that i can't manage to read something else. I want to read the text that is written in the tag. I wrote the text with my smartphone.

I know that you can read the UID without any key.

And i know that you need two keys to access the data. I also know that the default keys are all 0x00...

This is the code that only reads UID and works fine:

from smartcard.System import readers
from smartcard.util import toHexString
import time

def read_card():
    r = readers()
    if len(r) == 0:
        print("Nessun lettore NFC trovato.")
        return None

    reader = r[0]
    connection = reader.createConnection()

    try:
        connection.connect()
        get_uid_command = [0xFF, 0xCA, 0x00, 0x00, 0x00]
        data, sw1, sw2 = connection.transmit(get_uid_command)

        if sw1 == 0x90 and sw2 == 0x00:
            return toHexString(data)
        else:
            return None
    except Exception as e:
        print(f"Errore durante la lettura della carta: {e}")
        return None

if __name__ == "__main__":
    while True:
        print("In attesa di una carta...")
        uid = None
        while not uid:
            uid = read_card()
            if not uid:
                time.sleep(0.3)

        print(f"UID della carta: {uid}")
        print("Rimuovi la carta e passa un'altra per continuare.")
        time.sleep(2) #Pausa dopo la lettura per impedire che venga riletta la stessa card.

i and this the code that i tried but that continuosly fail the auth, giving me 63 00 error instead of 90 00.

from smartcard.System import readers
from smartcard.util import toHexString
import time

#Keys
KEYA = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]
KEYB = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]

SECTOR_NUMBER = 3  #Sector number
BLOCK_NUMBER = 12   #Block number

def authenticate_sector(connection, sector_number, key):
    auth_command = [0xFF, 0x88, 0x20, sector_number * 4, 0x60] + key
    _, sw1, sw2 = connection.transmit(auth_command)
    return sw1, sw2

def read_card():
    r = readers()
    if len(r) == 0:
        print("Nessun lettore NFC trovato.")
        return None

    reader = r[0]
    connection = reader.createConnection()

    try:
        connection.connect()

        #Attempt to authenticate with KEYA first
        sw1, sw2 = authenticate_sector(connection, SECTOR_NUMBER, KEYA)
        if sw1 != 0x90 or sw2 != 0x00:
            #If KEYA authentication fails, try KEYB
            sw1, sw2 = authenticate_sector(connection, SECTOR_NUMBER, KEYB)
            if sw1 != 0x90 or sw2 != 0x00:
                print(f"Autenticazione fallita: SW1={sw1:02X}, SW2={sw2:02X}")
                return None

        #Read data from the specified block
        read_command = [0xFF, 0xB0, BLOCK_NUMBER, 0x00, 0x10]  #Adjust the length as needed
        data, sw1, sw2 = connection.transmit(read_command)

        if sw1 == 0x90 and sw2 == 0x00:
            return bytes(data).decode('utf-8')  #Data is in UTF-8 format
        else:
            print(f"Lettura dei dati fallita: SW1={sw1:02X}, SW2={sw2:02X}")
            return None
    except Exception as e:
        print(f"Errore durante la lettura della carta: {e}")
        return None

if __name__ == "__main__":
    while True:
        print("In attesa di una carta...")
        data = None
        while not data:
            data = read_card()
            if not data:
                time.sleep(0.3)

        print(f"Dati della carta: {data}")
        print("Rimuovi la carta e passa un'altra per continuare.")
        time.sleep(2) #Pause after reading, just to not read again the same card.

I also tried various type of the auth commands, reading the documentation i found:

[! page documentation]
enter image description here

This is the pdf (in this case page 27): https://www.epsys.no/downloads/pdf/API_ACR128_v1.9.pdf

Does anybody know where i'm wrong? Thanks! Please don't remove my question again.

Upvotes: 0

Views: 104

Answers (0)

Related Questions