无法弄清楚如何使用 C# pcsc 将数据写入 NFC 类型 4 标签

Cannot figure out how to write data to NFC type 4 tag with C# pcsc

我正在用 c# .net 编写一个需要与 nfc reader(ACR122U 或内置 windows WUDC)通信的应用程序。 reader 将 send/receive 数据发送到腕带,其行为类似于类型 4 标签。为此,我正在使用 pcsc sharp 库:https://github.com/danm-de/pcsc-sharp。 通信采用NDEF协议,单文本记录,可用于从腕带获取数据。文本记录格式为G:{nameOfParameter}。例如G:ID,G:MAC.

几天来我一直在努力向腕带发送写入消息。我能够使用的命令是 APDU ReadBinary 命令:

using var isoReader = new IsoReader(context, readerName, SCardShareMode.Shared, SCardProtocol.T1);
var command = new CommandApdu(IsoCase.Case2Extended, isoReader.ActiveProtocol)
{
   CLA = 0x00,
   P1 = 0x00,
   P2 = 0x00,
   Le = 0x24,
   Instruction = InstructionCode.ReadBinary
};

var response = isoReader.Transmit(command);

转换为:00-B0-00-00-00-00-24

并且收到的数据是:00-14-C1-01-00-00-00-0D-54-02-65-6E-47-3A-49-44-3A-36-35-35- 33-35-00-00-00-00-00-00-00-00-00-00-00-00。其中包含带 G:ID:65535(这是预期值)的 NDEF 文本消息。

我尝试发送 APDU UpdateBinary 命令来发送数据(其中数据是单个 ndef 文本记录,enG:ID 作为有效负载):

new CommandApdu(IsoCase.Case3Extended, protocol)
{
   Data = new byte[] { 0xC1, 0x01, 0x06, 0x54, 0x65, 0x6E, 0x47, 0x3A, 0x49, 0x44 },
   CLA = 0x00,
   P1 = 0x00,
   P2 = 0x00,
   Instruction = InstructionCode.UpdateBinary // D6
};

但我得到 0B 00,根据 https://www.eftlab.com/knowledge-base/complete-list-of-apdu-responses/

转换为“错误的参数 P1-P2”

我已经使用 android NFC 工具应用程序进行了一些测试。我可以在那里创建一条带有单个文本记录的 NDEF 消息(例如 G:MAC 或 G:ID)并将其发送到腕带(使用应用程序 UI)。之后,下一次读取腕带会给出正确的响应值(ID 或 MAC,具体取决于上次发送的命令)。 因此,似乎使用 NFC 工具应用程序的通信工作正常。

我从 NFC 工具获得的关于标签的其他信息: 标签类型:ISO 14443-4 技术:IsoDep、NfcA、Ndef 数据格式:NFC Forum Type 4

有人能给我指出正确的方向吗?

Type 4 NDef 规范可能对 https://web.archive.org/web/20190827131645/http://apps4android.org/nfc-specifications/NFCForum-TS-Type-4-Tag_2.0.pdf

有帮助

我假设您已正确完成所有文件 select 内容

但规范中的更新程序说

The NDEF update procedure is:

  1. If the length of the NDEF message (to be written) is bigger than Maximum NDEF size-2 (see NFC File Control TLV in Section 5.1.2.1), the NDEF update procedure is aborted. Otherwise, go to item 2.
  2. Write the value 0000h in the NLEN field (see Table 9) using the NDEF Update command.
  3. Write the NDEF message in the NDEF message field (see Table 9) using one or more NDEF Update commands.
  4. Write the length of the NDEF message in the NLEN field (see Table 9) using the NDEF Update command.

似乎 NLEN 字段有一个大小,因此您的偏移量永远不应为零,但 2h

我还没有尝试过使用 Type 4 标签,但认为该规范可能对您有所帮助。