Java Card “Hello World” Applet

來源:互聯網
上載者:User

In the example below, java card ” Hello World”  applet  is demonstrated.  The applet flow is that when a user sends a spacific APDU to the applet then it will return ‘Hello Word’ to the off-card application or CAD. Firstly, we will test it on JCOP shell and later will write a off-card application which will display word ‘Hello World’.

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);  }}

Output on JcopShell:

First we need to select the newly installed applet => 00 A4 04 00 0E 48 65 6C 6C 6F 57 6F 72 6C 64 2E    .....HelloWorld. 61 70 70 00                                        app. (491682 nsec) <= 90 00                                              .. Status: No Error Sending command with correct CLA and INS which will return Hellow World in hex format cm>  /send 8000000000 => 80 00 00 00 00                                     ..... (556774 nsec) <= 48 65 6C 6C 6F 20 57 6F 72 6C 64 90 00             Hello World.. Status: No Error Sending APDU with wrong CLA which results in 6E00 (CLA value not support  ed) cm>  /send 0000000000 => 00 00 00 00 00                                     ..... (2417 usec) <= 6E 00                                              n. Status: CLA value not supported

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.