简体   繁体   English

如何将 apdu 命令传递给命令 apdu 功能

[英]how to pass apdu command to command apdu function

Hi am new to java card I have following data您好,我是 Java 卡的新手,我有以下数据

CLA '00'
INS 'A2' nb not real value
P1  '00'
P2  '00'
LC  '08'
Data In 'EF08'
Le   '0D'

How can I write above instructions into bytes and send to this function?如何将上述指令写入字节并发送到此函数? I need to get 9000 as success response and Data out.我需要获得 9000 作为成功响应和数据。

 ResponseAPDU respApdu = channel.transmit(
                                 new CommandAPDU(cmd));

There are multiple ways to do that:有多种方法可以做到这一点:

Case 1: (Not encouraged)案例1:(不鼓励)

int cla = 0x00;
int ins = 0xA2;
int p1  = 0x00;
int p2  = 0x00;
//int LC  = 0x08;'
byte[] data = new byte[] {(byte) 0xEF, (byte) 0x08};
int le  = 0x0D;

ResponseAPDU respApdu = channel.transmit(
                             new CommandAPDU(cla, ins, p1, p2, data, le));

Case 2: (Encouraged)案例2:(鼓励)

byte[] apdu = new byte[] {(byte) 0x00, (byte) 0xA2, (byte) 0x00, (byte)
              0x00, (byte) 0x02, (byte) 0xEF, (byte) 0x08, (byte) 0x0D};

ResponseAPDU respApdu = channel.transmit(
                             new CommandAPDU(apdu));

Read more about CommandAPDU and CardChannel .阅读有关CommandAPDUCardChannel 的更多信息。

Case 3: (Mostly used way)案例3:(最常用的方式)

String apdu = "00A2000002EF080D"; //also u can append strings into apdu
ResponseAPDU respApdu = channel.transmit(
                             new CommandAPDU(toByteArray(apdu)));

You need a Helper function:你需要一个辅助函数:

import javax.xml.bind.DatatypeConverter;
public static byte[] toByteArray(String s) {
    return DatatypeConverter.parseHexBinary(s);
}

Attention: The example APDU values you showed, LC '08' means the data will be 8 bytes long, but your data field is only 2 bytes long.注意:您显示的示例 APDU 值, LC '08'表示数据长度为 8 个字节,但您的数据字段长度仅为 2 个字节。 So check LC again.所以再次检查LC

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

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