Mifare Ultralight:锁定特定页面 [英] Mifare Ultralight: lock specific pages
问题描述
我从这个链接(Mifare Ultralight C Lock)得到了参考,让所有页面都在只读的 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?
此代码锁定所有页面:
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.writePage()
代替 mifare.transceive()
:
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
LOCKED (where 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屋!