NTAG213 Mifare Ultralight with Authentication

961 views Asked by At

I am new to NFC Android and I have been stuck for days trying to write in NTAG213 after set password to it
but it failed and i don`t know what happen can any one help me updated with more information about code

    // write PACK:
    mifareUltralight
        .writePage(
            pageOffset: 44, data: Uint8List.fromList([pack[0], pack[1], 0, 0]))
        .then((value) {
      setState(() {
        messageTXT = 'write PACK';
      });
    });

    // write PWD:
    mifareUltralight
        .writePage(
            pageOffset: 43,
            data: Uint8List.fromList([pwd[0], pwd[1], pwd[2], pwd[3]]))
        .then((value) {
      setState(() {
        messageTXT = 'write PWD';
      });
    });
    //set PROT
    bool prot =
        false; // false = PWD_AUTH for write only, true = PWD_AUTH for read and write
    int authlim = 0;
    // value between 0 and 7
    mifareUltralight
        .transceive(
            data: Uint8List.fromList([
      0xA2, // WRITE
      42, // page address
      ((object[0] & 0x078) | (prot ? 0x080 : 0x000) | (authlim & 0x007)),
      object[1], object[2], object[3]
    ]))
        .then((value) {
      setState(() {
        messageTXT = 'PWD_AUTH for write only0';
      });
    });

    // set AUTH0
    int auth0 = 0x00;
    // value between 0 and 7

    mifareUltralight
        .writePage(
            pageOffset: 41,
            data: Uint8List.fromList([
              object1[0], // keep old value for byte 0
              object1[1], // keep old value for byte 1
              object1[2],
              auth0
            ]))
        .then((value) {
      setState(() {
        messageTXT = 'PWD_AUTH for write only2';
      });
    });
  }

mifareUltralight
        .transceive(
            data: Uint8List.fromList([0x1B, pwd[0], pwd[1], pwd[2], pwd[3]]))
        .then((value) {
      if ((value != null) && (value.length >= 2)) {
        Ndef ndef = Ndef.from(tag);

        try {
          NdefMessage message = NdefMessage([
            NdefRecord.createUri(Uri.parse(url)),
          ]);
          ndef.write(message).then((value) {
            print('write success');
          });
        } catch (e) {
          print(e.toString());
        }
      }
    });
1

There are 1 answers

9
Andrew On

Update

Now that we see more code, the first place to start is that all these transceive commands will return a value if they are successful or not.

So it would be good to do error checking of all these commands sent to the tag, by checking the return valve to see which bit goes wrong.

From the datasheet for the Tag the ACK and NAK values are

Table 23.ACK and NAK valuesCode (4-bit) ACK/NAK
Ah Acknowledge (ACK)
0h NAK for invalid argument (i.e. invalid page address)
1h NAK for parity or CRC error
4h NAK for invalid authentication counter overflow
5h NAK for EEPROM write error

So the return value should always be Ah

Original

Just setting a password on the tag does not protect anything by default.

You also need to set the AUTH0 bytes (Byte 3 of page 0x29h on ntag213 - see datasheet of tag for details) as by default no memory pages are protected by the password you just set.

You need to change the value from protecting page 0xFFh and upwards (i.e no pages) to protecting at least page 0x04h (or 0x00h) and upwards to have any effect on access to NDEF data.

Changing the value Byte 3 of page 0x29h is done with a standard write command, so read page 0x29h, change the last byte of the 4 bytes read and write back to the page.

You should also be aware of the PROT bit of the Access byte memory address that changes between read and write for the password protection (default is password protect write access), this is also changed by a standard write command to the right memory page.