Search code examples
c++nfcrfidapdu

Mifare 1K writing data in two blocks


I'm working with APDU command for writing and reading an RFID card. I can read from block number 2 and write 16 bytes of data, but I have a problem writing longer data. How can I manage to do that? I have tried to write in two blocks but it doesn't work.

this the way I implemented my code in the operation of writing:

// write...
//


if (nres == SM_SUCCESS)// &&
//bAPDURes )
{
nlenrcv = sizeof(btRcv);
nlencmd = 0;

btCmd[nlencmd++] = 0xFF; // CLA
btCmd[nlencmd++] = 0xD6; // INS
btCmd[nlencmd++] = 0x00; // P1, Mifare Block Number MSB, for mifare it is always 0x00
btCmd[nlencmd++] = 0x04; // P2, Mifare Block Number LSB
btCmd[nlencmd++] = 32; // Lc, Data Length
memcpy(btCmd + nlencmd, btWrite, 32);
nlencmd += 32;
nres = m_Smart.RFTransmit(DEV_INTERNALRF, nlencmd, btCmd, (DWORD*)&nlenrcv, btRcv);

Solution

  • You need to send each block separately. The Mifare Classic Write command will only write one block at once.

    See Section 12.3 of the Card's Data sheet

    So RFTransmit the write command for the first 16 bytes to the first block and RFTransmit the write command for the second 16 bytes to the next block.