嗨,我是 java 卡新手,我有以下数据
CLA '00'
INS 'A2' nb not real value
P1 '00'
P2 '00'
LC '08'
Data In 'EF08'
Le '0D'
如何将上述指令写入字节并发送到该函数?我需要获得 9000 作为成功响应并输出数据。
ResponseAPDU respApdu = channel.transmit(
new CommandAPDU(cmd));
最佳答案
有多种方法可以做到这一点:
情况 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));
案例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));
了解更多关于CommandAPDU的信息和 CardChannel .
情况3:(最常用的方式)
String apdu = "00A2000002EF080D"; //also u can append strings into apdu
ResponseAPDU respApdu = channel.transmit(
new CommandAPDU(toByteArray(apdu)));
您需要一个Helper函数:
import javax.xml.bind.DatatypeConverter;
public static byte[] toByteArray(String s) {
return DatatypeConverter.parseHexBinary(s);
}
注意:您显示的示例 APDU 值,LC '08'
表示数据长度为 8 个字节,但您的数据字段只有 2 个字节长。所以再次检查LC
。
关于java - 如何将 apdu 命令传递给命令 apdu 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33215341/