从智能卡(CNS / CNR)读取数据

时间:2018-10-17 20:49:38

标签: java smartcard javacard apdu smartcardio

我使用Java卡编写了一个程序,该程序允许我从智能卡读取一些数据。卡的文件系统的结构如下:

Smart Card File System

智能卡是意大利公共行政部门的卡。

我可以正确连接到卡,通过运行以下代码将数据读取命令发送到路径 MF / DF1 / EF_Dati_Personali路径

当我尝试输入 DF2 / Dati_personali_aggiuntivi 时,我找不到任何数据。

根据参考指南,访问DF1和DF2区域的扇区如下:

Data sectors

下面是我编写的代码。在行中注释代码以获取数据以识别问题。

有人可以告诉我我错了吗?每个建议都表示赞赏。非常感谢

package smartcard;

import java.io.IOException;
import static java.lang.System.out;
import java.util.List;
import javax.smartcardio.ATR;
import javax.smartcardio.Card;
import javax.smartcardio.CardChannel;
import javax.smartcardio.CardException;
import javax.smartcardio.CardTerminal;
import javax.smartcardio.CommandAPDU;
import javax.smartcardio.ResponseAPDU;
import javax.smartcardio.TerminalFactory;
import javax.swing.JOptionPane;

public class SmartCard {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws CardException, IOException {
        try {
             TerminalFactory factory = TerminalFactory.getDefault();
            List<CardTerminal> terminals = factory.terminals().list();
            System.out.println("Terminals: " + terminals);

            // Use the first terminal
            CardTerminal terminal = terminals.get(0);

            // Connect wit hthe card
            Card card = terminal.connect("*");
            System.out.println("card: " + card);
            CardChannel channel = card.getBasicChannel();

            //GET ATR
            ATR atr = card.getATR();

            byte[] ATR = atr.getBytes();
            System.out.println("Card ATR: " + bytesToHex(ATR));
           //   // originale

            //GET SELECT_FILE_APDU
            byte[] READ_BINARY_APDU = {(byte) 0x00, (byte) 0xB0, (byte) 0x00, (byte) 0x00, (byte) 0xff}; 

            byte[] dati_personali = {(byte) 0x00, (byte) 0xA4, (byte) 0x08, (byte) 0x00, (byte) 0x04, (byte) 0x11, (byte) 0x00, (byte) 0x11, (byte) 0x02, (byte) 0x00};
            // Whit this String I can correctly read DF1 Data of CNS (Carta Nazionale servizi)

            // This should be the string for get DF2 data but I cannot find anything.
            byte[] dati_personali_aggiuntivi = {(byte) 0x00, (byte) 0xA4, (byte) 0x08, (byte) 0x00, (byte) 0x04, (byte) 0x12, (byte) 0x00, (byte) 0x12, (byte) 0x01, (byte) 0x00};

            String dati_personali_string = richiedi(channel, READ_BINARY_APDU, dati_personali, "<b>Dati personali:</b><br>");
            String dati_personali_aggiuntivi_string = richiedi(channel, READ_BINARY_APDU, dati_personali_aggiuntivi, "<b>Dati personali aggiuntivi:</b><br>");
            JOptionPane.showMessageDialog(null,"dati personali: "+ dati_personali_string, "Dati personali",JOptionPane.INFORMATION_MESSAGE);
            JOptionPane.showMessageDialog(null,"dati personali agiguntivi: "+ dati_personali_aggiuntivi_string, "Dati personali aggiuntivi",JOptionPane.INFORMATION_MESSAGE);


            // Disconnect the card
            card.disconnect(false);
            System.out.println("DISCONEESSO ");

        } catch (Exception e) {
            System.out.println("Ouch: " + e.toString());
        }

    }

    public static String richiedi(CardChannel channel, byte[] read, byte[] select, String titolo) throws CardException {
        out.println(titolo);
        // Send Select Applet command
        ResponseAPDU answer = channel.transmit(new CommandAPDU(select));
        // Send test command
        answer = channel.transmit(new CommandAPDU(read));
        byte r[] = answer.getData();
        String test = "";
        for (int i = 0; i < r.length; i++) {
            test += (char) r[i];
        }
        System.out.print(test);
        out.println(test);
        out.println("<br><br>");
        return test;
    }

    public static String bytesToHex(byte[] bytes) {
        StringBuilder sb = new StringBuilder(bytes.length * 2);
        for (int i = 0; i < bytes.length; i++) {
            sb.append(String.format("%02x", bytes[i]));
        }
        return sb.toString();
    }
}

更新APDU跟踪

REQUEST: Dati personali:
read command: >>> 00b00000ff
select command: >>> 00a40800041100110200

RESPONSE: Dati personali:
<<< answer from CNS: 303030303733303436303330303830373039323031303038303730393230313630384954414c49414e4f3130414e544f4e494f20435249535449414e3038323230343139383230314d30303130544c4e4e4e43383244323246323035493030303446323035303030303034463230353030303000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009000

_____________________
REQUEST: Dati personali aggiuntivi:
read command: >>> 00b00000ff
select command: >>> 00a40800041200120100

RESPONSE: Dati personali aggiuntivi:
<<< answer from CNS: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009000

1 个答案:

答案 0 :(得分:1)

根据给定的跟踪,您的代码可以正常工作,并且已成功读取卡中的数据-基本文件'DF2 / Dati_personali_aggiuntivi'中填充了零。

the documentation(第4.3节)一致:

  

EF.Dati_personali_aggiuntivi –互译为“ 00”十六进制

使用Google翻译进行翻译:

  

EF.Additional_personal_dates-整个内容设置为'00'hex

一些附加说明:

  • 'EF.Dati_personali'文件包含400个字节-您可能需要使用多个'READ BINARY'命令才能全部获取

  • 始终检查APDU响应状态('90 00')-ResponseAPDU.getSW()对此很有用

祝您的项目好运!

编辑> OpenSC支持CNS卡(请参阅herehere),您可能需要尝试一下