Command APDU returning 6985 (Conditions of use not satisfied) in result

杀马特。学长 韩版系。学妹 提交于 2019-12-11 04:14:20

问题


I am working on reading a smart card in Java. When I execute the following code below, the card returns 6985 (Conditions of use not satisfied) as a result.

  TerminalFactory factory = TerminalFactory.getDefault();
  List<CardTerminal> terminals = factory.terminals().list();
  System.out.println("Terminals: " + terminals);

  if (terminals != null && !terminals.isEmpty()) {
   // Use the first terminal
   CardTerminal terminal = terminals.get(0);

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

   CommandAPDU commandAPDU = new CommandAPDU(0x00, 0xA4, 0x00, 0x0C,
   new byte[]{0002},0,0x01);

   ResponseAPDU responseCheck = channel.transmit(commandApdu);
   System.out.println(responseCheck.getSW1()+":"+responseCheck.getSW2()+":"+
   commandApdu.toString());

The parameters provided by the client are:

  • CLA = 00
  • INS = A4
  • P1 = 00
  • P2 = 0C
  • LC = 02
  • Data = XXXX (The data passed here is File Identifier),As I want to select EF file so EFID for the file given by client is 0002

回答1:


CommandAPDU commandAPDU = new CommandAPDU(0x00, 0xA4, 0x00, 0x0C, new byte[]{0002},0,0x01);

won't do what you expect it to do.

new byte[]{0002} will give you a byte array with one byte of value 2. Also, the ,0,0x01); (last two parameters) will make the constructor only pick that one byte from the DATA array. So your APDU will look like this:

+------+------+------+------+------+------+------+
| CLA  | INS  | P1   | P2   | Lc   | DATA | Le   |
| 0x00 | 0xA4 | 0x00 | 0x0C | 0x01 | 0x02 | ---  |
+------+------+------+------+------+------+------+

This is probably not what you expected. Did you want new byte[]{0, 2} instead? Using

CommandAPDU commandAPDU = new CommandAPDU(0x00, 0xA4, 0x00, 0x0C, new byte[]{0, 2}, 256)

would result in the following APDU (note that Le is present and set to 0 (Ne = 256); Lc is automatically infered from the size of the DATA array):

+------+------+------+------+------+-----------+------+
| CLA  | INS  | P1   | P2   | Lc   | DATA      | Le   |
| 0x00 | 0xA4 | 0x00 | 0x0C | 0x02 | 0x00 0x02 | 0x00 |
+------+------+------+------+------+-----------+------+

Or using

CommandAPDU commandAPDU = new CommandAPDU(0x00, 0xA4, 0x00, 0x0C, new byte[]{0, 2})

would result in the following APDU (note that Le is absent (Ne = 0); Lc is automatically infered from the size of the DATA array):

+------+------+------+------+------+-----------+------+
| CLA  | INS  | P1   | P2   | Lc   | DATA      | Le   |
| 0x00 | 0xA4 | 0x00 | 0x0C | 0x02 | 0x00 0x02 | ---  |
+------+------+------+------+------+-----------+------+


来源:https://stackoverflow.com/questions/53274903/command-apdu-returning-6985-conditions-of-use-not-satisfied-in-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!