简体   繁体   English

Android Mifare Classic身份验证失败

[英]Android Mifare Classic authentication fails

I am trying to read a Mifare Classic card and I fail at the authentication step. 我试图读取Mifare Classic卡,但在身份验证步骤失败。 I've tried both authentication methods, A and B. 我已经尝试过两种身份验证方法A和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. 我也很熟悉这个问题,并且已经阅读了此页面 ,在这里您会找到无法阅读Mifare Classic卡/标签的手机列表的参考。

The phone I'm using is LG Optimus L5 II E460 . 我使用的电话是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? 有没有办法检查我的应用程序内部的手机是否可以读取Mifare Classic卡/标签?

Is there a way to lock a Mifare Classic card/tag with a personal key and not the MifareClassic.KEY_DEFAULT ? 有没有一种方法可以用个人密钥而不是MifareClassic.KEY_DEFAULT锁定Mifare Classic卡/标签?

Is there a way to format a Mifare Classic card/tag? 有没有办法格式化Mifare Classic卡/标签? 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. 我尝试使用NdefFormatable格式化卡以便与Ndef一起使用,但是当我调用NdefFormatable :: connect()函数时抛出IOException

When I try using the MifareClassic class I always receive false when I call function authenticateSectorWithKeyB or authenticateSectorWithKeyA . 当我尝试使用MifareClassic类时,当我调用函数authenticateSectorWithKeyBauthenticateSectorWithKeyA时,我总是收到false

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 ; 我尝试了这两种方法,因为当我调用Tag :: getTechList()时 ,会收到以下列表: android.nfc.tech.NfcAandroid.nfc.tech.MifareClassicandroid.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)

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 . 我之所以这样说,是因为我设法读取/写入具有NTAG21X芯片的NTAG216卡,据我所知,它替代了具有MIFARE S50芯片的ABK-1001MF卡,这是我尝试读取/写入的第一类卡。 ,使用相同类型的手机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! 谢谢大家的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM