0
votes

I am trying to read a Mifare Classic card and I fail at the authentication step. I've tried both authentication methods, A and B.

I'm also familiar with this question and I've also read this page where you'll find a reference to a list of phones that can't read Mifare Classic cards/tags.

The phone I'm using is LG Optimus L5 II E460. Have any of you used it?

Is there a way to check inside my application if the phone can read Mifare Classic cards/tags?

Is there a way to lock a Mifare Classic card/tag with a personal key and not the MifareClassic.KEY_DEFAULT ?

Is there a way to format a Mifare Classic card/tag? By format I mean erase everything from the card/tag, like a Factory Reset.

Here is my code:

public class NFC_Writer extends AppCompatActivity {

private NfcAdapter mNfcAdapter;
private Tag mTag;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nfc__writer);

    mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_nfc__writer, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
protected void onResume() {
    super.onResume();

    enableForegroungDispatchSystem();
}

@Override
protected void onPause() {
    super.onPause();

    disableForegroungDispatchSystem();
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    if(intent.hasExtra(NfcAdapter.EXTRA_TAG))
    {
        Toast.makeText(this, "We received a NFC intent!", Toast.LENGTH_SHORT).show();

        mTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

        String[] techList = mTag.getTechList();

        //writeCard(message);

        EditText editText = (EditText) this.findViewById(R.id.entered_text);
        String message = editText.getText().toString();

        NdefMessage ndefMessage = createNdefMessage(message);

        writeNdefMessage(mTag, ndefMessage);
    }
}

private void enableForegroungDispatchSystem(){

    Intent intent = new Intent(this, NFC_Writer.class).addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

    IntentFilter[] intentFilters = new IntentFilter[] {};

    mNfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFilters, null);
}

private void disableForegroungDispatchSystem(){
    mNfcAdapter.disableForegroundDispatch(this);
}


private void formatTag(Tag tag, NdefMessage ndefMessage){
    try{
        NdefFormatable ndefFormatable = NdefFormatable.get(tag);

        if(null == ndefFormatable){
            Toast.makeText(this, "Tag is not ndef formatable", Toast.LENGTH_SHORT).show();
        }

        ndefFormatable.connect();
        ndefFormatable.format(ndefMessage);
        ndefFormatable.close();
    }
    catch(Exception e){
        Log.e("formatTag", e.getMessage());
        Toast.makeText(this, "Tot avem exceptie in formatTag " + e.getMessage(), Toast.LENGTH_SHORT).show();
    }
}

private void writeNdefMessage(Tag tag, NdefMessage ndefMessage){
    try{
        if (null == tag) {
            Toast.makeText(this, "Tag object cannot be NULL!", Toast.LENGTH_SHORT).show();
            return;
        }

        Ndef ndef = Ndef.get(tag);

        if (null == ndef) {
            formatTag(tag, ndefMessage);
        }
        else{
            ndef.connect();

            if (false == ndef.isWritable()) {
                Toast.makeText(this, "Tag is not writable!", Toast.LENGTH_SHORT).show();

                ndef.close();

                return;
            }

            ndef.writeNdefMessage(ndefMessage);

            ndef.close();
        }

        Toast.makeText(this, "Tag written!", Toast.LENGTH_SHORT).show();
    }
    catch (Exception e){
        Log.e("writeNdefMessage", e.getMessage());
    }
}

private NdefRecord createTextRecord(String content){
    try{
        byte[] language;

        language = Locale.getDefault().getLanguage().getBytes("UTF-8");

        final byte[] text = content.getBytes("UTF-8");
        final int languageSize = language.length;
        final int textlength = text.length;
        final ByteArrayOutputStream payload = new ByteArrayOutputStream(1 + languageSize + textlength);

        payload.write( (byte)(languageSize & 0x1F));
        payload.write(language, 0, languageSize);
        payload.write(text, 0, textlength);

        return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], payload.toByteArray());
    }
    catch(UnsupportedEncodingException e){
        Log.e("createtextRecord", e.getMessage());
    }

    return null;
}

private NdefMessage createNdefMessage(String content){

    NdefRecord ndefRecord = createTextRecord(content);

    NdefMessage ndefMessage = new NdefMessage(new NdefRecord[] {ndefRecord});

    return ndefMessage;
}

private void writeCard(String content){
    MifareClassic mfc = MifareClassic.get(mTag);

    try{
        mfc.connect();
        {
            boolean authA = mfc.authenticateSectorWithKeyB(0, MifareClassic.KEY_DEFAULT);

            Toast.makeText(this, "writeCard() : " + String.valueOf(authA) + " ", Toast.LENGTH_SHORT).show();

            mfc.writeBlock(mfc.sectorToBlock(1), new byte[]{'A', 'l', 'v', 'a', 'r', 'e', 'z', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '});
        }
        mfc.close();
    }
    catch(Exception ioe) {
        Log.e("writeCard : ", ioe.getMessage());
    }
}
}

I've tried to use NdefFormatable to format the card in order to work with Ndef but an IOException in thrown when I call the NdefFormatable::connect() function.

When I try using the MifareClassic class I always receive false when I call function authenticateSectorWithKeyB or authenticateSectorWithKeyA.

I've tried both this methods because when I call Tag::getTechList() I receive the following list: android.nfc.tech.NfcA, android.nfc.tech.MifareClassic, android.nfc.tech.NdefFormatable;

Here are all the logs:

10-08 19:35:34.906    6649-6649/? D/dalvikvm﹕ Late-enabling CheckJNI
10-08 19:35:34.948    6649-6649/com.example.bosutar_cosmin.nfc_writer E/Trace﹕ error opening trace file: No such file or directory (2)
10-08 19:35:34.961    6649-6649/com.example.bosutar_cosmin.nfc_writer V/ActivityThread﹕ Class path: /data/app/com.example.bosutar_cosmin.nfc_writer-1.apk, JNI path: /data/data/com.example.bosutar_cosmin.nfc_writer/lib
10-08 19:35:35.014    6649-6649/com.example.bosutar_cosmin.nfc_writer W/dalvikvm﹕ VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
10-08 19:35:35.014    6649-6649/com.example.bosutar_cosmin.nfc_writer I/dalvikvm﹕ Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onSearchRequested
10-08 19:35:35.014    6649-6649/com.example.bosutar_cosmin.nfc_writer W/dalvikvm﹕ VFY: unable to resolve interface method 14076: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
10-08 19:35:35.015    6649-6649/com.example.bosutar_cosmin.nfc_writer D/dalvikvm﹕ VFY: replacing opcode 0x72 at 0x0002
10-08 19:35:35.016    6649-6649/com.example.bosutar_cosmin.nfc_writer I/dalvikvm﹕ Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onWindowStartingActionMode
10-08 19:35:35.016    6649-6649/com.example.bosutar_cosmin.nfc_writer W/dalvikvm﹕ VFY: unable to resolve interface method 14080: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
10-08 19:35:35.016    6649-6649/com.example.bosutar_cosmin.nfc_writer D/dalvikvm﹕ VFY: replacing opcode 0x72 at 0x0002
10-08 19:35:35.090    6649-6649/com.example.bosutar_cosmin.nfc_writer I/dalvikvm﹕ Could not find method android.view.ViewGroup.onRtlPropertiesChanged, referenced from method android.support.v7.widget.Toolbar.onRtlPropertiesChanged
10-08 19:35:35.090    6649-6649/com.example.bosutar_cosmin.nfc_writer W/dalvikvm﹕ VFY: unable to resolve virtual method 13977: Landroid/view/ViewGroup;.onRtlPropertiesChanged (I)V
10-08 19:35:35.090    6649-6649/com.example.bosutar_cosmin.nfc_writer D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0007
10-08 19:35:35.094    6649-6649/com.example.bosutar_cosmin.nfc_writer I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
10-08 19:35:35.095    6649-6649/com.example.bosutar_cosmin.nfc_writer W/dalvikvm﹕ VFY: unable to resolve virtual method 402: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
10-08 19:35:35.095    6649-6649/com.example.bosutar_cosmin.nfc_writer D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
10-08 19:35:35.096    6649-6649/com.example.bosutar_cosmin.nfc_writer I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
10-08 19:35:35.096    6649-6649/com.example.bosutar_cosmin.nfc_writer W/dalvikvm﹕ VFY: unable to resolve virtual method 424: Landroid/content/res/TypedArray;.getType (I)I
10-08 19:35:35.096    6649-6649/com.example.bosutar_cosmin.nfc_writer D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
10-08 19:35:35.145    6649-6649/com.example.bosutar_cosmin.nfc_writer I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawable
10-08 19:35:35.146    6649-6649/com.example.bosutar_cosmin.nfc_writer W/dalvikvm﹕ VFY: unable to resolve virtual method 365: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
10-08 19:35:35.146    6649-6649/com.example.bosutar_cosmin.nfc_writer D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
10-08 19:35:35.147    6649-6649/com.example.bosutar_cosmin.nfc_writer I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawableForDensity
10-08 19:35:35.147    6649-6649/com.example.bosutar_cosmin.nfc_writer W/dalvikvm﹕ VFY: unable to resolve virtual method 367: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
10-08 19:35:35.147    6649-6649/com.example.bosutar_cosmin.nfc_writer D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
10-08 19:35:35.230    6649-6653/com.example.bosutar_cosmin.nfc_writer D/dalvikvm﹕ GC_CONCURRENT freed 216K, 4% free 8490K/8839K, paused 13ms+9ms, total 71ms
10-08 19:35:35.348    6649-6649/com.example.bosutar_cosmin.nfc_writer I/SurfaceTextureClient﹕ [void android::SurfaceTextureClient::init()] debug.stc.fps: 3000 ms
10-08 19:35:35.350    6649-6649/com.example.bosutar_cosmin.nfc_writer D/libEGL﹕ loaded /vendor/lib/egl/libEGL_mtk.so
10-08 19:35:35.354    6649-6649/com.example.bosutar_cosmin.nfc_writer D/libEGL﹕ loaded /vendor/lib/egl/libGLESv1_CM_mtk.so
10-08 19:35:35.358    6649-6649/com.example.bosutar_cosmin.nfc_writer D/libEGL﹕ loaded /vendor/lib/egl/libGLESv2_mtk.so
10-08 19:35:35.399    6649-6649/com.example.bosutar_cosmin.nfc_writer E/MMUMapper﹕ fail to register MVA, unsupported format(0x5)
10-08 19:35:35.401    6649-6649/com.example.bosutar_cosmin.nfc_writer D/OpenGLRenderer﹕ Enabling debug mode 0
10-08 19:35:35.513    6649-6649/com.example.bosutar_cosmin.nfc_writer E/MMUMapper﹕ fail to register MVA, unsupported format(0x5)
10-08 19:35:35.980    6649-6649/com.example.bosutar_cosmin.nfc_writer E/MMUMapper﹕ fail to register MVA, unsupported format(0x5)
10-08 19:35:38.963    6649-6649/com.example.bosutar_cosmin.nfc_writer I/SurfaceTextureClient﹕ [0x52024190] frames:8, duration:3.459000, fps:2.312427
10-08 19:35:41.970    6649-6649/com.example.bosutar_cosmin.nfc_writer I/SurfaceTextureClient﹕ [0x52024190] frames:6, duration:3.006000, fps:1.995360
10-08 19:35:42.058    6649-6649/com.example.bosutar_cosmin.nfc_writer D/VelocityTracker﹕ Couldn't open '/dev/touch' (No such file or directory)
10-08 19:35:42.058    6649-6649/com.example.bosutar_cosmin.nfc_writer D/VelocityTracker﹕ tpd read x fail: Bad file number
10-08 19:35:42.059    6649-6649/com.example.bosutar_cosmin.nfc_writer D/VelocityTracker﹕ tpd read y fail: Bad file number
10-08 19:35:44.980    6649-6649/com.example.bosutar_cosmin.nfc_writer I/SurfaceTextureClient﹕ [0x52024190] frames:10, duration:3.009000, fps:3.322367
10-08 19:35:45.885    6649-6649/com.example.bosutar_cosmin.nfc_writer E/writeCard :﹕ Transceive failed
10-08 19:35:45.923    6649-6649/com.example.bosutar_cosmin.nfc_writer I/SurfaceTextureClient﹕ [void android::SurfaceTextureClient::init()] debug.stc.fps: 3000 ms
10-08 19:35:45.939    6649-6649/com.example.bosutar_cosmin.nfc_writer E/MMUMapper﹕ fail to register MVA, unsupported format(0x1)
10-08 19:35:47.885    6649-6649/com.example.bosutar_cosmin.nfc_writer E/MMUMapper﹕ invalid operation for unregister MVA with VA(0x532fb000) size(98560)
10-08 19:35:47.920    6649-6649/com.example.bosutar_cosmin.nfc_writer I/SurfaceTextureClient﹕ [void android::SurfaceTextureClient::init()] debug.stc.fps: 3000 ms
10-08 19:35:47.937    6649-6649/com.example.bosutar_cosmin.nfc_writer E/MMUMapper﹕ fail to register MVA, unsupported format(0x1)
10-08 19:35:47.986    6649-6649/com.example.bosutar_cosmin.nfc_writer I/SurfaceTextureClient﹕ [0x52024190] frames:6, duration:3.006000, fps:1.995598
10-08 19:35:49.884    6649-6649/com.example.bosutar_cosmin.nfc_writer E/MMUMapper﹕ invalid operation for unregister MVA with VA(0x532fb000) size(88704)
10-08 19:35:49.933    6649-6649/com.example.bosutar_cosmin.nfc_writer I/SurfaceTextureClient﹕ [void android::SurfaceTextureClient::init()] debug.stc.fps: 3000 ms
10-08 19:35:49.950    6649-6649/com.example.bosutar_cosmin.nfc_writer E/MMUMapper﹕ fail to register MVA, unsupported format(0x1)
1
What have you tried so far? What card do you use to authenticate to the card? At which point does that code fail?Michael Roland
I've edited the question. Please have a look there.Cosmin
Is the card empty? Could you successfully write to the card using Ndef (not NdefFormatable)?Michael Roland
No, the card that I'm using right now is not empty. It has been used before but not by me. I believe this this is the source of the problem. I read on some website (I don't remember which one) that after you write a Mifare card the authentication password is changed automatically. Is that right?Cosmin
Tomorrow I'll be able to test with some fresh new cards.Cosmin

1 Answers

0
votes

After all the research I've done I concluded that there is a compatibility problem.

I say this because I managed to read/write NTAG216 cards which have a NTAG21X chip and as far as I know come as a replacement for ABK-1001MF cards which have a MIFARE S50 chip, the first type of cards I tried to read/write, using the same type of phone LG Optimus L5 II E460.

My conclusion is strengthened by the fact that when I call:

getPackageManager().hasSystemFeature("com.nxp.mifare");

the return is false.

Thanks everyone for the help!