繁体   English   中英

了解主机卡仿真和APDU背后的代码

[英]Understanding the code behind Host Card Emulation and APDUs

我刚刚开始制作一个使用主机卡仿真(HCE)的应用程序,并且做了大量的工作。 我需要该应用程序使手机的行为像卡,而另一部手机的行为像NFC读取器,然后对其进行扫描,反之亦然,以便交换非常小的信息-ID号。

我已经完成了一些功能单元,例如ProcessCommandApdu并定义了AID,但我不太了解它是如何工作的。

到目前为止,这是我的代码...

@TargetApi(19)公共类MainActivity扩展了HostApduService {

@Override
public void onDeactivated(int reason) {

}

@Override
public byte[] processCommandApdu(byte[] commandApdu, Bundle extras) {
    String inboundApduDescription;
    byte[] responseApdu;

    if (Arrays.equals(AID_SELECT_APDU, commandApdu)) {
        inboundApduDescription = "Application selected";
        Log.i("HCEDEMO", inboundApduDescription);
        byte[] answer = new byte[2];
        answer[0] = (byte) 0x90;
        answer[1] = (byte) 0x00;
        responseApdu = answer;
        return responseApdu;

    }
    return commandApdu;
}

private static final byte[] AID_SELECT_APDU = {
        (byte) 0x00,
        (byte) 0xA4,
        (byte) 0x04,
        (byte) 0x00,
        (byte) 0x07,
        (byte) 0xF0, (byte) 0x39, (byte) 0x41, (byte) 0x48, (byte) 0x14, (byte) 0x81, (byte) 0x00,
        (byte) 0x00
};

public static void main(String[] args) throws UnsupportedEncodingException {
    TerminalFactory terminalFactory = TerminalFactory.getDefault();
    try {
        for (CardTerminal terminal : terminalFactory.terminals().list()) {
            System.out.println(terminal.getName());
            try {
                Card card = terminal.connect("*");
                CardChannel channel = card.getBasicChannel();
                System.out.println("SelectAID ");
                CommandAPDU command = new CommandAPDU(SelectAID);
                ResponseAPDU response = channel.transmit(command);
                byte recv[] = response.getBytes();
                for (int i = 0; i < recv.length; i++) {
                    System.out.print(String.format("%02X", recv[i]));
                }
                System.out.println("");
            }
            catch (CardException e){}


        }
    }
    catch(CardException e){

    }
}

}

public class ReaderActivity {
public static byte[] SelectAID = new byte[]{(byte) 0x00, (byte) 0xA4, (byte) 0x04, (byte) 0x00,
        (byte) 0x07,
        (byte) 0xF0, (byte) 0x39, (byte) 0x41, (byte) 0x48, (byte) 0x14, (byte) 0x81, (byte) 0x00, (byte)
        0x00
};

}

字节代表什么,例如0x00、0xA4,我该如何通过APDU传达自己的信息(ID号)?

我还需要添加什么?

我的代码基于以下教程https://www.slideshare.net/ChienMingChou/hce-tutorialv-v1

APDU在ISO 7816标准的第4部分中指定-您可以在其中找到每个字节代表什么(类字节,操作代码,参数字节等)。 例如, 0xA4表示SELECT command

我的建议是访问GlobalPlatform网站并下载卡规范-您必须注册,但这是免费的。 该卡规范的第11章包含许多有关APDU编码,最有趣的APDU命令以及管理卡的响应等信息。这将帮助您入门,并为您提供有关可以为应用程序设计哪些APDU命令的想法。

暂无
暂无

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

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