NFC-Tag als schreibgeschützt sperrenPython

Python-Programme
Anonymous
 NFC-Tag als schreibgeschützt sperren

Post by Anonymous »

Gibt es eine andere Möglichkeit, das NFC-Tag als schreibgeschützt zu sperren?
Ich habe den NTAG213 verwendet, um alle diese Sperrbefehle zu schreiben
Ich habe das Tag zuvor mit etwas NDEF im Tag gesperrt, das zur Überprüfung des Tags mit Telefonen verwendet wird.
Nach dem Sperren des NFC-Tags kann die Anwendung nicht auf das geschriebene NDEF zugreifen, obwohl das NDEF im NFC-Tag vorhanden ist

Code: Select all

def lock_nfc_tag_readonly_safe():
try:

block2 = pn532.ntag2xx_read_block(2)
if block2 is None or len(block2) != 4:
log("❌ Failed to read block 2")
return False

block2 = list(block2)

# ---------------------------
# byte 2: pages 3–15
# bit 0 = page 3 (CC) → MUST stay 0
# bits 1–7 = pages 4–15 → lock
# ---------------------------
block2[2] |= 0b11111110  # 0xFE

# ---------------------------
# byte 3: pages 16–39
# lock all remaining user memory
# ---------------------------
block2[3] |= 0xFF

if not pn532.ntag2xx_write_block(2, bytes(block2)):
log("❌ Failed to write lock bytes")
return False

return True

except Exception as e:
log(f"❌ Error locking NFC tag: {e}")
return False
Gibt es andere Möglichkeiten, das NFC-Tag für schreibgeschützte Zwecke zu sperren?
Ich habe die Funktion angepasst, anstatt nur die statischen Sperrbits auszuführen, um sowohl statische als auch dynamische Sperrbits zu kombinieren. Die aktualisierte Funktion sperrt nur die nicht verwendete Seite, um ein Überschreiben durch andere Anwendungen zu verhindern.

Code: Select all

def lock_nfc_tag(pn532, ndef_length_bytes):
"""
Lock NTAG213 safely for Flutter/Android apps.

Args:
pn532: PN532 instance
ndef_length_bytes: length of NDEF message in bytes

Returns:
True if lock succeeded, False otherwise
"""
try:
log("🔒 Locking NTAG213 safely (Flutter/Android compatible)")

# --- Step 1: Determine last page of NDEF ---
pages_needed = (ndef_length_bytes + 3) // 4  # 4 bytes per page
start_page = 4
end_page = start_page + pages_needed - 1

# --- Step 2: Static lock (page 2) ---
# NTAG213 static lock bytes control pages 3–19
b2 = pn532.ntag2xx_read_block(2)
if not b2 or len(b2) != 4:
log("❌ Failed to read static lock page")
return False

b2 = list(b2)

# Lock pages 4–15 (page 3 = CC, must stay 0)
if end_page >= 4:
b2[2] |= 0b11111110  # bits 1–7 → pages 4–15

# Lock pages 16–19 if NDEF does not occupy them
if end_page < 16:
b2[3] |= 0b00001111  # lower 4 bits → pages 16–19

if not pn532.ntag2xx_write_block(2, bytes(b2)):
log("❌ Failed to write static lock bytes")
return False

# --- Step 3: Dynamic lock (page 40) ---
# Pages 20–39 are controlled by dynamic lock bits
# Only lock pages AFTER end_page
dynamic_lock = [0x00, 0x00, 0x00, 0x00]

# Each bit represents a page (pages 20–39)
FLUTTER_BUFFER = 2  # extra pages Android may read

for i in range(20, 40):
if i > end_page + FLUTTER_BUFFER:
bit_index = i - 20
byte_index = bit_index // 8
bit_in_byte = bit_index % 8
dynamic_lock[byte_index] |= (1

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post