I'm using the SMARTCARD API from CardWerk.
How can I change the default key ((byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF
) using an APDU?
The APDU consits of a CLASS, an INSTRUCTION, P1, P2. I have been reading documentation but I'm unable to find what parameters do I need to change the actual key to a new one.
The process for changing the keys of a MIFARE Classic card is like this:
The sector trailer is formatted like this:
Where
xx xx xx xx xx xx
is key A,yy yy yy yy yy yy
is key B andzz zz zz
are the access bytes that enforce key-based access permissions.gg
is a general-purpose byte with no specific meaning unless you use a MIFARE application directory or NXP's NDEF mapping for using MIFARE Classic as NFC tag).Be warned that setting the access bytes to an invalid value will render the card inaccessible!
An example sector trailer could look like this:
The access conditions meaning that you can read with key A and read/write with key B.
As MIFARE Classic cards do not speak APDU, it's difficult to give you a ready-made APDU command for this. (MIFARE Classic cards are contactless memory cards and use their own proprietary contactless protocol and PC/SC-compliant smartcard readers usually only map these proprietary memory access commands to APDUs.)
However, if your reader supports PC/SC 2.01 commands for storage cards, commands could look like this:
Load a key
xxxxxxxxxxxx
to key slot 0 (depending on your reader you might need to encode a different slot numer in P2; remember to adapt the authenticate command in that case):Authenticate sector 0 using that key as key A:
Or authenticate sector 0 using that key as key B:
Write new sector trailer for sector 0 (in block 3):