MIFARE卡读取失败后进行身份验证

时间:2018-08-25 20:25:00

标签: arduino rfid mifare

arduino和mfrc522 rfid读卡器易于使用,可以读取和写入mifare卡。

我的目的是在一个扇区中同时使用卡的两个密钥,一些块可以用密钥A读取,而某些只能用密钥B读取。

正确设置访问位可允许这种行为。为了在第二个扇区(块4 5 6 7)上进行尝试,我将访问位g0 [0 0 0],g1 [1 0 1],g2 [0 0 0],g3 [1 0 1]设置为使用{,0xF5,0xA5,0xA0,0x38,}来编写块7。 https://www.nxp.com/docs/en/data-sheet/MF1S50YYX_V1.pdf的8.7节。现在,块5不能用keyA读取,而可以用keyB(g1)读取,而keyB不再可读(g3)。因此,当我使用keyA进行身份验证时,尝试读取块5会导致错误。此时,其他身份验证失败,尽管访问位允许,也无法读取其他块。

我试图用块5的键B和其他代码的键A读取第二个扇区,它正在工作。但是,如果我尝试用键A读取,然后用键B读取,则在失败的情况下将无法正常工作。

代码摘录:

// The sector of interest is the second one : blocks 4 5 6 7
Serial.println("\nAuthenticate block 0x05 with key B");
for (i = 4; i < 8; i++) {
      // Block 5 is readable with key B
      status = readblock(i==5?MF1_AUTHENT1B:MF1_AUTHENT1A, i, i==5?keyB:keyA, serial);
      if ( status == MI_ERR) {
           Serial.print(" - Unable to read block nb. 0x");
           Serial.println(i, HEX);
      }
}

Serial.println("\nAuthenticate with key A then key B if failed");
for (i = 4; i < 8; i++) {
       // Try to authenticate each block first with the A key.
       status = readblock(MF1_AUTHENT1A, i, keyA, serial);
       if ( status == MI_ERR) {
            Serial.print(" - Try keyB - ");
            status = readblock(MF1_AUTHENT1B, i, keyB, serial);
            if ( status == MI_ERR) {
                 Serial.print(" - Unable to read block nb. 0x");
                 Serial.println(i, HEX);
            }
        }
}

readblock功能(身份验证和读取)

byte readblock(byte mode, byte block, byte *key, byte *serial)
{
int j;
 byte data[MAX_LEN];
byte status = MI_ERR;

// Try to authenticate the block first
status = nfc.authenticate(mode, block, key, serial);
if ( status == MI_OK) {
    Serial.print("Authenticated block nb. 0x");
    Serial.print(block, HEX);
    // Reading block i from the tag into data.
    status = nfc.readFromTag(block, data);
    if (status == MI_OK) {
        // If there was no error when reading; print all the hex
        // values in the data.
        Serial.print(" : ");
        for (j = 0; j < 15; j++) {
            Serial.print(data[j], HEX);
            Serial.print(", ");
        }
        Serial.println(data[15], HEX);
    } else
        Serial.print(" - Read failed");
  } else {
    Serial.print("Failed authentication block nb. 0x");
Serial.print(block, HEX);
}
  return status;
}

结果是

Authenticate block 0x05 with key B
Authenticated block nb. 0x4 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
Authenticated block nb. 0x5 : AB, CD, EF, 1, 23, 45, 67, 89, 98, 76, 54, 1A, 10, FE, DC, BA
Authenticated block nb. 0x6 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
Authenticated block nb. 0x7 : 0, 0, 0, 0, 0, 0, F5, A5, A0, 38, 0, 0, 0, 0, 0, 0

Authenticate with key A then key B if failed
Authenticated block nb. 0x4 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
Authenticated block nb. 0x5 - Read failed - Try keyB - Failed authentication block nb. 0x5 - Unable to read block nb. 0x5
Failed authentication block nb. 0x6 - Try keyB - Failed authentication block nb. 0x6 - Unable to read block nb. 0x6
Failed authentication block nb. 0x7 - Try keyB - Failed authentication block nb. 0x7 - Unable to read block nb. 0x7

所以我想知道是否有可能尝试使用错误的键读取一个块,然后继续使用其他键读取该块,依此类推。

1 个答案:

答案 0 :(得分:1)

可以在https://www.nxp.com/docs/en/application-note/AN1304.pdf第24页

中找到说明。
  

每次身份验证操作,读取操作或写入操作失败时,MIFARE Classic或MIFARE Plus都会保持静音,并且不再响应任何命令。在这种情况下,为了继续执行NDEF检测程序,需要重新激活并选择MIFARE Classic或MIFARE Plus。

因此,您必须重新激活并在失败后进行选择,方法是将这些行添加到您的代码中,例如:

     Serial.println("\nAuthenticate with key A then key B if failed");
        for (i = 4; i < 8; i++) {
            // Try to authenticate each block first with the A key.
            status = readblock(MF1_AUTHENT1A, i, keyA, serial);
            if ( status == MI_ERR) {
                Serial.print(" - Try keyB - ");
/** RE ACTIVATE AND SELECT ------------------------------- **/
                nfc.haltTag();
                status = nfc.requestTag(MF1_REQIDL, data);
                if (status == MI_OK) {
                    status = nfc.antiCollision(data);
                    memcpy(serial, data, 5);
                    nfc.selectTag(serial);
                }
/** ------------------------------------------------------ **/
                status = readblock(MF1_AUTHENT1B, i, keyB, serial);
            if ( status == MI_ERR) {
                    Serial.print(" - Unable to read block nb. 0x");
                    Serial.println(i, HEX);
                }
            }
        }