我在javacard小程序中声明BigNumber数据类型时遇到了问题。如果我只是注释声明,小程序将正确加载到模拟器中。确切地说,问题是在加载import.cap文件时(jcshell:错误代码:6a80(错误数据))
Java 卡套件 2.2.2 使用
import javacard.framework.APDU;
import javacard.framework.Applet;
import javacard.framework.ISO7816;
import javacard.framework.ISOException;
import javacard.framework.JCSystem;
import javacardx.framework.math.BigNumber;
public class LargeBal extends Applet {
// CLA byte
public static final byte BANK_CLA = (byte) 0x80;
// INS byte
public static final byte INS_GET_BALANCE = 0X02;
public static final byte INS_CREDIT = 0X04;
public static final byte INS_DEBIT = 0X06;
/**
* SW bytes for Arithmetic exception
*/
final static short INVALID_NUMBER_FORMAT = 0x6308;
/**
* Initial account balance
*/
final static byte[] INITIAL_ACCOUNT_BALANCE = { (byte) 0x01, (byte) 0x00,
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
// Amount of money in user's account
private BigNumber accountBalance;
// Big number for temporary calculation
BigNumber tempBigNum;
// temporary buffer used as scratch space
byte[] scratchSpace;
private LargeBal() {
accountBalance = new BigNumber((byte) 8);
// initialize account balance to 100,000.00
accountBalance.init(INITIAL_ACCOUNT_BALANCE, (byte) 0,
(byte) INITIAL_ACCOUNT_BALANCE.length, BigNumber.FORMAT_BCD);
// initialize the temporary big number
tempBigNum = new BigNumber(BigNumber.getMaxBytesSupported());
// initialize the scratchSpace
scratchSpace = JCSystem.makeTransientByteArray((short) 10,
JCSystem.CLEAR_ON_DESELECT);
register();
}
public static void install(byte[] bArray, short bOffset, byte bLength) {
// GP-compliant JavaCard applet registration
new LargeBal();
}
public void process(APDU apdu) {
// Good practice: Return 9000 on SELECT
if (selectingApplet()) {
return;
}
byte[] buf = apdu.getBuffer();
switch (buf[ISO7816.OFFSET_INS]) {
case INS_GET_BALANCE:
getBalance(apdu, buf);
break;
case INS_CREDIT:
break;
case INS_DEBIT:
break;
default:
// good practice: If you don't know the INStruction, say so:
ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
}
}
private void getBalance(APDU apdu, byte[] buffer) {
if (buffer[ISO7816.OFFSET_P1] == BigNumber.FORMAT_BCD) {
accountBalance.toBytes(buffer, (short) 0, (short) 8,
BigNumber.FORMAT_BCD);
} else if (buffer[ISO7816.OFFSET_P1] == BigNumber.FORMAT_HEX) {
accountBalance.toBytes(buffer, (short) 0, (short) 8,
BigNumber.FORMAT_HEX);
} else
ISOException.throwIt(INVALID_NUMBER_FORMAT);
apdu.setOutgoingAndSend((short) 0, (short) 8);
}
}
javacardx.framework.math
是一个 可选的包。因此,并非所有卡/仿真器都实现这一点。在您的情况下,该卡似乎没有实现javacardx.framework.math.BigNumber
。因此,它拒绝加载/安装小程序。
来自运行时环境规范,Java Card 平台,版本 2.2.2(第 9.7 节):
可选扩展包
Java Card 技术中的某些 API 包被指定为扩展 包,并且可能由实现选择性地支持。但是,如果支持, 包及其子包中的所有类都必须由 平台并驻留在卡上。
以下是可选的 Java Card 技术扩展包:
javacardx.apdu
[...]javacardx.biometry
[...]javacardx.crypto
[...]javacardx.external
[...]javacardx.framework
[...]如果实现,此包必须包含所有包含的子包 -util
、math
和tlv
。