我正在尝试使用Hello World Applet运行jwcde模拟。我用的是JC221。
这是小程序:
package hello;
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);
}
}
我设法用以下命令编译它:
javac -g -d hello -classpath “.;%JC_HOME%\lib\api.jar;%JC_HOME%\lib\javacardframework.jar” HelloWorldApplet.java
我检查了编译后的类文件是否在路径上。
这是jcwde使用的jcwde.app文件:
//+
// Copyright © 2003 Sun Microsystems, Inc. All rights reserved.
// SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
//-
//+
// Workfile:@(#)jcwde.app 1.4
// Version:1.4
// Date:07/16/03
//-
// applet AID
com.sun.javacard.installer.InstallerApplet 0xa0:0x0:0x0:0x0:0x62:0x3:0x1:0x8:0x1
com.sun.javacard.samples.JavaPurse.JavaPurse 0xa0:0x0:0x0:0x0:0x62:0x3:0x1:0xc:0x2:0x1
com.sun.javacard.samples.JavaLoyalty.JavaLoyalty 0xa0:0x0:0x0:0x0:0x62:0x3:0x1:0xc:0x5:0x1
com.sun.javacard.samples.wallet.Wallet 0xa0:0x0:0x0:0x0:0x62:0x3:0x1:0xc:0x6:0x1
hello.HelloWorldApplet 0x01:0x02:0x03:0x04:0x05:0x06:0x07:0x08:0x09:0x00:0x01
我在命令行窗口(Windows7)中使用以下命令启动jcwde:
jcwde jcwde.app
然后,我启动一个新的命令行窗口并在那里启动apdutool:
apdutool -nobanner -noatr HelloWorldApplet.scr > hello.scr.jcwde.out
这是.scr脚本文件:
充电;
// Select the installer applet
0x00 0xA4 0x04 0x00 0x09 0xa0 0x00 0x00 0x00 0x62 0x03 0x01 0x08 0x01 0x7F;
// 90 00 = SW_NO_ERROR
// create Applet
0x80 0xB8 0x00 0x00 0x0d 0x0b 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x00 0x01 0x00 0x7F;
// Select Applet
0x00 0xa4 0x04 0x00 0x0b 0x01 0x02 0x03 0x04 0x05 0x6 0x07 0x08 0x09 0x00 0x01 0x7F;
// Send APDU command to get a Hello World message
0x80 0x00 0x00 0x00 0x00 0x00;
powerdown;
这是结果(来自jcwde命令窗口):
D:\jc221\samples\src\demo\jcwde>jcwde jcwde.app
Java Card 2.2.1 Workstation Development Environment, Version 1.3
Copyright 2003 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms.
jcwde is listening for T=0 Apdu's on TCP/IP port 9á025.
Exception from the invoked install() method:public static void hello.HelloWorldApplet.install(byte[],short,byte)
jcwde exiting on receipt of power down command.
D:\jc221\samples\src\demo\jcwde>
这是hello.scr.jcwde.out文件中的结果:
CLA: 00, INS: a4, P1: 04, P2: 00, Lc: 09, a0, 00, 00, 00, 62, 03, 01, 08, 01, Le: 00, SW1: 90, SW2: 00
CLA: 80, INS: b0, P1: 00, P2: 00, Lc: 00, Le: 00, SW1: 90, SW2: 00
CLA: 80, INS: b8, P1: 00, P2: 00, Lc: 0d, 0b, 01, 02, 03, 04, 05, 06, 07, 08, 09, 00, 01, 00, Le: 00, SW1: 64, SW2: 44
CLA: 80, INS: ba, P1: 00, P2: 00, Lc: 00, Le: 00, SW1: 90, SW2: 00
CLA: 00, INS: a4, P1: 04, P2: 00, Lc: 0b, 01, 02, 03, 04, 05, 06, 07, 08, 09, 00, 01, Le: 00, SW1: 6d, SW2: 00
CLA: 80, INS: 00, P1: 00, P2: 00, Lc: 00, Le: 00, SW1: 6d, SW2: 00
所以问题是为什么它会引发异常:调用的install()方法中的异常:public static void hello.HelloWorldApplet.install(byte [],short,byte)
为什么结果SW在第三行SW1:64 SW2:44而不是应该的90 00?
最佳答案
此问题的根源似乎是JCWDE中的bug(?)。尽管Java Card规范要求install参数至少包含长度字节(甚至用于指示零长度),但JCWDE似乎使用通过脚本获取的任何安装参数。因此,如果未明确输入安装参数,则字节数组bArray
的长度为零。因此,访问bArray[bOffset]
将导致IndexOutOfBounds
异常。因此,检查bArray
/ bLength
的长度并调用其中一个可能是一个好主意
register(bArray, (short) (bOffset + 1), bArray[bOffset]);
要么
register();
相应地。