在 Android 上清除 MifareClassic 扇区的最佳方法是什么?

What is the best way to clear MifareClassic sector on Android?

清除 MIFARE Classic 卡上的扇区是否有比全零写入更好的方法?

这是我当前代码的示例:

for (i in 0 until sectorCount) {
    if (!authenticateSectorWithKeyA(i, key) && !authenticateSectorWithKeyA(i, MifareClassic.KEY_DEFAULT)) {
        break
    }

    val idx = sectorToBlock(i)

    for (j in 0 until getBlockCountInSector(i)) {
        val index = idx + j
        when (index) {
            0 -> cardId = getCardId(readBlock(idx + j))
            1 -> {} //Todo: Write specail data
            4 -> {} //Todo: Write specail data
            5 -> {} //Todo: Write specail data
            else -> {
                if ((index + 1) % 4 != 0) {
                    writeBlock(index, ByteArray(16).apply { fill(0.toByte()) })
                }
            }
        }
    }
}

通过用零填充所有数据块来清除扇区是完全可以的。没有其他方法可以擦除 MIFARE Classic 卡上的数据。

只要确保您没有用零覆盖扇区尾部(每个扇区中的最后一个块),因为这会导致无效的访问位(这反过来会导致该扇区永久无法访问)。我认为这就是您尝试使用

实现的目标
if ((index + 1) % 4 != 0)

但请注意,例如MIFARE Classic 4K 具有更大的扇区,其中尾部不是扇区的第 4 个数据块。