如何将NFC标签设为只读?

问题描述

我有一个NTAG213 NFC贴纸。我想知道如何使此标签只读。如果以后我切换到NTAG215,如何使该标签为只读。实际使不同类型的标签只读的过程是什么?当我说“只读”时,我的意思是NFC的记录永远无法修改,但设备仍可以在未经身份验证的情况下读取记录。

我读过https://answers.launchpad.net/nfcpy/+question/242606,并尝试实施其解决方

import nfc
from time import sleep
from nfc.clf import RemoteTarget
import ndef

clf = nfc.ContactlessFrontend('usb')

while True:
    target = clf.sense(RemoteTarget('106A'),RemoteTarget('106B'),RemoteTarget('212F'))
    if target is None:
        sleep(1)
        continue

    serial = target.sdd_res.hex()
    tag = nfc.tag.activate(clf,target)

    if not tag.ndef:
        print("No NDEF records found!")
        continue
    
    for record in tag.ndef.records:
        print("Found record: " + str(record))

    record = ndef.UriRecord("https://www.example.com")
    tag.ndef.records = [record]
    # Code is fine until it gets to these tag indexes
    tag[15] = tag[15] | 0x0F
    tag[10] = 0xFF
    tag[11] = 0xFF

我得到了错误

  File "test.py",line 26,in <module>
    tag[15] = tag[15] | 0x0F
TypeError: 'NTAG213' object does not support indexing

解决方法

查找Python库,它具有一种简单的方法,可以使用锁定字节或通过密码将其设置为只读。

https://nfcpy.readthedocs.io/en/stable-0.10/modules/tag.html#nfc.tag.tt2_nxp.NTAG21x

只需使用或不使用密码即可对protect对象调用tag方法(不使用密码使用锁定字节,并且是永久只读)。

因此,该库已在其中编程了正确的存储器地址和位,以翻转各种卡,包括所有NTAG21x卡。