我正在编写一个 sim 卡小程序,我需要将数据存储在 sim 卡上。
但我没有这样做。我找到了一个示例并使用它,但是当模拟器重新启动时数据总是消失。我使用“cmdPUTDATA(apdu);” 保存数据的方法,我使用“cmdGETDATA(apdu);” 保存数据的方法。
这是我的代码和响应;
public void process(APDU apdu) {
byte[] buffer = apdu.getBuffer();
if (apdu.isISOInterindustryCLA()) {
if (buffer[ISO7816.OFFSET_INS] == (byte) (0xA4)) {
return;
}
ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
}
switch (buffer[ISO7816.OFFSET_INS]) {
case INS_GET_BALANCE:
getBalance(apdu);
return;
case INS_CREDIT:
credit(apdu);
return;
case INS_CHARGE:
charge(apdu);
return;
// case INS_SELECT: // it is a SELECT FILE instruction
// cmdSELECT(apdu);
// break;
// case INS_VERIFY: // it is a VERIFY instruction
// cmdVERIFY(apdu);
// break;
// case INS_PUTDATA: // it is a PUT DATA instruction
// cmdPUTDATA(apdu);
// break;
// case INS_GETDATA: // it is a GET DATA instruction
// cmdGETDATA(apdu);
// break;
default:
ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
}
}
// @TransactionType(REQUIRED)
//synchronized
private void credit(APDU apdu) {
byte[] buffer = apdu.getBuffer();
byte numBytes = buffer[ISO7816.OFFSET_LC];
byte byteRead = (byte) (apdu.setIncomingAndReceive());
if ((numBytes != 2) || (byteRead != 2)) {
ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
}
short creditAmount = (short) ((short) (buffer[ISO7816.OFFSET_CDATA] << (short) 8) | (buffer[ISO7816.OFFSET_CDATA + 1]));
if ((creditAmount > MAX_BALANCE) || (creditAmount < (short) 0)) {
ISOException.throwIt(SW_INVALID_TRANSACTION_AMOUNT);
}
if ((short) (balance + creditAmount) > MAX_BALANCE) {
ISOException.throwIt(SW_MAX_BALANCE_EXCEEDED);
}
JCSystem.beginTransaction();
balance = (short) (balance + creditAmount);
JCSystem.commitTransaction();
}
private void getBalance(APDU apdu) {
byte[] buffer = apdu.getBuffer();
buffer[0] = (byte) (balance >> (short) 8);
buffer[1] = (byte) (balance & (short) 0x00FF);
//apdu.setOutgoingLength((byte) 2);
//apdu.sendBytes((short) 0, (short) 2);
apdu.setOutgoingAndSend((short)0, (short)2);
}
private void charge(APDU apdu) {
byte[] buffer = apdu.getBuffer();
byte numBytes = buffer[ISO7816.OFFSET_LC];
byte byteRead = (byte) (apdu.setIncomingAndReceive());
if ((numBytes != 2) || (byteRead != 2)) {
ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
}
short chargeAmount = (short) ((short) (buffer[ISO7816.OFFSET_CDATA] << (short) 8) | (buffer[ISO7816.OFFSET_CDATA + 1]));
if ((chargeAmount > MAX_BALANCE) || (chargeAmount < (short) 0)) {
ISOException.throwIt(SW_INVALID_TRANSACTION_AMOUNT);
}
if ((short) (balance - chargeAmount) < 0) {
ISOException.throwIt(SW_MIN_BALANCE_EXCEEDED);
}
JCSystem.beginTransaction();
balance = (short) (balance - chargeAmount);
JCSystem.commitTransaction();
}