【发布时间】:2014-10-08 16:21:58
【问题描述】:
我正在尝试使用 hello world 小程序运行 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?
【问题讨论】:
-
请更改 new HelloWorldApplet().register(bArray, (short) (bOffset + 1), bArray[bOffset]);到 HelloWorldApplet().register()
-
@AnuragSharma 为什么要忽略实例 AID?