« Return to Thread: Reading Mifare 1K

Re: Reading Mifare 1K

by geri-m :: Rate this Message:

Reply to Author | View in Thread

Try these variable definitions:

authenticate = 0;
read = 2;

Authentication works (what a success!!!), but the following readCMD does not. The Read command should take the ID of the Block (= SectorID* 4 + BlockNR [0...3]) as the parameter (= 2. Element of the Vector), which looks something like this:

<code>
input = new Vector();
input.addElement(new Integer(MIFARE_READ_CMD));
                       
// Parameter is byte array containg the (absolut) ID of the block to be read ...
input.addElement(new byte[]{(byte)(4*k+j)});
</code>

The Phone throws a "javax.microedition.contactless.ContactlessExceptioN: Commands can not be performed" Exception.

There are two other things that are quite annoying:
* when the connection to the tag is established, I do get an alert window ("... do you want to allow xyxz to us the contactless_connection selcect Oneshot ...."), although I already granted the right to the application during startup.
* after the (failed) reading of the tag, I do close the connection properly, but the phone frezzes and forces me to reboot (which is quite time consuming on this device).

And here is my read method:

<code>
public void read() {
        form.append("Read started\n");
        PlainTagConnection pconn = null;
        try {

            for (int i = 0; i < target.getConnectionNames().length; i++) {
                form.append(target.getConnectionNames()[i].getName().toString() + "\n");
                if (target.getConnectionNames()[i].getName().equals("javax.microedition.contactless.rf.PlainTagConnection")) {
                    form.append("Conneciton found");
                    pconn = (PlainTagConnection) Connector.open(target.getUrl(target.getConnectionNames()[i]));
                    form.append("Conneciton set");
                }
            }
            if (pconn == null) {
                return;
            }

            // Fill a buffer with the keys;
            // 16 Byte = 16 Sectors
            // 7 Byte  =  1. Byte = Secotrd ID; 2 - 7. Byte = Key;
            byte[][] authent_buffer = new byte[16][7];
            // buffer containing the password for each block
            for (int j = 0; j < 16; j++) {
                // index of the
                // block 0, 4, 8,12,...,60
                authent_buffer[j][0] = (byte) (4 * j);
                for (int u = 1; u < 7; u++) {
                    // default passwords
                    // are 0xFFFFFFFFFFFF
                    authent_buffer[j][u] = (byte) 0xFF;
                }
            }

            // go thru all 16 sectores
            for (int k = 0; k < 16 ; k++) {
                form.append("Loop to authenticate: " + k + "\n");
                try {
                    // authenticate the sector!
                    Vector input = new Vector();
                    // do the auth. cation
                    input.addElement(new Integer(0));
                    input.addElement(authent_buffer[k]);
                    Vector retVal = pconn.transceive(input);
                    form.append("OKAY - Sector " + k + "Len: " + retVal.size() + "\n");

                    // go thru all 3 block in each sector
                    for (int j = 0; j < 3; j++) {
                        input = new Vector();
                        // do the read
                        input.addElement(new Integer(2));

                        // Parameter is byte of the block to be read ...
                        input.addElement(new byte[]{(byte) (4 * k + j)});

                        form.append("Try to read Blcok " + (4 * k + j) + "\n");
                       
                        // now do the reading
                        Vector out = pconn.transceive(input);

                        // print the ouptut of the reading
                        byte[] x = (byte[]) out.elementAt(0);
                        form.append("READ: #" + j + "Data: " + arrayToHex(x) + "\n");
                    }

                } catch (Exception e1) {
                    form.append("FAILED - Sector " + k + "| " + e1.toString() + "\n");
                }

            }
        } catch (IOException e) {
            form.append("IOExcpetion " + e.toString());
        }

       
        // Closing Connection again.
        try {
            if (pconn != null) {
                pconn.close();
            }
        } catch (IOException io) {
            form.append("Can't close Connection " + io.toString());
        }

    }
</code>

 « Return to Thread: Reading Mifare 1K