コード例 #1
0
 /**
  * sendCommand
  *
  * @param command
  * @return byte[] response
  * @throws Exception
  */
 public static byte[] sendCommand(byte[] cmd, CardChannel channel) throws Exception {
   if (channel == null) {
     throw new ChannelNotOpenException();
   }
   byte[] rep = null;
   ResponseAPDU r = null;
   try {
     CommandAPDU apdu = new CommandAPDU(cmd);
     try {
       System.out.println("APDU Command: " + BytesTool.byteArrayToHexString(apdu.getBytes()));
     } catch (ByteArrayToHexaStringException e) {
       throw e;
     }
     r = channel.transmit(apdu);
     rep = r.getBytes();
     try {
       System.out.println("APDU Response: " + BytesTool.byteArrayToHexString(rep));
     } catch (ByteArrayToHexaStringException e) {
       throw e;
     }
     return rep;
   } catch (CardException e) {
     e.printStackTrace();
     throw e;
   }
 }
コード例 #2
0
  /**
   * sayHello (SELECT AID APDU ISO 7816-4)
   *
   * @param appletName
   * @param commandId
   * @return boolean
   */
  @Override
  public boolean sayHello(String appletName, int commandHelloId) {
    try {
      AppletModel model = null;
      try {
        model = SeqlMetadata.getInstance(configFile).getAppletByAlias(appletName);
      } catch (Exception e) {
        e.printStackTrace();
      }
      if (model == null) {
        callback.onNoApplet();
      }

      channel = openChannel(model);

      byte[] apdu = null;
      APDUResponse resp = new APDUResponse();
      try {
        // select the applet
        apdu = APDUModel.getSelectAidApdu(BytesTool.hexStringToByteArray(model.getAID()));
        apdu = sendCommand(apdu, channel);
        resp.setResponse(apdu);
        return APDUModel.isResponseSucceded(resp.getStatusWord());
      } catch (Exception e) {
        e.printStackTrace();
        callback.onNoApplet();
      }
    } catch (NoSuchElementException e) {
      callback.onNoApplet();
    } catch (IOException e) {
      callback.onNoSecureElement();
    } catch (NoReaderException e) {
      callback.onNoReader();
    } catch (NoSecureElementException e) {
      callback.onNoSecureElement();
    } catch (BadRequestException e) {
      callback.onBadRequest(e.getMessage());
    } catch (Exception e) {
      callback.onNotConnected();
    }
    return false;
  }