簡體   English   中英

我如何在卡上貸記或借記超過1個字節?

[英]How can i credit or debit more than 1 byte to/from card?

我是新手小程序,我通過以下鏈接使用: 與Java Card Wallet一起創建Wallet項目。 我以前可以通過以下命令來刷卡: 80 30 00 00 01 1A 00

我現在想在當前金額上加上“ 5000”。 如您所知,十六進制等於5000

其中'1388'為2個字節。 因此,我必須向卡發送2字節數據13和88。

我創建了波紋管命令並將其發送到卡,但是我收到“ 67 00錯誤長度”作為

響應。

80 30 00 00 02 13 88 00

我如何在卡上貸記或借記超過1個字節?

當然,您必須更改要指向的Applet的代碼:

if ((numBytes != 1) || (byteRead != 1)) {
    ISOException.throwIt(ISO7816.SW_WRONG_LENGTH); // constant with value 0x6700
}

因此,必須確保它允許發送2個字節,然后可以使用Util.getShort方法將這些字節轉換為16位帶符號的值(照常使用大端2補碼表示法)。

用此方法替換creadit()方法。 但是請記住,從今以后您必須使用兩個字節的值來記賬。 (即使對於小於255或0xFF的值。即,您必須使用0x00FF借記255 $借記您的錢包)

private void credit(APDU apdu) {

    // access authentication
    if (!pin.isValidated()) {
        ISOException.throwIt(SW_PIN_VERIFICATION_REQUIRED);
    }

    byte[] buffer = apdu.getBuffer();

    // Lc byte denotes the number of bytes in the
    // data field of the command APDU
    byte numBytes = buffer[ISO7816.OFFSET_LC];

    // indicate that this APDU has incoming data
    // and receive data starting from the offset
    // ISO7816.OFFSET_CDATA following the 5 header
    // bytes.
    byte byteRead = (byte) (apdu.setIncomingAndReceive());

    // it is an error if the number of data bytes
    // read does not match the number in Lc byte
    if ((numBytes != 2) || (byteRead != 2)) {
        ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
    }

    // get the creditBytes
    byte[] creditBytes = new byte[2];
    creditBytes[0]=buffer[ISO7816.OFFSET_CDATA];
    creditBytes[1]=buffer[ISO7816.OFFSET_CDATA+1];

    // convert 2 byte of creatBytes to a single short value.
    short creditAmount = Util.getShort(creditBytes,(short)0);

    // check the credit amount
    if ((creditAmount > MAX_TRANSACTION_AMOUNT) || (creditAmount < 0)) {
        ISOException.throwIt(SW_INVALID_TRANSACTION_AMOUNT);
    }

    // check the new balance
    if ((short) (balance + creditAmount) > MAX_BALANCE) {
        ISOException.throwIt(SW_EXCEED_MAXIMUM_BALANCE);
    }

    // credit the amount
    balance = (short) (balance + creditAmount);

}

我建議使用BCD加法和BCD減法,如下所示:

  1. 每個字節代表兩個BCD,例如0x99代表99而不是153。
  2. 加和減中包括的所有數據應具有相同的長度,例如6個字節將代表12位數字。 這應該涵蓋大多數情況,但是如果您需要更多,只需更改常數即可。
  3. 您的applet在字節之間執行循環以執行加法或減法。 從BCD到該值進行編碼和解碼操作,反之亦然。

這是實現示例。 它尚未經過測試,但是應該讓您了解它的工作原理:

public class BCD {

    public static final short NUMBER_OF_BYTES = 6;

    static void add(byte[] augend, byte[] addend, byte[] result) {
        byte carry = 0;
        short temp = 0;
        for (short i = (short) (NUMBER_OF_BYTES - 1); i >= 0; i--) {
            temp = (short) (decode(augend[i]) + decode(addend[i]) + carry);
            carry = (byte) ((temp > 100) ? 1 : 0);
            result[i] = encode((byte) temp);
        }

        if (carry == 1) {
            // TODO: result more than maximum
            // you can set all digits to 9 or throw exception
        }
    }

    static void subtract(byte[] minuend, byte[] subtrahend, byte[] result) {
        byte borrow = 0;
        short temp = 0;
        for (short i = (short) (NUMBER_OF_BYTES - 1); i >= 0; i--) {
            temp = (short) (100 + decode(minuend[i]) - decode(subtrahend[i]) - borrow);
            borrow = (byte) ((temp < 100) ? 1 : 0);
            result[i] = encode((byte) temp);
        }

        if (borrow == 1) {
            // TODO: subtrahend > minuend, 
            // you can set all digits to 0 or throw exception
        }
    }

    static byte encode(byte value) {
        value %= 100; // only convert two digits, ignore borrow/carry
        return (byte) (((value / 10) << 4) | (value % 10));
    }

    static byte decode(byte bcdByte) {
        byte highNibble = (byte) ((bcdByte >> 4) & 0x0F);
        byte lowNibble = (byte) (bcdByte & 0x0F);

        if ((highNibble > 9) || (lowNibble > 9)) {
            // found 'A' to 'F' character which should be invalid
            // you can change this line, e.g. throwing exception
            return 0;
        }

        return (byte) ((highNibble * 10) + lowNibble);
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM