« Return to Thread: store key A and key B with Access bits in Sector Trailer Block

store key A and key B with Access bits in Sector Trailer Block

by legauss :: Rate this Message:

Reply to Author | View in Thread

hello ,
I have a mifare card and I can write and read in this card with defauts keys() , but I want to store keys A and B with the Access bits in the sector trailer bits , and I am enable to find the way that let me to do this .
Can anybody help me , I am blocked since 1 month and I have to find a solution for my project .
Please help me.


This is my code
import java.nio.ByteBuffer;
import java.util.List;
import javax.smartcardio.Card;
import javax.smartcardio.CardChannel;
import javax.smartcardio.CardException;
import javax.smartcardio.CardTerminal;
import javax.smartcardio.TerminalFactory;
 
/**
 *
 * @
 */
public class Read {
 
    public Read() {
 
        try {
 
            CardTerminal terminal = null;
 
            // show the list of available terminals
            TerminalFactory factory = TerminalFactory.getDefault();
            List<CardTerminal> terminals = factory.terminals().list();
 
            String readerName = "";
 
            for (int i = 0; i < terminals.size(); i++) {
 
                readerName = terminals.get(i).toString().substring(
                                    terminals.get(i).toString().length() - 2);
 
                if (readerName.equalsIgnoreCase("01")) {
                    terminal = terminals.get(i);
                }
            }
 
            // Establish a connection with the card
            System.out.println("Waiting for a card..");
            terminal.waitForCardPresent(0);
 
            Card card = terminal.connect("T=0");
            CardChannel channel = card.getBasicChannel();
 
            // Start with something simple, read UID, kinda like Hello World!
            byte[] baReadUID = new byte[5];
 
            baReadUID = new byte[]{(byte) 0xFF, (byte) 0xCA, (byte) 0x00,
                                   (byte) 0x00, (byte) 0x00};
 
            System.out.println("UID: " + send(baReadUID, channel));
            // If successfull, the output will end with 9000
 
            // OK, now, the real work
            // Get Serial Number
            // Load key
            byte[] baLoadKey = new byte[12];
 
            baLoadKey = new byte[]{(byte) 0xFF, (byte) 0x82, (byte) 0x20,
                                      (byte) 0x1A, (byte) 0x06, (byte) 0xFF,
                                      (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
                                      (byte) 0xFF, (byte) 0xFF};
 
            System.out.println("LOAD KEY: " + send(baLoadKey, channel));
            // If successfull, will output 9000
 
            // Authenticate
            byte[] baAuth = new byte[7];
 
            baAuth = new byte[]{(byte) 0xFF, (byte) 0x88, (byte) 0x00,
                                (byte) 0x09, (byte) 0x60, (byte) 0x00};
 
            System.out.println("AUTHENTICATE: " + send(baAuth, channel));
            // If successfull, will output 9000
          // Write Serial
           byte[] baWrite=new byte[] {(byte) 0xFF,(byte) 0xD6,(byte) 0x00,(byte)0x09, (byte)0x10,    
                                      (byte) 0xbd,(byte) 0xde, (byte) 0x6f, (byte) 0x37,
                                      (byte) 0x83, (byte) 0x83,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x01,
                                      (byte) 0x14,(byte) 0x8a, (byte) 0xc5, (byte) 0xe2,
                                      (byte) 0x28, (byte) 0x28};
            System.out.println("AUTHENTICATE: " + send(baWrite, channel));
            // Read Serial
            byte[] baRead = new byte[6];
 
            baRead = new byte[]{(byte) 0xFF, (byte) 0xB0, (byte) 0x00,
                                (byte) 0x09, (byte) 0x10};
 
            System.out.println("READ: " + send(baRead, channel));
            // If successfull, the output will end with 9000
 
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
 
    public String send(byte[] cmd, CardChannel channel) {
 
        String res = "";
 
        byte[] baResp = new byte[258];
        ByteBuffer bufCmd = ByteBuffer.wrap(cmd);
        ByteBuffer bufResp = ByteBuffer.wrap(baResp);
 
        // output = The length of the received response APDU
        int output = 0;
 
        try {
 
            output = channel.transmit(bufCmd, bufResp);
 
        } catch (CardException ex) {
            ex.printStackTrace();
        }
 
        for (int i = 0; i < output; i++) {
            res += String.format("%02X", baResp[i]);
            // The result is formatted as a hexadecimal integer
        }
 
        return res;
    }
 
    public static void main(String[] args) {
        new Read();
    }
}

 « Return to Thread: store key A and key B with Access bits in Sector Trailer Block