I'm having a problem in my application with the MifareClassic.autenticateSectorWithKeyA(int sector, byte[] keyA)
method. I have tried many ways but it dose not authenticate.
My key A is:
byte[] key = new byte[] { (byte) 0x3c, (byte) 0x55, (byte) 0x28,
(byte) 0x12, (byte) 0x5c, (byte) 0x61, (byte) 0x00,
(byte) 0x5C, (byte) 0x71, (byte) 0x00 };
3c5528125c61
as above, how should I write the byte array to authenticate, read block 2 and get the bytes?Your key
byte array does not make much sense as a MIFARE Classic key. A key for MIFARE Classic consists of only 6 bytes. Hence, this would be a possible value for key:
byte[] key = new byte[] { (byte)0x3c, (byte)0x55, (byte)0x28,
(byte)0x12, (byte)0x5c, (byte)0x61 };
In order to read block 2 (and assuming that it is readable after authenticationg with the above key
), you would do something like the following:
MifareClassic.blockToSector()
to obtain the sector number for a given block number.final MifareClassic mfc = MifareClassic.get(tag); mfc.connect(); final int blockNumber = 2; if (mfc.authenticateSectorWithKeyA(mfc.blockToSector(blockNumber), key)) { final byte[] data = mfc.readBlock(blockNumber) // TODO: do something with data } mfc.close();
MIFARE Classic itself has a linear memory layout. I.e. one that is addressed based on block numbers, where each block contains 16 bytes. These blocks are grouped into sectors with individual access conditions and keys.
In order to logically assign this data (the sectors/groups of blocks) to specific applications (e.g. some data for a physical access control system, some data for an electronic purse, etc.) and, hence, to use one card for more that one application at the same time, the MIFARE Application Directory (MAD) was introduced. The MAD is basically a lookup table (located in sector 0 for MIFARE Classic 1K and in sectors 0 and 16 for MIFARE Classic 4K). This lookup table maps each sector of the card to one application. Applications are identified though a two byte value, the MIFARE application identifier (AID). Thus, if a card uses the MAD, an application can lookup its data sectors by browsing the MAD for its AID.