2015-06-04 20 views
0

我正在使用JavaCard 2.2.2。JavaCard 2.2.2中ISO7816.SW_PIN_REQUIRED的替代方法是什麼?

以下程序不能正常工作,因爲ISO7816.SW_PIN_REQUIRED未找到任何要自行解析的庫。

如何用其他常數替換ISO7816.SW_PIN_REQUIRED以使程序運行?

package monpackage; 

import javacard.framework.*; 
//import javacardx.framework.*; 

public class Wallet extends Applet 
{ 
/* constants declaration */ 
    // code of CLA byte in the command APDU header 
    final static byte Wallet_CLA =(byte)0xB0; 
    // codes of INS byte in the command APDU header 
    final static byte Deposit = (byte) 0x10; 
    final static byte Debit = (byte) 0x20; 
    final static byte Balance = (byte) 0x30; 
    final static byte Validate = (byte) 0x40; 
    // maximum number of incorrect tries before the 
    // PIN is blocked 
    final static byte PinTryLimit =(byte)0x03; 
    // maximum size PIN 
    final static byte MaxPinSize =(byte)0x04; 
    // status word (SW1-SW2) to signal that the 
    // balance becomes negative; 
    final static short SW_NEGATIVE_BALANCE = (short)0x6910; 
    /* instance variables declaration */ 
    OwnerPIN pin; 
    byte balance; 
    byte buffer[]; // APDU buffer 
    private Wallet() 
    { 
     // It is good programming practice to allocate 
     // all the memory that an applet needs during 
     // its lifetime inside the constructor 
     pin = new OwnerPIN(PinTryLimit, MaxPinSize); 
     balance = 0; 
     register(); 
    } // end of the constructor 

    public static void install(APDU apdu) 
    { 
     // create a Wallet applet instance 
     new Wallet(); 
    } // end of install method 

    public boolean select() 
    { 
     // reset validation flag in the PIN object to 
     // false 
     pin.reset(); 
     // returns true to JCRE to indicate that the 
     // applet is ready to accept incoming APDUs. 
     return true; 
    }// end of select method 

    public void process(APDU apdu) 
    { 
     // APDU object carries a byte array (buffer) to 
     // transfer incoming and outgoing APDU header 
     // and data bytes between card and CAD 
     buffer = apdu.getBuffer(); 
     // verify that if the applet can accept this 
     // APDU message 
     if (buffer[ISO7816.OFFSET_CLA] !== Wallet_CLA) 
     { 
      ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED); 
     } 

     switch (buffer[ISO7816.OFFSET_INS]) 
     { 
      case Balance: getBalance(apdu); return; 
      case Debit: debit(apdu); return; 
      case Deposit: deposit(apdu);return; 
      case Validate: validate(apdu);return 
      default: ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED); 
     } 
    } // end of process method 

    private void deposit(APDU apdu) { 
     // access authentication 
     if (! pin.isValidated()) 
      ISOException.throwIt (ISO7816.SW_PIN_REQUIRED); 
     // Lc byte denotes the number of bytes in the 
     // data field of the comamnd APDU 
     byte numBytes = (byte) (buffer[ISO7816.OFFSET_LC]); 
     // indicate that this APDU has incoming data and 
     // receive data starting from the offset 
     // ISO.OFFSET_CDATA 
     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 (byteRead != 1) 
      ISOException.throwIt(ISO7816.SW_WRONG_LENGTH); 
     // increase the balance by the amount specified 
     // in the data field of the command APDU. 
     balance = (byte) 
      (balance + buffer[ISO7816.OFFSET_CDATA]); 
     // return successfully 
     return; 
     } // end of deposit method 

    private void debit(APDU apdu) { 
     // access authentication 
     if (! pin.isValidated()) 
      ISOException.throwIt(ISO7816.SW_PIN_REQUIRED); 
     byte numBytes = (byte)(buffer[ISO7816.OFFSET_LC]); 
     byte byteRead = 
     (byte)(apdu.setIncomingAndReceive()); 
     if (byteRead != 1) 
      ISOException.throwIt(ISO7816.SW_WRONG_LENGTH); 
     // balance can not be negative 
     if ((balance - buffer[ISO7816.OFFSET_CDATA]) < 0) 
      ISOException.throwIt(SW_NEGATIVE_BALANCE); 
     balance = (byte) 
      (balance - buffer[ISO7816.OFFSET_CDATA]); 
     } // end of debit method 

    private void getBalance(APDU apdu) { 
     // access authentication 
     if (! pin.isValidated()) 
      ISOException.throwIt(ISO7816.SW_PIN_REQUIRED); 
     // inform system that the applet has finished 
     // processing the command and the system should 
     // now prepare to construct a response APDU 
     // which contains data field 
     apdu.setOutgoing(); 
     //indicate the number of bytes in the data field 
     apdu.setOutgoingLength((byte)1); 
     // move the data into the APDU buffer starting 
     // at offset 0 
     buffer[0] = balance; 
     // send 1 byte of data at offset 0 in the APDU 
     // buffer 
     apdu.sendBytes((short)0, (short)1); 
     } // end of getBalance method 
    private void validate(APDU apdu) { 
     // retrieve the PIN data which requires to be 
     // valid ated. The user interface data is 
     // stored in the data field of the APDU 
     byte byteRead = 
     (byte)(apdu.setIncomingAndReceive()); 
     // validate user interface and set the 
     // validation flag in the user interface 
     // object to be true if the validation. 
     // succeeds. If user interface validation 
     // fails, PinException would be thrown from 
     // the pin.check() method. 
     pin.check(buffer, ISO7816.OFFSET_CDATA, byteRead); 
     } // end of validate method 
     } // end of class Wallet 
+0

親愛的布羅,你有什麼其他問題,我想念我的答案? – Abraham

回答

4

退房Java Card 2.2.2 API Specifications - ISO7816 Interface重新。 :)

您會看到在此界面中沒有名爲SW_PIN_REQUIRED的字段。您可以簡單地在程序的常量聲明部分中添加以下行,然後將ISO.SW_PIN_REQUIRED替換爲SW_PIN_REQUIRED

final static short SW_PIN_REQUIRED = 0x6968

此外,您可以使用下面的ISO7816,而不是不確定的領域SW_PIN_REQUIRED已定義的字段:

ISO7816.SW_CONDITIONS_NOT_SATISFIED

ISO7816.SW_SECURITY_STATUS_NOT_SATISFIED

BTW這些都是一些特別短在規範中定義的值,並且您不必在程序中使用相同的值。例如ISO7816.SW_SECURITY_STATUS_NOT_SATISFIED將由編譯器在編譯時間內替換爲0x6982(如API規範中所述),並且當您與卡上的小程序進行通信時,您將看到此值。所以你可以簡單地定義你的特殊狀態詞,而不是使用這些值(不推薦)。

編輯:

由於Bodewes先生在評論中說,我們強烈建議保持在ISO/IEC 7816 - 3 Standard4中定義的可能狀態字範圍(感謝Bodewes先生)。如果我們不這樣做,我們可能會遇到麻煩,特別是對於T = 0(字節定向接觸模式)操作。

從7816-3開始:「如果數值是'6X'或'9X',除'60'外,它是一個SW1字節,它不要求數據傳輸操作,接口設備應等待字符表示SW2字節,SW2值沒有限制。「所以它需要6X或9X才能與T = 0兼容。

+2

我強烈建議保留在ISO 7816-3和4中定義的可能狀態字範圍。如果不這樣做,您可能會遇到麻煩,尤其是對於T = 0(字節定向接觸模式)操作而言。 –

+0

你說得對。在我的一張舊卡(即T = 0工作)中,我今天遇到了0x8080的一些問題,而新的卡(即T = 1工作)成功運行。我會編輯我的答案。謝謝Bodewes先生。 – Abraham

+2

你忘了最後一句話:)。從7816-3開始:「如果數值是'6X'或'9X',除'60'外,它是一個SW1字節,它不要求數據傳輸操作,接口設備應等待一個傳送SW2的字符字節,SW2值沒有限制。「所以它需要6X或9X才能與T = 0兼容。 –

相關問題