Сбой аутентификации Android Mifare Classic

Я пытаюсь прочитать карту Mifare Classic, и мне не удается выполнить аутентификацию. Я пробовал оба метода аутентификации, A и B.

Я также знаком с этим вопросом, и я также прочитал эту страницу, где вы найдете ссылку на список телефонов, которые не могут читать карты / метки Mifare Classic.

Я использую телефон LG Optimus L5 II E460. Кто-нибудь из вас использовал это?

Есть ли способ проверить внутри моего приложения, может ли телефон считывать карты / метки Mifare Classic?

Есть ли способ заблокировать карту / метку Mifare Classic с помощью личного ключа, а не MifareClassic.KEY_DEFAULT?

Есть ли способ отформатировать карту / метку Mifare Classic? Под форматом я имею в виду стирание всего с карты / тега, как сброс настроек к заводским.

Вот мой код:

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());
    }
}
}

Я попытался использовать NdefFormatable для форматирования карты, чтобы работать с Ndef, но IOException выдается, когда я вызываю функцию NdefFormatable::connect().

Когда я пытаюсь использовать класс MifareClassic, я всегда получаю false при вызове функции authenticateSectorWithKeyB или authenticateSectorWithKeyA.

Я пробовал оба этих метода, потому что когда я вызываю Tag::getTechList(), я получаю следующий список: android.nfc.tech.NfcA, android.nfc.tech.MifareClassic, android.nfc.tech.NdefFormatable;

Вот все журналы:

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 ответ

После всех исследований, которые я провел, я пришел к выводу, что существует проблема совместимости.

Я говорю это потому, что мне удалось прочитать / записать карты NTAG216 с чипом NTAG21X и, насколько я знаю, заменить вместо карт ABK-1001MF с чипом MIFARE S50, первого типа карт, которые я пытался читать / записывать, используя тот же тип телефона LG Optimus L5 II E460.

Мой вывод подкрепляется тем, что когда я звоню:

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

возвращение ложно.

Спасибо всем за помощь!

Другие вопросы по тегам