development

    Password-Protecting NTAG213/215/216 Tags in Flutter, Straight from the Datasheet

    By Ayesha IftikharSenior Software Engineer & Product Engineer
    Password-Protecting NTAG213/215/216 Tags in Flutter, Straight from the Datasheet

    Short answer

    Identify the chip with GET_VERSION to find its configuration pages, then write PWD and PACK first, ACCESS next and AUTH0 last. Set AUTH0 first and the tag locks itself with the factory password FF FF FF FF. To remove protection, authenticate with PWD_AUTH and set AUTH0 back to 0xFF. On Android, an empty response to a write is the chip's ACK, not an error.

    NTAG213, NTAG215 and NTAG216 are the NFC chips inside most stickers, cards and key fobs. They support a 32-bit password that blocks writes while leaving the tag readable by anyone. That's exactly what you want for a tag on a café table: every phone can tap it, nobody can rewrite it.

    When I added this to NFC Wizard, I implemented it from NXP's NTAG21x datasheet using raw NFC-A commands. Here's how it works, and the details that are easy to get wrong.

    The four pieces of configuration

    Each NTAG21x chip has a few configuration pages (4 bytes each) near the end of its memory:

    PageContentsWhat matters here
    CFG0MIRROR, RFUI, MIRROR_PAGE, AUTH0AUTH0 = first page that needs the password
    CFG1ACCESS, RFUI…Bit 7 of ACCESS (PROT): 0 = only writes need the password, 1 = reads too
    PWD4-byte passwordWrite-only; reads back as zeros
    PACK2-byte acknowledgement (+2 RFU)Returned by the tag after a correct password

    Protection is off when AUTH0 points beyond the last page (0xFF). To turn it on, you set AUTH0 to the first page you want protected.

    Step 1: Find out exactly which chip you have

    The configuration pages live at different addresses on each chip, so you have to know which one you're holding. GET_VERSION (0x60) returns 8 bytes: byte 2 is the vendor (0x04 = NXP) and byte 6 encodes the storage size.

    static const int _cmdGetVersion = 0x60;
    
    Future<int> _configPage() async {
      final version = await _transceive(Uint8List.fromList([_cmdGetVersion]));
      if (version.length < 8 || version[2] != 0x04) {
        throw const AppException(AppExceptionType.nfc, _unsupported);
      }
      return switch (version[6]) {
        0x0F => 0x29, // NTAG213
        0x11 => 0x83, // NTAG215
        0x13 => 0xE3, // NTAG216
        _ => throw const AppException(AppExceptionType.nfc, _unsupported),
      };
    }

    The returned value is the page of CFG0. CFG1, PWD and PACK follow it.

    Step 2: Write in the right order

    This is the part that can brick a tag. A blank NTAG's password is the factory default, FF FF FF FF. If you set AUTH0 first, protection switches on immediately, using the factory password. Your next write (the real password) then needs authentication you haven't done.

    So the order is: password and PACK first, then ACCESS, and AUTH0 last.

    Future<void> setPassword(String password) async {
      final config = await _configPage();
      final cfg0 = await _readPage(config);
      if (cfg0[3] <= config) {
        throw const AppException(
          AppExceptionType.nfc,
          'This tag already has a password. Unlock it first.',
        );
      }
      final cfg1 = await _readPage(config + 1);
      // PWD and PACK before AUTH0, or the tag locks itself with FF FF FF FF.
      await _writePage(config + 2, keyFor(password));
      await _writePage(config + 3, const [0x00, 0x00, 0x00, 0x00]);
      // PROT = 0: only writes need the password.
      await _writePage(config + 1, [cfg1[0] & 0x7F, cfg1[1], cfg1[2], cfg1[3]]);
      // Protect from page 4, the first user page.
      await _writePage(config, [cfg0[0], cfg0[1], cfg0[2], 0x04]);
    }

    Starting protection at page 4 keeps the UID and capability container readable, so phones still recognise the tag as NDEF.

    Step 3: Turn a typed password into 4 bytes

    Users type passwords of any length; the chip wants exactly 4 bytes. I hash the UTF-8 text with 32-bit FNV-1a. It's deterministic (the same text always unlocks the same tag) and spreads short passwords across the whole 32-bit space. It isn't a security feature, since the chip's 32-bit limit is the ceiling either way, but it means "cafe2026" and "Cafe2026" are different keys, as people expect.

    Step 4: Unlock with PWD_AUTH

    To remove protection, you authenticate with PWD_AUTH (0x1B + 4 password bytes). A correct password returns the 2-byte PACK; a wrong one returns a 4-bit NAK. Once authenticated, set AUTH0 back to 0xFF.

    Future<bool> unlock(String password) async {
      final config = await _configPage();
      final cfg0 = await _readPage(config);
      if (cfg0[3] > config) return false; // no password set
      await _authenticate(keyFor(password));
      await _writePage(config, [cfg0[0], cfg0[1], cfg0[2], 0xFF]);
      return true;
    }

    The Android/iOS difference in the ACK

    After a WRITE (0xA2), the tag answers with a 4-bit ACK (0xA). On iOS, that arrives as a byte 0x0A. On Android, the same ACK comes back as an empty response. Treat the empty frame as an error and every write looks like a failure on Android, even though the tag accepted it.

    // Android returns an empty frame for the 4-bit ACK; iOS returns 0x0A.
    if (response.isNotEmpty && (response.first & 0x0F) != _ack) {
      throw const AppException(AppExceptionType.nfc, 'The tag rejected the write.');
    }

    Testing without a tag

    All of this lives in one class that takes a single function:

    typedef NfcTransceive = Future<Uint8List> Function(Uint8List command);

    In the app, that function sends a frame through the platform NFC session. In tests, it's a fake tag: a list of pages that answers GET_VERSION, READ, WRITE and PWD_AUTH like the datasheet says. That made it easy to test the dangerous cases (wrong password, a tag that already has one, an unsupported chip, and the write order) without burning through stickers.

    What this protects, and what it doesn't

    An NTAG21x password is good at one thing: stopping casual rewrites, like someone with an NFC app pointing your table tag at a different site. It is not strong security:

    • It's 32 bits.
    • The password travels unencrypted over the air during PWD_AUTH.
    • It doesn't stop anyone reading or copying the tag's contents.

    If you need tags that can't be cloned, look at chips with cryptographic authentication, such as NXP's NTAG 424 DNA. For everyday stickers, a password plus a clear "permanent lock" option covers what people actually need. In NFC Wizard, the permanent lock sits behind a confirmation that spells out that it can never be undone.

    You can try this in NFC Wizard, or read the user-facing guide: How to lock or password-protect an NFC tag.

    Share this article

    You Might Also Like

    Scheduling Class Reminders in Flutter Around iOS's 64-Notification Limit

    Scheduling Class Reminders in Flutter Around iOS's 64-Notification Limit

    How ClassPing schedules a whole semester of class reminders offline with flutter_local_notifications: a 60-reminder rolling window under iOS's 64 cap, stable notification IDs, a serial scheduling queue, inexact Android alarms and reboot handling.

    Read More →
    What I Check in a Product Audit Before Launch

    What I Check in a Product Audit Before Launch

    A practical pre-launch product audit checklist: the architecture, UX, security, performance, store and analytics checks I run before an app goes live, and why each one matters.

    Read More →
    How Uber Computes Estimated Time of Arrival (ETA)

    How Uber Computes Estimated Time of Arrival (ETA)

    How Uber ETA works: a deep technical breakdown of how Uber computes estimated time of arrival using map matching, graph routing, machine learning, real-time traffic updates, and uncertainty modeling at global scale.

    Read More →