NTAG212带认证的Mifare Ultralight

4

我是 NFC Android 的新手,尝试着几天时间一直卡在了获取基于 NTAG212 Mifare Ultralight with Authentication 的第4页到第7页的问题上。根据 NTAG212 文档,我已经有了进行基于 PWD_AUTH 的 PWD 和 PACK。

我采用以下方法...

//assume password as array of bytes
//assume pack as array of bytes
try{
nfc.connect();
byte[] cmd1 = nfc.transceive(new byte[]{ (byte) 0x30, (byte) 0x00 }); //read the page 0     to make the NFC active
nfc.transceive(new byte[]{
   (byte) 0x1B, //command for PWD_AUTH
   pass[0],
   pass[1],
   pass[2],
   pass[3]
});
byte[] cmd4 = nfc.transceive(new byte[]{ (byte) 0x30, (byte) 0x04 }); //read the page 4
}catch(TagLostException e){
  e.printStackTrace();
}catch(IOException e){
  e.printStachTrace();
}finally{
    try{
        nfc.close();
    }catch(Exception e){
      //display failed to close
    }
}

我在向NFC发送PWD_AUTH命令后,总是会收到一个android.nfc.TagLostException: Tag was lost.的错误。请问我的做法是否正确?有没有做错什么?请帮忙解决问题。

注意:我已经多次阅读NTAG212的文档,并搜索了谷歌、stackoverflow和所有可能的资源。

谢谢您的帮助。
Kenster

2个回答

10

你发送给标签的PWD_AUTH命令意义不大。

PWD_AUTH命令的想法是,你发送你的密码(一个4字节的值),标签会响应它的密码确认(PACK)值(一个2字节的值),如果你使用正确的密码进行了身份验证。然后,你可以将PACK值与预期的密码确认进行验证,以“认证”该标签。

因此,正确的命令应该是:

byte[] response = nfc.transceive(new byte[] {
    (byte) 0x1B, // PWD_AUTH
    pass[0], pass[1], pass[2], pass[3]
});
if ((response != null) && (response.length >= 2)) {
   byte[] pack = Arrays.copyOf(response, 2);
   // TODO: verify PACK to confirm that tag is authentic (not really,
   // but that whole PWD_AUTH/PACK authentication mechanism was not
   // really meant to bring much security, I hope; same with the
   // NTAG signature btw.)
}

为了启用NTAG212上的密码保护,您需要做以下几点:

  1. 将PWD(第39页)设置为所需的密码(默认值为0xFFFFFFFF)。

byte[] response = nfc.transceive(new byte[] {
    (byte) 0xA2, // WRITE
    (byte) 39,   // page address
    pass[0], pass[1], pass[2], pass[3]
});
  • 将PACK(第40页,字节0-1)设置为您期望的密码确认(默认值为0x0000)。

    byte[] response = nfc.transceive(new byte[] {
        (byte) 0xA2, // WRITE
        (byte) 40,   // page address
        pack[0], pack[1],   // bytes 0-1 are PACK value
        (byte) 0, (byte) 0  // other bytes are RFU and must be written as 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, // READ
        (byte) 38    // page address
    });
    if ((response != null) && (response.length >= 16)) {  // read always returns 4 pages
        boolean prot = false;  // false = PWD_AUTH for write only, true = PWD_AUTH for read and write
        int authlim = 0; // value between 0 and 7
        response = nfc.transceive(new byte[] {
            (byte) 0xA2, // WRITE
            (byte) 38,   // page address
            (byte) ((response[0] & 0x078) | (prot ? 0x080 : 0x000) | (authlim & 0x007)),
            response[1], response[2], response[3]  // keep old value for bytes 1-3, you could also simply set them to 0 as they are currently RFU and must always be written as 0 (response[1], response[2], response[3] will contain 0 too as they contain the read RFU value)
        });
    }
    
  • 将 AUTH0(第37页,第3个字节)设置为应该需要密码验证的第一页。

  • byte[] response = nfc.transceive(new byte[] {
        (byte) 0x30, // READ
        (byte) 37    // page address
    });
    if ((response != null) && (response.length >= 16)) {  // read always returns 4 pages
        boolean prot = false;  // false = PWD_AUTH for write only, true = PWD_AUTH for read and write
        int auth0 = 0; // first page to be protected, set to a value between 0 and 37 for NTAG212
        response = nfc.transceive(new byte[] {
            (byte) 0xA2, // WRITE
            (byte) 37,   // page address
            response[0], // keep old value for byte 0
            response[1], // keep old value for byte 1
            response[2], // keep old value for byte 2
            (byte) (auth0 & 0x0ff)
        });
    }
    
    如果您使用MifareUltralight标签技术,可以使用readPageswritePage方法,而不是直接使用transceive方法:

  • READ命令:

  • byte[] response = nfc.transceive(new byte[] {
        (byte) 0x30,                  // READ
        (byte) (pageAddress & 0x0ff)  // page address
    });
    

    等同于

    byte[] response = nfc.readPages(pageAddress);
    
  • 写入命令

  • byte[] data = { (byte)..., (byte)..., (byte)..., (byte)... };
    byte[] response = nfc.transceive(new byte[] {
        (byte) 0xA2,                  // WRITE
        (byte) (pageAddress & 0x0ff), // page address
        data[0], data[1], data[2], data[3]
    });
    

    等价于

    nfc.writePage(pageAddress, data);
    

    嗨,Michael,我按照你的建议做了,但仍然出现相同的错误...我的方法正确吗?我读取页面0以使NFC处于活动状态,然后继续进行PWD_AUTH吗?还是我漏掉了什么。顺便感谢您的回复。 - Kenster
    顺便提一下,在Android中可以使用NfcA或MifareUltralight标签技术,两种标签技术的transceive方法将执行完全相同的操作。 - Michael Roland
    那么如果是这种情况,我该怎么办?我发送了正确的密码,但它失败了...标签是在交给我们之前由制造商设置的。我有一些新的标签还没有扫描过。但我不知道如何获取数据...在pwd_auth中似乎存在什么问题? - Kenster
    我该如何解锁它?我有必要的细节,但不知道发送什么命令...请问您能提供一个吗?我真的很困惑...谢谢。 - Kenster
    Michael,再给我一个例子代码来设置PROT、AUTHLIM和AUTH0好吗?我就是不理解7位、2位和AUTH0的第3个字节。这将是我最后的请求了。 - Kenster
    显示剩余13条评论

    0

    在进行PWD_AUTH操作期间发送错误密码时,会出现TagLostException异常。

    确保在常规WRITE操作中使用的与标签的PWD字段设置相匹配的4字节密码已发送到PWD_AUTH操作中。


    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接