Search code examples
androidnfcmifare

How to write MifareClassic with Custom Key


I need to find a solution to write Mifare Classic 1 K Tag with Custom Key. I am unable to write it, I have tried with every option but unfortunately all the time I get the error "IOException: Transceive Failed".

Below is my code snippet:

byte custom_key[]={
  (byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff
};

// 16 bytes of Data. Otherwise it was throwing IllegalArgumentException .
byte[] data="Hello world of N".getBytes();
getMfc().connect();

if(getMfc().authenticateSectorWithKeyA(4, custom_key)) {
  getMfc().writeBlock(3, data); // Here I receive IOException all the time.
} else {
  getMfc().close();
}

Please help me in this regard. I need to write Mifare Classic 1 K Tag with my own key.


Solution

  • You authenticate to sector 4 and then you try to write to block 3. Block 3 is in sector 0, so this will always fail. Try authenticating to sector 0 instead.

    BTW: Please, don't write random data to a sector trailer (such as block 3). It will likely lock up the sector with no way to recover.