【发布时间】:2019-02-01 19:11:53
【问题描述】:
我在 javacard 小程序中声明 BigNumber 数据类型时遇到了问题。如果我只是评论声明,小程序会正确加载到模拟器中。准确的问题是在加载 import.cap 文件时 (jcshell:错误代码:6a80(错误数据))
java card kit 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);
}
}
【问题讨论】:
-
请您也添加安装过程输出吗?您确定您的卡与 Java Card 2.2.2 兼容吗?
-
@Abraham 我正在开发 jcop 模拟器,它是 java card 2.2.2 compatible.cap 文件。已加载 Header.cap,已加载 Directory.cap,加载 Import.cap 失败。状态:错误的数据。 jcshell:错误代码:6a80(错误数据) jcshell:错误响应 APDU:6A80
-
我在NetBeans模拟器上成功加载了你的程序!它工作正常。所以,正如 Roland 先生在他的回答中提到的,我认为这与您的模拟器/卡与数学库包的兼容性有关。
-
我认为迈克尔的答案是正确的答案。因此,您可以使用问题旁边的“v”标记进行检查。
标签: applet smartcard javacard jcop