public static MemoryMap loadCardFromFile(String fileName) throws IOException {
Collection<String> lines = readLinesFromFile(fileName);
if (lines.size() == 256 || lines.size() == 64) {
MemoryLayout memoryLayout = lines.size() == 64 ? MemoryLayout.CLASSIC_1K : MemoryLayout.CLASSIC_4K;
MemoryMap memoryMap = new MemoryMap(lines.size(), 16);
int blockNumber = 0;
Pattern pattern = Pattern
.compile("S(.*)\\|B(.*) Key: (............).*\\[(................................)\\]");
for (String data : lines) {
Matcher matcher = pattern.matcher(data);
if (matcher.matches()) {
int sectorId = Integer.parseInt(matcher.group(1));
int blockId = Integer.parseInt(matcher.group(2));
byte[] keyA = NfcUtils.convertASCIIToBin(matcher.group(3));
byte[] blockData = NfcUtils.convertASCIIToBin(matcher.group(4));
if (memoryLayout.isTrailerBlock(sectorId, blockId)) {
byte[] key = new byte[6];
System.arraycopy(blockData, 0, key, 0, 6);
// copy keyA only if the read data is all 0. This is a real card scan.
if (Arrays.equals(EMPTY_KEY, key)) {
System.arraycopy(keyA, 0, blockData, 0, 6);