/// <summary> /// Read a sector trailer with provided keys /// </summary> /// <param name="sector"></param> /// <returns></returns> public bool ReadSectorTrailer(ref Sector sector) { byte[] data = null; ushort blockAddress = (ushort)MemoryCardMifareClassic.SectorTrailerAddress(sector.Number); if (sector.isSetKeyA()) { data = MifareClassicRead(blockAddress, 16, sector.getKeyA()); if (data == null) { if (sector.isSetKeyB()) { data = MifareClassicRead(blockAddress, 16, sector.getKeyB()); } } } else { if (sector.isSetKeyB()) { data = MifareClassicRead(blockAddress, 16, sector.getKeyB()); } } if (data == null) { return(false); } // Set access bits byte[] accessBytes = new byte[4]; Array.ConstrainedCopy(data, 6, accessBytes, 0, 4); sector.AccessBits = accessBytes; return(true); }
/// <summary> /// Try to read a sector with a specified key /// </summary> /// <param name="sectorNumber">Sector's number</param> /// <param name="key">Key used to read</param> /// <returns></returns> private MemoryCardMifareClassic.Sector ReadSectorWithKey(int sectorNumber, byte[] key) { MemoryCardMifareClassic.Sector sector = new Sector(sectorNumber); int firstBlockAddress = MemoryCardMifareClassic.SectorFirstBlockAddress(sectorNumber); int lastBlockAddress = MemoryCardMifareClassic.SectorTrailerAddress(sectorNumber); int counter = 0; for (ushort blockAddress = (ushort)firstBlockAddress; blockAddress < (ushort)lastBlockAddress; blockAddress++) { byte[] data = MifareClassicRead(blockAddress, 16, key); if (data != null) { sector.SetBlock(counter, data); counter++; } else { return(null); } } return(sector); }