问题描述
我正在尝试使用 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 }; // 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);
}
}
解决方法
请勿将 NFC UID 用于任何安全目的,因为您可以看到出于隐私目的,手机确实会随机提供一个。
NFC UID 仅用于帮助读取硬件处理在范围内有多个不同标签时将数据发送到正确的卡。无法保证 UID 实际上是唯一的并且不能复制(即使使用应该在工厂对其进行编程的标签,您也可以从中国购买克隆,最终用户可以对其进行编程)。
如果使用手机来提供唯一性,以便将标签用于任何具有安全隐患的事情,最好对存储在标签或模拟标签上的数据使用加密方法。