Mifare NFC中字符串之间的字节数是多少? [重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

当我从块到块读取Mifare经典4k卡并将字节转换为十六进制然后转换为UTF8 / ASCII时,我会得到可能控制字节而非实际文本的奇怪字符。由于我只是将整个块直接转换为UTF,我应该怎样做才能利用它们之间的那些位?下面是我得到的读数,左边是预期的翻译值。如果你自己转换十六进制,你会发现这些单词之间有奇怪的字符。

c5 42 4e 49 44 00 07 4f 4f 4f 4f 4f 4f 00 4b 42    "Åbnid" "OOOOOO" "KB" 
44 44 44 20 44 44 44 44 44 00 82 4d 00 c9 31 39    "DDD DDDDD" "M" "19"                     
39 34 34 33 34 32 00 d0 4e 4f 39 36 36 36 35 31     "944342" "NO966651"
00000000000070f78800000000000000
30 32 32 20 20 41 53 00 d3 54 4f 54 41 4c 20 4b    "022" "AS" "Total k"
 4f 4e 54 52 4f 4f 4f 20 41 53 20 00 c9 30 32 38    "ONTROOO AS" "028" 
37 30 34 33 33 00 c9 32 30 32 31 30 32 31 31 00    "70433" "20210211" 
00000000000070f78800000000000000

如何实现一个方法,该方法接受十六进制字符串或字节数组[]并仅通过逗号分隔返回单词?

java android nfc mifare
1个回答
2
投票

您可以按地址阅读,可能您只需要按数据地址读取。对于Mifare Classic卡,数据地址从0到63开始,16个扇区用4个块(= 1024字节))。但地址0始终存储UID或制造商ID。所以,从地址1,地址2 ...地址63开始阅读。让我为你分解,

 Sector 0: Address 0        , Address 1, Address 2, Address 3
          UID/ManufacturerID, Data     , Data     ,Sector Trail (KeyA,AccessKey,KeyB)
 Sector 1: Address 4, Address 5, Address 6, Address 7
           Data     , Data     , Data     , Sector Trail
 ...
 Sector 63 ...
 So Sector Trail = 00000000000070f78800000000000000
 KeyA = 000000000000
 AccessKey = 70f78800
 KeyB = 000000000000 

所以每个扇区,如果你没有设置读写保护,你跳过最后一个地址。所以试试这个。并相应地改变,这可能足以阅读

// final data
String data="";
// read sector 1 and 2
for(int sector = 1; sector < 3, sector++){
    // auth sector
    auth = mfc.authenticateSectorWithKeyA(sector, bytekey3);
    if(auth) {
        // read blocks from sector
        data += convertHexToString(readBlockData(sector)).trim();
    }
}

// read block 
private String readBlockData(int sector) {
            String blockvalues = "";


            // Read all blocks in sector
            for (int block = 0; (block < mfc.getBlockCountInSector(sector)); ++block) {

                // Get block number for sector + block
                int blockIndex = (mfc.sectorToBlock(sector) + block);

                try {

                    // Create a string of bits from block data and fix endianness
                    // http://en.wikipedia.org/wiki/Endianness                    
                    if (block < 3) {
                        // Read block data from block index
                        byte[] data = mfc.readBlock(blockIndex);
                        if (!(sector == 0 && block == 0)) {
                            String temp = ByteArrayToHexString(data);
                            blockvalues += temp;
                            Log.i(TAG, "Block " + blockIndex + " : " + temp);
                            rawData += ("Block " + blockIndex + " : " + temp + "\n");
                        }

                    }
                } catch (IOException e) {
                    Log.e(TAG, "Exception occurred  " + e.getLocalizedMessage());
                }
            }
            return blockvalues.trim();
}

 public String convertHexToString(String hex) {

        StringBuilder sb = new StringBuilder();
        StringBuilder temp = new StringBuilder();

        //49204c6f7665204a617661 split into two characters 49, 20, 4c...
        for (int i = 0; i < hex.length() - 1; i += 2) {

            //grab the hex in pairs
            String output = hex.substring(i, (i + 2));
            //convert hex to decimal
            int decimal = Integer.parseInt(output, 16);
            //convert the decimal to character
            sb.append((char) decimal);

            temp.append(decimal);
        }
        System.out.println("Decimal : " + temp.toString());

        return sb.toString().trim();
}

 private String ByteArrayToHexString(byte[] inarray) {
        int i, j, in;
        String[] hex = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",
                "B", "C", "D", "E", "F"};
        String out = "";

        for (j = 0; j < inarray.length; ++j) {
            in = inarray[j] & 0xff;
            i = (in >> 4) & 0x0f;
            out += hex[i];
            i = in & 0x0f;
            out += hex[i];
        }
        return out;
    }

最后一部分将是字符串操作。基本上,用空格重放所有双引号并使用String [] yourdata = data.split(“\ s +”);你会得到你的数据。我借用这个link的一些代码

© www.soinside.com 2019 - 2024. All rights reserved.