Package org.nfctools.mf.classic

Examples of org.nfctools.mf.classic.MemoryLayout


public class MfClassicCardScanner extends AbstractCardTool {

  @Override
  public void doWithReaderWriter(MfClassicReaderWriter readerWriter) throws IOException {
    MemoryLayout memoryLayout = readerWriter.getMemoryLayout();

    for (int sectorId = 0; sectorId < memoryLayout.getSectors(); sectorId++) {
      for (byte[] key : knownKeys) {
        try {
          MfClassicAccess access = new MfClassicAccess(new KeyValue(Key.A, key), sectorId, 0,
              memoryLayout.getBlocksPerSector(sectorId));
          MfBlock[] mfBlock = readerWriter.readBlock(access);
          for (int blockId = 0; blockId < mfBlock.length; blockId++) {
            System.out.println("S" + sectorId + "|B" + blockId + " Key: " + NfcUtils.convertBinToASCII(key)
                + " " + mfBlock[blockId]);
          }
View Full Code Here


    return tag.getTagType().equals(TagType.MIFARE_CLASSIC_1K) || tag.getTagType().equals(TagType.MIFARE_CLASSIC_4K);
  }

  @Override
  public void handleTag(Tag tag) {
    MemoryLayout memoryLayout = tag.getTagType().equals(TagType.MIFARE_CLASSIC_1K) ? MemoryLayout.CLASSIC_1K
        : MemoryLayout.CLASSIC_4K;
    MfClassicReaderWriter readerWriter = new AcrMfClassicReaderWriter((ApduTag)tag, memoryLayout);
    try {
      doWithReaderWriter(readerWriter);
    }
View Full Code Here

import org.nfctools.utils.NfcUtils;

public class MfClassicCardCleaner extends AbstractCardTool {

  private TrailerBlock readTrailerBlock(MfClassicReaderWriter readerWriter, int sectorId) throws IOException {
    MemoryLayout memoryLayout = readerWriter.getMemoryLayout();
    for (byte[] key : knownKeys) {
      try {
        MfClassicAccess access = new MfClassicAccess(new KeyValue(Key.A, key), sectorId,
            memoryLayout.getTrailerBlockNumberForSector(sectorId));
        MfBlock[] readBlock = readerWriter.readBlock(access);
        return (TrailerBlock)readBlock[0];
      }
      catch (MfLoginException e) {
      }
View Full Code Here

  }

  public static ApplicationDirectory initInstance(MfClassicReaderWriter readerWriter, MadKeyConfig keyConfig)
      throws IOException {
    byte[] writeKeyValue = keyConfig == null ? null : keyConfig.getWriteKeyValue();
    MemoryLayout memoryLayout = readerWriter.getMemoryLayout();
    MfClassicAccess accessTrailer = new MfClassicAccess(MfClassicConstants.MAD_KEY, 0,
        memoryLayout.getTrailerBlockNumberForSector(0));
    TrailerBlock madTrailer = (TrailerBlock)readerWriter.readBlock(accessTrailer)[0];

    if ((madTrailer.getGeneralPurposeByte() & MadConstants.GPB_MAD_AVAILABLE) != 0) {
      if ((madTrailer.getGeneralPurposeByte() & MadConstants.GPB_MAD_V1) == MadConstants.GPB_MAD_V1) {
        madTrailer.setKey(Key.A, MadConstants.DEFAULT_MAD_KEY);
View Full Code Here

    }
  }

  public static ApplicationDirectory createInstance(MfClassicReaderWriter readerWriter, MadKeyConfig keyConfig)
      throws IOException {
    MemoryLayout memoryLayout = readerWriter.getMemoryLayout();
    if (memoryLayout.getMadVersion() == MadConstants.GPB_MAD_V1) {
      Mad1 mad1 = new Mad1(readerWriter, keyConfig);
      mad1.initMadTrailer(memoryLayout.getMadVersion());
      mad1.writeMad();
      return mad1;
    }

    if (memoryLayout.getMadVersion() == MadConstants.GPB_MAD_V2) {
      Mad2 mad2 = new Mad2(readerWriter, keyConfig);
      mad2.initMadTrailer(memoryLayout.getMadVersion());
      mad2.writeMad();
      return mad2;
    }
    throw new RuntimeException("Unsupported MAD version" + memoryLayout.getMadVersion());
  }
View Full Code Here

  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);
View Full Code Here

  }

  @Override
  public void doWithReaderWriter(MfClassicReaderWriter readerWriter) throws IOException {
    DataBlock emptyDataBlock = new DataBlock();
    MemoryLayout memoryLayout = readerWriter.getMemoryLayout();

    for (int sectorId = 0; sectorId < memoryLayout.getSectors(); sectorId++) {
      TrailerBlock trailerBlock = readTrailerBlock(readerWriter, sectorId);
      if (trailerBlock != null) {
        Key keyToWrite = trailerBlock.isKeyBReadable() ? Key.A : Key.B;
        for (byte[] key : knownKeys) {
          try {
            initTransportConfig(readerWriter, sectorId, new KeyValue(keyToWrite, key));
            for (int blockId = 0; blockId < memoryLayout.getBlocksPerSector(sectorId); blockId++) {
              if (!memoryLayout.isTrailerBlock(sectorId, blockId) && !(blockId == 0 && sectorId == 0)) {
                log.info("Cleaning S" + sectorId + "|B" + blockId);
                MfClassicAccess access = new MfClassicAccess(new KeyValue(Key.A,
                    MfConstants.TRANSPORT_KEY), sectorId, blockId);
                readerWriter.writeBlock(access, emptyDataBlock);
              }
View Full Code Here

    log.info("Done!");
  }

  public static void initTransportConfig(MfClassicReaderWriter readerWriter, int sector, KeyValue keyValue)
      throws IOException {
    MemoryLayout memoryLayout = readerWriter.getMemoryLayout();
    TrailerBlock transportTrailer = new TrailerBlock();
    MfClassicAccess access = new MfClassicAccess(keyValue, sector,
        memoryLayout.getTrailerBlockNumberForSector(sector));
    readerWriter.writeBlock(access, transportTrailer);
  }
View Full Code Here

TOP

Related Classes of org.nfctools.mf.classic.MemoryLayout

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.