当前位置: 首页 > 知识库问答 >
问题:

如何使用Java或Java applet以编程方式加载Java card applet(.cap文件)

鄢英哲
2023-03-14

如何使用Java或Java applet以编程方式加载Java card applet(.cap文件)。

I'm trying to install an applet (.cap file) into a smart card. I read that can be done using APDU. I created my applet using Netbeans.

我已经使用Netbeans为我的applet创建了一个.cap文件,现在我希望将其安装/加载到物理智能卡中。如果能帮助我如何或使用什么工具将。cap文件安装到卡上,我将不胜感激。还有,我还需要安装什么到卡里。我有两个设备HID OMNIKEY CARDMAN 5x21 1和HID OMNIKEY CARDMAN 3x21 0 java智能卡阅读器,我也有两种卡像java卡(J2A或J3A.)和基本卡(ZC.)注:我不想使用任何第三方软件或工具,我的小程序html" target="_blank">代码是:包智能卡;导入javacard.framework.*;

/*****@author patidar*/public class SmartCard extends Applet{

/**
 * Installs this applet.
 * 
 * @param bArray
 *            the array containing installation parameters
 * @param bOffset
 *            the starting offset in bArray
 * @param bLength
 *            the length in bytes of the parameter data in bArray
 */
/**
 * Only this class's install method should create the applet object.
 */
protected SmartCard() {
    register();
}
/**
 * Processes an incoming APDU.
 * 
 * @see APDU
 * @param apdu
 *            the incoming APDU
 */
/* constants declaration */
// code of CLA byte in the command APDU header
final static byte Wallet_CLA =(byte)0x80;
// codes of INS byte in the command APDU header
final static byte VERIFY = (byte) 0x20;
final static byte CREDIT = (byte) 0x30;
final static byte DEBIT = (byte) 0x40;
final static byte GET_BALANCE = (byte) 0x50;
// maximum balance
final static short MAX_BALANCE = 0x7FFF;
// maximum transaction amount
final static byte MAX_TRANSACTION_AMOUNT = 127;

// maximum number of incorrect tries before the
// PIN is blocked
final static byte PIN_TRY_LIMIT =(byte)0x03;
// maximum size PIN
final static byte MAX_PIN_SIZE =(byte)0x08;

// signal that the PIN verification failed
final static short SW_VERIFICATION_FAILED =
0x6300;
// signal the the PIN validation is required
// for a credit or a debit transaction
final static short SW_PIN_VERIFICATION_REQUIRED =
                                        0x6301;
// signal invalid transaction amount
// amount > MAX_TRANSACTION_AMOUNT or amount < 0
final static short SW_INVALID_TRANSACTION_AMOUNT = 0x6A83;

// signal that the balance exceed the maximum
final static short SW_EXCEED_MAXIMUM_BALANCE = 0x6A84;
// signal the the balance becomes negative
final static short SW_NEGATIVE_BALANCE = 0x6A85;

/* instance variables declaration */
OwnerPIN pin;
short balance;
private SmartCard (byte[] bArray,short bOffset,byte bLength) 
    // It is good programming practice to allocate
    // all the memory that an applet needs during
    // its lifetime inside the constructor
    pin = new OwnerPIN(PIN_TRY_LIMIT,   MAX_PIN_SIZE);

    byte iLen = bArray[bOffset]; // aid length
    bOffset = (short) (bOffset+iLen+1);
    byte cLen = bArray[bOffset]; // info length
    bOffset = (short) (bOffset+cLen+1);
    byte aLen = bArray[bOffset]; // applet data length

    // The installation parameters contain the PIN
    // initialization value
    pin.update(bArray, (short)(bOffset+1), aLen);
    register();

} // end of the constructor

public static void install(byte[] bArray, short bOffset, byte bLength) {
    // create a Wallet applet instance
    new SmartCard(bArray, bOffset, bLength);
} // end of install method

public boolean select() {

    // The applet declines to be selected
    // if the pin is blocked.
    if ( pin.getTriesRemaining() == 0 )
       return false;

    return true;

}// end of select method

public void deselect() {

    // reset the pin value
    pin.reset();

}

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

    // At this point, only the first header bytes
    // [CLA, INS, P1, P2, P3] are available in
    // the APDU buffer.
    // The interface javacard.framework.ISO7816
    // declares constants to denote the offset of
    // these bytes in the APDU buffer

    byte[] buffer = apdu.getBuffer();
    // check SELECT APDU command

    if (apdu.isISOInterindustryCLA()) {
        if (buffer[ISO7816.OFFSET_INS] == (byte)(0xA4)) {
            return;
        } else {
            ISOException.throwIt (ISO7816.SW_CLA_NOT_SUPPORTED);
        }
    }

    // verify the reset of commands have the
    // correct CLA byte, which specifies the
    // command structure
    if (buffer[ISO7816.OFFSET_CLA] != Wallet_CLA)
        ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);

    switch (buffer[ISO7816.OFFSET_INS]) {
    case GET_BALANCE:
        getBalance(apdu);
        return;
    case DEBIT:
        debit(apdu);
        return;
    case CREDIT:
        credit(apdu);
        return;
    case VERIFY:
        verify(apdu);
        return;
    default:
        ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
    }

}   // end of process method

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 != 1 ) || (byteRead != 1) )
        ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);

    // get the credit amount
    byte creditAmount = buffer[ISO7816.OFFSET_CDATA];

    // 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);

} // end of deposit method

private void debit(APDU apdu) {

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

    byte[] buffer = apdu.getBuffer();

    byte numBytes =
        (byte)(buffer[ISO7816.OFFSET_LC]);

    byte byteRead =
        (byte)(apdu.setIncomingAndReceive());

    if ( ( numBytes != 1 ) || (byteRead != 1) )
       ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);

    // get debit amount
    byte debitAmount = buffer[ISO7816.OFFSET_CDATA];

    // check debit amount
    if ( ( debitAmount > MAX_TRANSACTION_AMOUNT)
         ||  ( debitAmount < 0 ) )
       ISOException.throwIt(SW_INVALID_TRANSACTION_AMOUNT);

    // check the new balance
    if ( (short)( balance - debitAmount ) < (short)0 )
         ISOException.throwIt(SW_NEGATIVE_BALANCE);

    balance = (short) (balance - debitAmount);

} // end of debit method

private void getBalance(APDU apdu) {

    byte[] buffer = apdu.getBuffer();

    // 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
    short le = apdu.setOutgoing();

    if ( le < 2 )
       ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);

    //informs the CAD the actual number of bytes
    //returned
    apdu.setOutgoingLength((byte)2);

    // move the balance data into the APDU buffer
    // starting at the offset 0
    buffer[0] = (byte)(balance >> 8);
    buffer[1] = (byte)(balance & 0xFF);

    // send the 2-byte balance at the offset
    // 0 in the apdu buffer
    apdu.sendBytes((short)0, (short)2);

} // end of getBalance method

private void verify(APDU apdu) {

    byte[] buffer = apdu.getBuffer();
    // retrieve the PIN data for validation.
    byte byteRead = (byte)(apdu.setIncomingAndReceive());

    // check pin
    // the PIN data is read into the APDU buffer
    // at the offset ISO7816.OFFSET_CDATA
    // the PIN data length = byteRead
    if ( pin.check(buffer, ISO7816.OFFSET_CDATA,
        byteRead) == false )
        ISOException.throwIt(SW_VERIFICATION_FAILED);

} // end of validate method

共有1个答案

陆雅志
2023-03-14

我也坚持了同样的问题,我找到了解决方案,我将与你分享。

请按照以下步骤操作:

1:请点击这里下载gp.exe

4:现在将您的读卡器连接到机器上,并将卡插入读卡器中。

5:比键入

cmd > gp.exe
cmd > gp –help

如果一切正常,您将看到所有支持的命令:

 类似资料:
  • 问题内容: 我想以编程方式从应用程序中加载Log4j2 XML配置文件。 试过这个: 还有这个: 但是什么都没有。 问题答案: 自己找到答案。有人可能会觉得有用。

  • 如果文件在类路径上,这就是我加载application.properties的方式。 我们正在尝试部署应用程序,其中属性文件存储在外部的Google Cloud bucket(GCS)上。我可以从GCS加载属性文件并将其保存在内存中。如何将属性传递给应用程序上下文并重写从类路径加载的属性?

  • 问题内容: 使用Hibernate,您可以将类加载为: 有没有一种方法可以以符合JPA 2.0的方式以编程方式加载Entity类来做同样的事情? 这个问题的原因是因为我想 动态 加载我的类,因此不必以编程方式加载。 问题答案: 在Spring的帮助下,我以符合JPA的方式进行了此操作。 我的“ persistence.xml”看起来是空的,元素中没有列出任何实体。 然后,我编写了一个实现Persi

  • 问题内容: 我希望能够获取网页的html并将其保存到,因此可以对其进行一些处理。另外,我该如何处理各种类型的压缩。 我将如何使用Java做到这一点? 问题答案: 这是一些使用Java的URL类的经过测试的代码。我建议比在这里处理异常或将异常传递到调用堆栈方面做得更好。

  • 如何加载图像从文件夹以编程方式(如图所示)? 我使用的是Android Studio 1.2.1。

  • 问题内容: 我正在尝试编写一个可以自动登录Facebook的Java程序。 到目前为止,我已经获得了下面的代码,可以将主页html页面下载到String中,但是不知道如何发送电子邮件和密码来登录Facebook?Java程序还需要处理返回的Cookie才能保持登录状态吗? } 更新: 我已经使用htmlUnit尝试了以下代码,但是出现以下异常: 有人知道为什么吗? 问题答案: 您应该看一下HTML