【问题标题】:Write and read data to Mifare Classic 1k NFC tag向 Mifare Classic 1k NFC 标签写入和读取数据
【发布时间】:2020-01-14 19:03:32
【问题描述】:

我正在尝试在 Mifare Classic 1k NFC 标签上读取和写入数据。

感谢this app,我找到了卡的钥匙和访问条件:

键:

访问条件:

<uses-permission android:name="android.permission.NFC" /> 出现在我的清单中。

这是我的代码:

Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

if(tag != null) {
    Log.i("hey", Arrays.toString(tag.getTechList()));
    MifareClassic mfc = MifareClassic.get(tag) ;

    try {
        mfc.connect();
        boolean authA = mfc.authenticateSectorWithKeyA(2, MifareClassic.KEY_NFC_FORUM) ;
        boolean authB = mfc.authenticateSectorWithKeyB(2, MifareClassic.KEY_DEFAULT) ;
        Log.i("hey", "authA : " + authA) ;
        Log.i("hey", "authB : " + authB) ;

        if (authB && authA) {
            byte[] bWrite = new byte[16];
            byte[] hello = "hello".getBytes(StandardCharsets.US_ASCII);
            System.arraycopy(hello, 0, bWrite, 0, hello.length);
            mfc.writeBlock(0, bWrite);
            Log.i("hey", "write : " + Arrays.toString(bWrite));

            byte[] bRead = mfc.readBlock(0);
            String str = new String(bRead, StandardCharsets.US_ASCII);
            Log.i("hey", "read bytes : " + Arrays.toString(bRead));
            Log.i("hey", "read string : " + str);
            Toast.makeText(this, "read : " + str, Toast.LENGTH_SHORT).show();
            Log.i("hey", "expected : " + new String(bWrite, StandardCharsets.US_ASCII));
        }



        mfc.close();

    } catch (IOException e) {
        Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
        e.printStackTrace();
        Log.i("hey", "Error") ;
    }


}

当我尝试这样写作和阅读时,我读到的不是我写的。

这里是日志:

I/hey: [android.nfc.tech.NfcA, android.nfc.tech.MifareClassic, android.nfc.tech.NdefFormatable]
I/hey: authA : true
    authB : true
I/hey: write : [104, 101, 108, 108, 111, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
I/hey: read byte : [-78, 0, 0, 0, 0, 0, -1, 7, -128, 105, -1, -1, -1, -1, -1, -1]
    read string : �������������i������
I/hey: expected : hello����������������������

我尝试了不同的字符集,但没有任何改变。

我还尝试通过注释写入部分并将身份验证密钥 A 更改为 MifareClassic.KEY_MIFARE_APPLICATION_DIRECTORY 来仅读取扇区 0,这次我得到了这个 Logcat:

I/hey: [android.nfc.tech.NfcA, android.nfc.tech.MifareClassic, android.nfc.tech.NdefFormatable] 
I/hey: authA : true
        authB : true 
I/hey: read bytes : [-123, -121, 0, 16, 18, 8, 4, 0, 1, -64, 62, -70, 74, 124, 78, 29]
        read string : �������>�J|N

关于如何修复它以正确书写和显示文本的任何想法?

【问题讨论】:

    标签: android authentication permissions nfc mifare


    【解决方案1】:

    您首先对扇区 2 进行身份验证:

    boolean authA = mfc.authenticateSectorWithKeyA(2, MifareClassic.KEY_NFC_FORUM);
    boolean authB = mfc.authenticateSectorWithKeyB(2, MifareClassic.KEY_DEFAULT);
    

    然后,您尝试写入和读取块 0:

    mfc.writeBlock(0, bWrite);
    byte[] bRead = mfc.readBlock(0);
    

    这有几个问题:

    1. 同时使用密钥 A 和密钥 B 进行身份验证没有意义。只有最后一次身份验证才能确定标签的身份验证状态。由于似乎所有扇区都可以使用密钥 B 写入,因此您可以安全地使用第二行(仅限mfc.authenticateSectorWithKeyB())。

    2. 您对扇区 2 进行身份验证,该扇区由块 8、9、10 和 11 组成。在该身份验证状态下,写入和读取块 0 没有意义。请注意,读/写操作通常使用全局块编号。扇区仅用作身份验证/访问控制目的的逻辑单元。您可以使用mfc.sectorToBlock() 和mfc.getBlockCountInSector() 从扇区编号轻松计算块编号。

    3. 块 0(在扇区 0 中)是一个特殊块,即制造商块。它通常包含在生产过程中烧入标签的 UID、SAK 和制造商信息。此块是只读的,不能更改。因此,写入该块没有任何效果。

    【讨论】:

    • 我自己计算了第 1 点和第 3 点,但我认为每个扇区的块数分别为 0、1、2 和 3。非常感谢迈克尔!
    • @Tonher 块在查看每个扇区时确实从 0 开始编号。然而,由于 MIFARE Classic 的线性内存布局的性质,纯基于块的编号通常用于内存访问,并且扇区仅被视为用于身份验证和访问控制目的的逻辑单元。顺便提一句。您可以使用 Android 的内置方法 MifareClassic.sectorToBlock() 和 MifareClassic.getBlockCountInSector() 轻松地将扇区转换为块。
    • 感谢您的洞察
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-02
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    相关资源
    最近更新 更多