刚刚在 services.msc 上启动了一个名为
“Smart Card Device Enumeration Service”的 Windows 服务,我可以向 SmartCards 写入/读取数据。
关于 NDEF 格式化,我在扇区 0、块 1 和 2 上插入了 MAD 数据。之后,在扇区 1、块 0..2 上添加了 NDEF 记录。然后,我更新了 MAD 和 NDEF 扇区的 Key A 和 Key B,如下所示:
手动填写 MAD 和 2 条 NDEF 记录:
//Atualiza KeyB
mifareCard.AddOrUpdateSectorKeySet(new SectorKeySet {
Key = new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
KeyType = KeyType.KeyB,
Sector = 0
});
//Seta dados do MAD
await mifareCard.SetData(0, 1, new byte[] { 0x14, 0x01, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1 });
await mifareCard.SetData(0, 2, new byte[] { 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1 });
mifareCard.AddOrUpdateSectorKeySet(new SectorKeySet {
Key = new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
KeyType = KeyType.KeyB,
Sector = 1
});
//Incluis dois records NDEF
await mifareCard.SetData(1, 0, new byte[] { 0x00, 0x00, 0x03, 0x11, 0xD1, 0x01, 0x0D, 0x55, 0x01, 0x61, 0x64, 0x61, 0x66, 0x72, 0x75, 0x69 });
await mifareCard.SetData(1, 1, new byte[] { 0x74, 0x2E, 0x63, 0x6F, 0x6D, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 });
await mifareCard.Flush();
更新 NDEF 记录的密钥:
//Get MAD sector
var setor = mifareCard.GetSector(0);
//Get Trail data
await setor.GetData(3);
//Update chaves. Acess bits are generated by Mirafe API
await setor.FlushTrailer(
Extensions.ByteArrayToString(new byte[] { 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5 }),
Extensions.ByteArrayToString(new byte[] { 0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7 }
));
//Set others sectors keys for NDEF
for (var sector = 1; sector < 16; sector++) {
try {
setor = mifareCard.GetSector(sector);
await setor.GetData(3);
await setor.FlushTrailer(
Extensions.ByteArrayToString(new byte[] { 0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7 }),
Extensions.ByteArrayToString(new byte[] { 0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7 }
));
} catch (Exception ex) {
Debug.Write(ex.Message + "\n");
}
}