你发送给标签的PWD_AUTH命令意义不大。
PWD_AUTH命令的想法是,你发送你的密码(一个4字节的值),标签会响应它的密码确认(PACK)值(一个2字节的值),如果你使用正确的密码进行了身份验证。然后,你可以将PACK值与预期的密码确认进行验证,以“认证”该标签。
因此,正确的命令应该是:
byte[] response = nfc.transceive(new byte[] {
(byte) 0x1B,
pass[0], pass[1], pass[2], pass[3]
});
if ((response != null) && (response.length >= 2)) {
byte[] pack = Arrays.copyOf(response, 2);
}
为了启用NTAG212上的密码保护,您需要做以下几点:
将PWD(第39页)设置为所需的密码(默认值为0xFFFFFFFF
)。
byte[] response = nfc.transceive(new byte[] {
(byte) 0xA2,
(byte) 39,
pass[0], pass[1], pass[2], pass[3]
});
将PACK(第40页,字节0-1)设置为您期望的密码确认(默认值为0x0000
)。
byte[] response = nfc.transceive(new byte[] {
(byte) 0xA2,
(byte) 40,
pack[0], pack[1],
(byte) 0, (byte) 0
});
将AUTHLIM(第38页,字节0,位2-0)设置为最大的密码验证失败次数(将该值设为0将允许无限次PWD_AUTH尝试)。
将PROT(第38页,字节0,第7位)设置为您想要的值(0 = 仅在需要写入访问时使用PWD_AUTH,1 = 在读写访问时都需要PWD_AUTH)。
byte[] response = nfc.transceive(new byte[] {
(byte) 0x30,
(byte) 38
});
if ((response != null) && (response.length >= 16)) {
boolean prot = false;
int authlim = 0;
response = nfc.transceive(new byte[] {
(byte) 0xA2,
(byte) 38,
(byte) ((response[0] & 0x078) | (prot ? 0x080 : 0x000) | (authlim & 0x007)),
response[1], response[2], response[3]
});
}
将 AUTH0(第37页,第3个字节)设置为应该需要密码验证的第一页。
byte[] response = nfc.transceive(new byte[] {
(byte) 0x30,
(byte) 37
});
if ((response != null) && (response.length >= 16)) {
boolean prot = false;
int auth0 = 0;
response = nfc.transceive(new byte[] {
(byte) 0xA2,
(byte) 37,
response[0],
response[1],
response[2],
(byte) (auth0 & 0x0ff)
});
}
如果您使用MifareUltralight标签技术,可以使用
readPages
和
writePage
方法,而不是直接使用
transceive
方法:
READ命令:
byte[] response = nfc.transceive(new byte[] {
(byte) 0x30,
(byte) (pageAddress & 0x0ff)
});
等同于
byte[] response = nfc.readPages(pageAddress)
写入命令
byte[] data = { (byte)..., (byte)..., (byte)..., (byte)... };
byte[] response = nfc.transceive(new byte[] {
(byte) 0xA2,
(byte) (pageAddress & 0x0ff),
data[0], data[1], data[2], data[3]
});
等价于
nfc.writePage(pageAddress, data);