Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Mifare NFC中字符串之间的字节是多少?_Java_Android_Nfc_Mifare - Fatal编程技术网

Java Mifare NFC中字符串之间的字节是多少?

Java Mifare NFC中字符串之间的字节是多少?,java,android,nfc,mifare,Java,Android,Nfc,Mifare,当我逐块读取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

当我逐块读取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

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

您可以按地址读取,可能只需要按数据地址读取。 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;
    }
//最终数据
字符串数据=”;
//读取扇区1和扇区2
对于(int扇区=1;扇区<3,扇区++){
//认证扇区
auth=mfc.authenticateSector with keya(扇区,bytekey3);
if(auth){
//从扇区读取块
data+=convertHexToString(readBlockData(扇区)).trim();
}
}
//读块
私有字符串readBlockData(整型扇区){
字符串blockvalues=“”;
//读取扇区中的所有块
对于(int block=0;(block>4中)&0x0f;
out+=十六进制[i];
i=in&0x0f;
out+=十六进制[i];
}
返回;
}

最后一部分是字符串操作。基本上,用空格重放所有双引号,并使用字符串[]yourdata=data.split(“\s+”);你会得到你的数据。我从中借用的一些代码可以按地址读取,可能只需要按数据地址读取。 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;
    }
//最终数据
字符串数据=”;
//读取扇区1和扇区2
对于(int扇区=1;扇区<3,扇区++){
//认证扇区
auth=mfc.authenticateSector with keya(扇区,bytekey3);
if(auth){
//从扇区读取块
data+=convertHexToString(readBlockData(扇区)).trim();
}
}
//读块
私有字符串readBlockData(整型扇区){
字符串blockvalues=“”;
//读取扇区中的所有块
对于(int block=0;(block