Mifare Ultralight:锁定特定页面 [英] Mifare Ultralight: lock specific pages

查看:122
本文介绍了Mifare Ultralight:锁定特定页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从此链接( Mifare Ultralight C锁)获得了参考,以使所有页面都显示在只读的Mifare Ultralight标签.

I got reference from this link (Mifare Ultralight C Lock) to make all pages on a Mifare Ultralight tag read-only.

我可以在Android上成功地在Mifare Ultralight标签上写一条消息.现在,我想锁定第4至7页(或任何特定的页面).上面的链接仅显示了如何锁定所有页面.如何锁定特定页面?

I can write a message on a Mifare Ultralight tag successfully on Android. Now I want to lock pages 4 to 7 (or any specific page). The above link only shows how to lock all pages. How I can lock specific pages?

此代码将锁定所有页面:

This code locks all pages:

mifare.transceive(new byte[] {
    (byte)0xA2,  /* CMD = WRITE */
    (byte)0x02,  /* PAGE = 2    */
    (byte)0x00, (byte)0x00, (byte)0xFF, (byte)0xFF  /* DATA = lock pages 3..15*/
});

public static boolean writeOnMifareUltralight(Context _context, Tag tag,String pageData, int i) {
    byte[]result;
    MifareUltralight mifare = null;
    try {

        mifare = MifareUltralight.get(tag);
        mifare.connect();
        mifare.writePage(i, pageData.getBytes(Charset.forName("US-ASCII")));

        mifare.transceive(new byte[] {
            (byte)0xA2,  /* CMD = WRITE */
            (byte)0x02,  /* PAGE = 2    */
            (byte)0x00, (byte)0x00, (byte)0xFF, (byte)0xFF/* DATA = lock pages 3..15*/
        });
    } catch (Exception ex) {
        ex.printStackTrace();
        Log.d("mtw", ex.getMessage());
        // return false;
    } finally {
        try {
            mifare.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    return true;
}

推荐答案

MIFARE Ultralight的锁定字节使用一位来锁定一个块.此外,它们还包含3个块锁定位,这些位可控制锁定字节本身的锁定状态.

The lock bytes of MIFARE Ultralight use one bit to lock one block. In addition they contain 3 block locking bits that control the lock status of the lock bytes themselves.

  • 字节2(第2页):位3-7是第3-7页的锁定位.因此,位3锁定第3页,位4锁定第4页,依此类推.低三位是块锁定位.位0锁定第3页的锁定位,位1锁定第4​​-9页的锁定位,位2锁定第10-15页的锁定位.

  • Byte 2 (on page 2): Bits 3-7 are the lock bits for pages 3-7. Thus, bit 3 locks page 3, bit 4 locks page 4, etc. The lower three bits are the block locking bits. Bit 0 locks the lock bit for page 3, bit 1 locks the lock bits for pages 4-9, and bit 2 locks the lock bits for pages 10-15.

字节3(第2页):位0-7是第8-15页的锁定位.因此,位0锁定第8页,位1锁定第9页,依此类推.

Byte 3 (on page 2): Bits 0-7 are the lock bits for pages 8-15. Thus, bit 0 locks page 8, bit 1 locks page 9, etc.

例如,要锁定第4-7页,您可以使用以下命令:

So, for example, to lock pages 4-7, you could use the following command:

mifare.transceive(new byte[] {
    (byte)0xA2,  /* CMD = WRITE */
    (byte)0x02,  /* PAGE = 2    */
    (byte)0x00, (byte)0x00, (byte)0xF0, (byte)0x00 /* DATA = lock pages 4..7*/
});

请注意,除了mifare.transceive()之外,您还可以简单地使用mifare.writePage():

Note that instead of mifare.transceive() you could also simply use mifare.writePage():

mifare.writePage(2, new byte[] {
    (byte)0x00, (byte)0x00, (byte)0xF0, (byte)0x00 /* DATA = lock pages 4..7*/
});

请记住,锁定位是一次性可编程的.因此,一旦将锁定位设置为逻辑1(即LOCKED),就不能将其反转为逻辑0(即UNLOCKED).

Keep in mind that lock bits are one-time-programmable. Hence, once a lock bit is set to logical 1 (i.e. LOCKED), it can't be reversed to a logical 0 (i.e. UNLOCKED).

这还允许您一次设置一个锁定位.例如,要将页面i设置为已锁定(其中3< = i< = 15 !!! ),应该可以执行以下操作:

This also allows you to set one lock bit at a time. For instance, to set page i LOCKED (where 3 <= i <= 15 !!!), something like this should work:

mifare.transceive(new byte[] {
    (byte)0xA2,  /* CMD = WRITE */
    (byte)0x02,  /* PAGE = 2    */
    (byte)0x00, (byte)0x00, (byte)((1<<i) & 0x0FF), (byte)(((1<<i)>>>8) & 0x0FF)
});

这篇关于Mifare Ultralight:锁定特定页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆