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.
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.
If I understand correctly, the question is how to edit block 3 in sector 4.
After authentication you try to write to block 3. But:
A blocks number does not start at 0 in every block. Sector 0 has blocks 0-3, sector 1 has blocks 4-7, and so on. Sector 4 has a block-offset of 4*4=16. So the block you want to write to is block 19.
There is a function available that calculates this offset for you. sectorToBlock(4) yields 16.
For writing to block 3 in sector 4 I would use the following:
getMfc().writeBlock(getMfc().sectorToBlock(4)+3, data)
The accepted answer does only provide the alternative that leaves the questioner unable to understand the block-indexing beyond sector 0.
来源:https://stackoverflow.com/questions/13154452/how-to-write-mifareclassic-with-custom-key