0

我正在尝试使用 PN532 从手机(三星 Galaxy S10)读取我的 NFC UID,但我只收到 08 和另外 3 位随机值。我读到以 08 开头的值是 RID(随机 ID)。是否有任何可能的方法来读取唯一值,或使用 PN532 从我的手机 NFC 中读取唯一值?我想使用该值将其与我的代码中的常数进行比较,并向继电器发送一个脉冲以打开一扇门。此代码来自 da Adafruit_PN532 库。

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_PN532.h>

#define PN532_IRQ   (2)
#define PN532_RESET (3)  // Not connected by default on the NFC Shield

// Or use this line for a breakout or shield with an I2C connection:
Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);

void setup(void) {
  Serial.begin(115200);
  Serial.println("Hello!");

  nfc.begin();

  uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
    Serial.print("Didn't find PN53x board");
    while (1); // halt
  }

  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);

  // configure board to read RFID tags
  nfc.SAMConfig();

  Serial.println("Waiting for an ISO14443A Card ...");
}


void loop(void) {
  uint8_t success;
  uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
  uint8_t uidLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)

  // Wait for an ISO14443A type cards (Mifare, etc.).  When one is found
  // 'uid' will be populated with the UID, and uidLength will indicate
  // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
  success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);

  if (success) {
    // Display some basic information about the card
    Serial.println("Found an ISO14443A card");
    Serial.print("  UID Length: "); Serial.print(uidLength, DEC); Serial.println(" bytes");
    Serial.print("  UID Value: ");
    nfc.PrintHex(uid, uidLength);

    if (uidLength == 4) {
      // We probably have a Mifare Classic card ...
      uint32_t cardid = uid[0];
      cardid <<= 8;
      cardid |= uid[1];
      cardid <<= 8;
      cardid |= uid[2];
      cardid <<= 8;
      cardid |= uid[3];
      Serial.print("Seems to be a Mifare Classic card #");
      Serial.println(cardid);
    }
    delay(2000);
  }
}
4

1 回答 1

0

不要将 NFC UID 用于任何安全目的,因为您可以看到手机出于隐私目的确实会随机提供一个。

NFC UID 仅用于帮助读取硬件处理当多个不同标签在范围内时向正确卡发送数据的处理。无法保证 UID 实际上是唯一的并且不能复制(即使使用应该在工厂对其进行编程的标签,您也可以从中国购买克隆,最终用户可以对其进行编程)。

如果使用电话提供唯一性以将标签用于具有安全隐患的任何事物,则最好使用加密方法与存储在标签或模拟标签上的数据。

于 2021-04-18T06:23:55.317 回答