首页 > 解决方案 > Java卡通过服务器发送APDU命令

问题描述

我创建了一个服务器来向 java 卡应用程序发送 APDU 命令。

连接已成功建立。我遇到的唯一问题是我成功发送了一个命令,但 Java Card 应用程序没有收到它。

以下代码代表我发送命令的客户端:

public class Client {

public static void main(String[] args) {
    Socket sock;
    try {

        sock = new Socket("localhost", 9025);

        InputStream is = sock.getInputStream();
        OutputStream os = sock.getOutputStream();
        CadClientInterface cad = CadDevice.getCadClientInstance(CadDevice.PROTOCOL_T0, is, os);
        Apdu apdu = new Apdu();
        System.out.println("Initialized apdu !");
        byte[] installer = new byte[]{0x00, (byte) 0xA4, 0x04, 0x00, 0x09, (byte) 0xA0, 0x00, 0x00, 0x00, 0x62, 0x03, 0x01, 0x08, 0x01, 0x7F};
        System.out.println("Prepare installer of cap !");
        apdu.setDataIn(installer, installer.length);
        System.out.println("Apdu set !");


        System.out.println("Apdu sent !");
        System.out.println(apdu);
        cad.powerUp();
        System.out.println("Powered up !");

        cad.exchangeApdu(apdu);

        cad.powerDown();
        System.out.println("Powered down !");
    } catch (IOException | CadTransportException e) {
        e.printStackTrace();
        System.out.println("Fail! " + e.getMessage());
    }

}

}

java卡小程序是IDE创建的一个简单的小程序。

public class Proj extends Applet {

/**
 * Installs this applet.
 * 
 * @param bArray
 *            the array containing installation parameters
 * @param bOffset
 *            the starting offset in bArray
 * @param bLength
 *            the length in bytes of the parameter data in bArray
 */
public static void install(byte[] bArray, short bOffset, byte bLength) {
    new Proj();
}

/**
 * Only this class's install method should create the applet object.
 */
protected Proj() {
    register();
}

/**
 * Processes an incoming APDU.
 * 
 * @see APDU
 * @param apdu
 *            the incoming APDU
 */
@Override
public void process(APDU apdu) {
    //Insert your code here
}

}

在 java 卡中,我打开设备并建立端口。不知道为什么命令发送成功,java卡服务器收不到。

编辑:

我看到了为什么 javacard 没有收到任何数据的问题。问题出在客户内部。当语句cad.powerUp();到达整个客户端块并且不再执行任何其他操作时,就像sleep();调用函数一样。所以现在真正的问题是为什么 cad.powerUp() 会阻止客户端。

标签: javacard

解决方案


我在这里假设您已将完整的小程序代码粘贴到此处。Applet.register()安装时小程序中不调用该方法。因此,applet 从未向 JCRE 注册,因此它无法接收 APDU。

事实上,它不会可供选择,因为 JCRE 没有任何关于它的信息。

用以下代码修改代码并分享结果。

public static void install(byte[] bArray, short bOffset, byte bLength) {
    new Proj();
}

protected Proj(){
    register();
}

另外,请确保安装小程序。


推荐阅读