使Java卡应用程序与RB 5.0类似的资源

时间:2015-05-23 20:43:49

标签: java resources javacard jcop

我正在制作java应用程序,主要功能是在java卡36k上写入数据,所以我需要资源,关于java卡的教程,这样的东西。我没有制作Java卡应用程序的经验,所以请给我任何有用的资源..

我正在制作像RB 5.0这样的应用程序,如果你能给我这个应用程序的src代码会更好:)

1 个答案:

答案 0 :(得分:1)

智能卡规格:

  1. Global Platform Card Specification(这是v 2.2.0.7,您的卡可能兼容较低版本)
  2. ISO/IEC 7816(通常需要第3部分和第4部分)
  3. Java Card applet开发工具包(包括API规范+ RE和VM规范):

    与智能卡通信的Java应用程序库:

    • javax.smartcardio(据我所知,它已从较新版本的Java Development Kit中删除)

    一个示例Java卡小程序:(一个HelloWorld,从here被盗):

    package helloWorldPackage;
    
    import javacard.framework.APDU;
    import javacard.framework.Applet;
    import javacard.framework.ISO7816;
    import javacard.framework.ISOException;
    import javacard.framework.Util;
    
    public class HelloWorldApplet extends Applet {
             private static final byte[] helloWorld = {(byte)'H',(byte)'e',(byte)'l',(byte)'l',(byte)'o',(byte)' ',(byte)'W',(byte)'o',(byte)'r',(byte)'l',(byte)'d',};
    
             private static final byte HW_CLA = (byte)0x80;
             private static final byte HW_INS = (byte)0x00;
    
             public static void install(byte[] bArray, short bOffset, byte bLength) {
                 new HelloWorldApplet().register(bArray, (short) (bOffset + 1), bArray[bOffset]);
             }
    
             public void process(APDU apdu) {
    
                 if (selectingApplet()) {
                 return;
             }
    
             byte[] buffer = apdu.getBuffer();
             byte CLA = (byte) (buffer[ISO7816.OFFSET_CLA] & 0xFF);
             byte INS = (byte) (buffer[ISO7816.OFFSET_INS] & 0xFF);
    
             if (CLA != HW_CLA)
            {
                ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
            }
    
              switch ( INS ) {
                 case HW_INS:
                   getHelloWorld( apdu );
                   break;
                default:
                   ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
             }
       }
    
      private void getHelloWorld( APDU apdu)
      {
          byte[] buffer = apdu.getBuffer();
          short length = (short) helloWorld.length;
    
          Util.arrayCopyNonAtomic(helloWorld, (short)0, buffer, (short)0, (short) length);
    
          apdu.setOutgoingAndSend((short)0, length);
      }
    }
    

    与上述applet一起使用的相关Java程序(从here偷来并在之后修改):

    import java.util.List;
    import javax.smartcardio.*;
    
    public class Blog {
     public static void main(String[] args) {
      try {
       // Display the list of terminals
       TerminalFactory factory = TerminalFactory.getDefault();
       List<CardTerminal> terminals = factory.terminals().list();
       System.out.println("Terminals: " + terminals);
    
       // 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();
    
       // Send Select Applet command
       byte[] aid = {(byte)0xA0, 0x00, 0x00, 0x00, 0x62, 0x03, 0x01, 0x0C, 0x06, 0x01}; //Replace your Applet AID instead of this AID here.
       ResponseAPDU answer = channel.transmit(new CommandAPDU(0x00, 0xA4, 0x04, 0x00, aid));
       System.out.println("answer: " + answer.toString());
    
       // Send test command
       answer = channel.transmit(new CommandAPDU(0x00, 0x00, 0x00, 0x00));
       System.out.println("answer: " + answer.toString());
       byte r[] = answer.getData();
       for (int i=0; i<r.length; i++)
        System.out.print((char)r[i]);
       System.out.println();
    
       // Disconnect the card
       card.disconnect(false);
      } catch(Exception e) {
       System.out.println("Ouch: " + e.toString());
      }
     }
    }
    

    您必须将小程序从.java转换为.class并从.class转换为.cap文件。 JCDK包含了必要的工具。为了简化此过程,您可以在 Eclipse IDE (适用于Java Card 2.2.2)中使用Eclipse-JCDE插件或使用Java Card的Netbeans IDE插件(默认情况下包含在较新版本中)。

    生成.CAP文件后,您需要上传并将其安装在卡上。为了实现这一目标,您可以使用名为GlobalPlatformPro的精心设计的开源工具。

    安装完成后,您可以使用上面提到的Java程序与您的applet进行通信,或者您可以使用另一个名为OpenSC-Tool的开源工具将APDU命令发送到卡并接收响应。