Reading Mifare 1K

View: New views
5 Messages — Rating Filter:   Alert me  

Reading Mifare 1K

by geri-m :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I tried to read a Mifare 1K according to the exampel in the manuel pages 14 - 16.

Unfortunatly the constants
* MIFARE_AUTHENT_A_CMD
* MIFARE_WRITE16_CMD

can not be resolved. Where are they can they be found? (which package?) Is there somewhere an Impelmentation of split16buffer() avialable as well?

Addtionally: is the Vector() implemented in the JVM on the Benq ... usually it is not part of J2ME.

Cheers, geri-m

Re: Reading Mifare 1K

by Zec54 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I was wondering the same thing about MiFare reading and the missing definitions of the constants.
Hope Jose has more info.

As for the class Vector, AFAIK it is part of the J2ME and also it works on my T80.

Samo

Re: Reading Mifare 1K

by josergc :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi geri-m,

I have been checking the documentation, and you are right, they have not defined the values of these constants, but I am figuring out these values are coming from the specific development for Mifare. Better if we check the documentation of the Mifare cards.

What is your mean about if Vector() is implemented in the JVM? I have been using this kind of objects in the BenQ and not problem.

Anyway, I am forwarding your questions to BenQ, but still waiting for answers. I will answer you as soon as possible.

Best regards,

Jose

Re: Reading Mifare 1K

by geri-m :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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>

Re: Reading Mifare 1K

by josergc :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Gerald,

I forwarded your questions to BenQ and I got the answer. They say if you are trying to read a Mifare tag, it will throw an exception of read the manufacturer sector (first sector) because it cannot be read or write. This implementation don't read the fourth block of each sector and the first block of the first sector.

In this case, your code will look like

...

 // go thru all 3 block in each sector
                    for (int j = 0; j < 3; j++) {



if (k != 0 || j != 0) { // we don't read the fourth block of each sector and the first block of the first sector


                              input = new Vector();
                              // do the read
                            input.addElement(new Integer(2));
...

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

} // if (k != 0 || j != 0)


                    }

...

Can you check if this works?

Greetings!

José