The bench
An Uno with an MFRC522 reader on the SPI pins: SDA (the chip select) on 10, SCK on 13, MOSI on 11, MISO on 12 and RST on 9, powered from the Uno's 3.3 V. The NFC lesson swaps it for a PN532 board in I2C mode on A4 and A5, with its IRQ on pin 2. The access and security lessons add a green lamp on pin 7 (the lock), a red lamp on pin 6 and a buzzer on pin 8; the boss adds a 16x2 I2C LCD.
Click a reader to lay a card on it: cards 1 to 4 in turn, then none. The
reader's uids property sets the four cards. A four-byte UID makes a MIFARE
Classic 1K, a seven-byte UID an NTAG213. The defaults are C3 5A 1E 94,
7B 02 D4 19, 91 E6 3C A7 and 04 6F 2A D2 5B 71 80.
What each sketch does
rfidtags: reads the reader's version register, then each card's ATQA and SAK.rfiduids: prints each card's UID and length, once per tap.rfidblocks: a wrong key, the transport key, sector 1's trailer, and a name written to block 4 and read back.rfidpn532: the PN532's firmware version, an NTAG's UID, its capability container and a page of user memory.rfidacl: a door with an allow list, a buzzer for strangers and a master card that enrolls a guest.rfidclone: a door that trusts the UID lets in a clone; withCHECK_SECRETon it checks a secret in sector 2 and refuses the clone.rfidboss: the boss, a crew airlock that tracks who is aboard and sounds an intruder alarm.
Security, honestly
- A UID is sent in the clear, before any authentication, to any reader that asks. It identifies a card; it does not prove anything. "Magic" cards with a writable block 0, and phones or a Proxmark, can carry a copied UID.
- MIFARE Classic's Crypto1 cipher was reverse-engineered in 2007 and 2008, and its keys can be recovered in seconds to minutes with cheap hardware. Treat a Classic card as unauthenticated storage.
- For doors that matter, use AES cards (MIFARE DESFire EV2 or EV3, MIFARE Plus in SL3, NTAG 424 DNA) with keys diversified per card, plus a PIN for anything high value.
Honest limits of the simulator
- There is no radio: a card is either on the reader or not, with no range, angle or collisions between two cards.
- Crypto1 is not modeled. Authentication checks the key, so the security lesson can show a UID clone but not a key-recovery attack.
- Value blocks (increment and decrement) are refused, and the PN532 does not act as a tag or talk to a phone.
- What a sketch writes to a card stays on it until the circuit is rebuilt.
Related
Device drivers for SPI and I2C drivers, and Protocol design for record formats on a card.
Take the course at RFID and NFC.