簡體   English   中英

使用javax.smartcardio從mifare classic中讀取塊

[英]Reading block from mifare classic using javax.smartcardio

我想使用Java的javax.smartcardio閱讀有關Mifare classic的特定內容。 這是我的代碼:

public byte[] getCardUID() throws CardException {
    CardTerminals terminals = TerminalFactory.getDefault().terminals();
    terminal = terminals.list().get(0);
    Card card = terminal.connect("*");
    CardChannel channel = card.getBasicChannel();
    CommandAPDU command = new CommandAPDU( new byte[] { (byte) 0xFF, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x04, (byte) 0xD4, (byte) 0x4A, (byte) 0x01, (byte) 0x00 });
    ResponseAPDU response = channel.transmit(command);
    card.disconnect(true);
    if (response.getSW1() == 0x90) {
        byte[] data = response.getData();
        data = Arrays.copyOfRange(data, 0x08, data.length);
        return data;
    }
    return new byte[] {};
}

此方法(在Internet上找到示例)成功讀取了卡的UID,但是當我嘗試構造自己的命令時,總是出現錯誤SW1 = 63。

在此網站(http://www.acs.com.hk/drivers/eng/API_ACR122U_v2.00.pdf)上,我找到了一些有關APDU的信息,但沒有任何效果,我也找不到原因。 我嘗試了以下命令,但均未成功(總是錯誤63):FF B0 00 04 10(B0-讀取二進制塊,04-扇區數,10-讀取16字節)。 我也嘗試只讀取一個字節,讀取值塊(INS B1),但也沒有成功。

FF 00 00 00 ...(以我的示例為准)應為直接發送,但我不知道以下讀取塊的說明。

誰能幫我? 非常感謝。 (對不起我的英語不好)

在Mifare Classic 1K標簽中,有16個扇區,每個扇區包含4個塊,每個塊包含16個字節。 從頁面讀取或寫入之前,必須必須使用密鑰A或密鑰B對扇區進行身份驗證。身份驗證完成后,您才能進行讀取或寫入。 這是使用該密鑰作為密鑰A(60)的認證命令認證扇區0:

FF 86 0000 05 01 0000 60 00

或使用該密鑰作為密鑰B(61)來驗證扇區0:

FF 86 0000 05 01 0000 61 00

或使用此命令,您還可以驗證扇區0

byte[] authenticationByte = new byte[10];
authenticationByte = new byte[] { (byte) 0xFF, (byte) 0x86, (byte) 0x00,
 (byte) 0x00, (byte) 0x05, (byte) 0x00,(byte) 0x00, (byte) 0x04, 
                                    (byte) 0x60,(byte) 0x00 };

如果身份驗證成功,則您將獲得9000。這是成功消息。 其他響應是63 00,這表示認證失敗。 身份驗證完成后,您可以讀取塊(0,1,2,3),因為扇區0包含4個塊,而它們是塊(0,1,2,3)。

使用此命令可以從扇區0的塊1中讀取數據

byte[] readFromPage = new byte[10];
readFromPage = new byte[] { (byte) 0xFF, (byte) 0x00, (byte) 0x00,
 (byte) 0x00, (byte) 0x05, (byte) 0xD4, (byte) 0x40,
 (byte) 0x00, (byte) 0x30, (byte) 0x01 };

最后一個(字節)0x01是您要讀取的塊。 在此答案中,您可以找到完整的代碼。 只需使用此替換字節值。 謝謝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM