【问题标题】:Can't authenticate block with PN532无法使用 PN532 验证块
【发布时间】:2018-03-21 10:42:46
【问题描述】:

我是 Arduino 新手。我正在尝试将 ID 写入我的 NFC 卡。我使用了示例中的 PN532 库和代码。我不能比authenticateBlock() 更进一步。还有哪个块正在尝试进行身份验证?我在输出中看不到它。

输出:

  • 找到 1 个标签
  • 感应响应:0x44
  • 选择响应:0x0
  • 0x4 0xB9 0xC9 0xBA 0x20 0x4B 0x80
  • 读卡#3122678656

代码是:

// This example writes a MIFARE memory block 0x08. It is tested with a new MIFARE 1K cards. Uses default keys.
// Note: Memory block 0 is readonly and contains manufacturer data. Do not write to Sector Trailer block
// unless you know what you are doing. Otherwise, the MIFARE card may be unusable in the future.

//Contributed by Seeed Technology Inc (www.seeedstudio.com)

#include <PN532.h>
#include <SPI.h>

/*Chip select pin can be connected to D10 or D9 which is hareware optional*/
/*if you the version of NFC Shield from SeeedStudio is v2.0.*/
#define PN532_CS 10
PN532 nfc(PN532_CS);

uint8_t written = 0;
#define  NFC_DEMO_DEBUG 1

void setup(void) {
#ifdef NFC_DEMO_DEBUG
  Serial.begin(9600);
  Serial.println("Hello!");
#endif
  nfc.begin();

  uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
#ifdef NFC_DEMO_DEBUG
    Serial.print("Didn't find PN53x board");
#endif
    while (1); // halt
  }
#ifdef NFC_DEMO_DEBUG
  // Got ok data, print it out!
  Serial.print("Found chip PN5");
  Serial.println((versiondata >> 24) & 0xFF, HEX);
  Serial.print("Firmware ver. ");
  Serial.print((versiondata >> 16) & 0xFF, DEC);
  Serial.print('.');
  Serial.println((versiondata >> 8) & 0xFF, DEC);
  Serial.print("Supports ");
  Serial.println(versiondata & 0xFF, HEX);
#endif
  // configure board to read RFID tags and cards
  nfc.SAMConfig();
}

void loop(void) {
  uint32_t id;
  // look for MiFare type cards
  id = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A);
  if (id != 0) {
#ifdef NFC_DEMO_DEBUG
    Serial.print("Read card #");
    Serial.println(id);
    Serial.println();
#endif
    uint8_t keys[] = {
      0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
    };
    uint8_t writeBuffer[16];
    for (uint8_t i = 0; i < 16; i ++) {
      writeBuffer[i] = i; //Fill buffer with 0,1,2....F
    }
    if (nfc.authenticateBlock(1, id , 0x08, KEY_A, keys)) {
      //authenticate block 0x08
      //if authentication successful
      if (written == 0) {
        //Not written
        written = nfc.writeMemoryBlock(1, 0x08, writeBuffer); // Write writeBuffer[] to block 0x08
        if (written)
#ifdef NFC_DEMO_DEBUG
          Serial.println("Write Successful");
#endif
      }
      uint8_t block[16];
      //read memory block 0x08
      if (nfc.readMemoryBlock(1, 0x08, block)) {
#ifdef NFC_DEMO_DEBUG
        Serial.println("Read block 0x08:");
        //if read operation is successful
        for (uint8_t i = 0; i < 16; i++) {
          //print memory block
          Serial.print(block[i], HEX);
          Serial.print(" ");
        }
        Serial.println();
#endif
      }
    }
  }
  delay(500);
}

【问题讨论】:

  • 您使用的 PN532 的库版本是多少?我认为问题可能出在身份验证命令中。在我在互联网上查看的库中,我在 authenticateBlock 函数中看到了这些参数:“uint8_t * uid,uint8_t uidLen,uint32_t blockNumber,uint8_t keyNumber,uint8_t * keyData”。在您的情况下,卡的 uid 是一个 uint32_t 变量,也许您需要检索 uid 或在 uint8_t 变量中重新转换,您能否分享您正在使用的 authenticateBlock 函数定义?

标签: authentication arduino nfc iot arduino-uno


【解决方案1】:

SENS_RES (ATQA) = 0x0044、SEL_RES (SAK) = 0x00 和 UID = 0x04B9C9BA204B80 的组合表明您的标签是 MIFARE Ultralight 标签或 NTAG em> 标签。但是,您在问题中显示的代码是为访问 MIFARE Classic 标签而设计的。

MIFARE Ultralight/NTAG 标签不支持nfc.authenticateBlock() 执行的身份验证机制(特定于 MIFARE Classic)。因此,代码将在nfc.authenticateBlock() 上失败。由于您的标签不支持该类型的身份验证(尽管它可能支持不同的机制),您可以尝试跳过身份验证步骤并仅使用读/写方法。这些应该适用于您的标签类型(有一些限制,请参阅Byte array gets truncated when writing to memory of RFID tag using Adafruit PN532 library)。

【讨论】:

  • 我们可以更改标签 targetID 吗?
  • 是的,我的卡是 ntag213,所以我如何存储您可以推荐的任何网站的数据?
  • @Mr_Rj 不,您不能更改防碰撞标识符(由readPassiveTargetID() 部分返回的标识符)。见Is there a software that can change NFC Tag's serial number?
  • @Mr_Rj 请参阅我的答案中链接的帖子,了解如何将数据写入标签。您可能还想检查例如Writing NDEF data to NTAG216 tag using low-level NFC communication methods 关于如何生成可以被其他设备(例如 Android)解释的 NDEF 格式数据。
  • 我能够从 ntag 卡中获取数据,但我不明白这是一种你知道如何解码或我们如何将其转换为纯文本形式的骗局?
猜你喜欢
  • 2018-09-12
  • 1970-01-01
  • 1970-01-01
  • 2021-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-12
  • 1970-01-01
相关资源
最近更新 更多