gpt4 book ai didi

java - Mifare NFC 中字符串之间的字节数是多少?

转载 作者:行者123 更新时间:2023-12-02 01:38:23 24 4
gpt4 key购买 nike

当我从一个 block 读取 Mifare classic 4k 卡并将字节转换为十六进制,然后转换为 UTF8/ASCII 时,我得到了可能控制字节而不是实际文本的奇怪字符。由于我只是将整个 block 直接转换为 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 个 block (=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

关于java - Mifare NFC 中字符串之间的字节数是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54862089/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com