Java Card - Applet选择失败

时间:2014-08-30 10:23:00

标签: applet smartcard javacard

以下是DES加密代码。我收到错误0x6999:“Applet选择失败”。

package JCardDES;

import javacard.framework.*;
import javacard.security.*;
import javacardx.crypto.*;

public class JCard_DES extends Applet
{
     // globals
     DESKey deskey;
     Cipher cipherCBC;
     final short dataOffset = (short) ISO7816.OFFSET_CDATA;
     static byte[] TrippleDESKey = {(byte) 0x38, (byte) 0x12, (byte) 0xA4,
          (byte) 0x19, (byte) 0xC6, (byte) 0x3B, (byte) 0xE7, (byte) 0x71, (byte) 0x00, (byte) 0x12, (byte) 0x00,
          (byte) 0x19, (byte) 0x80, (byte) 0x3B, (byte) 0xE7, (byte) 0x71, (byte) 0x01, (byte) 0x12, (byte) 0x01,
          (byte) 0x01, (byte) 0x01, (byte) 0x03, (byte) 0xE7, (byte) 0x71};

     // constructor,
     // initialization
     private JCard_DES(byte bArray[], short bOffset, byte bLength)
     {
          try {
             deskey = (DESKey) KeyBuilder.buildKey(KeyBuilder.TYPE_DES, KeyBuilder.LENGTH_DES3_3KEY, false);
             cipherCBC = Cipher.getInstance(Cipher.ALG_DES_CBC_NOPAD, false);
          }
          catch (CryptoException e) {
             ISOException.throwIt((short) ((short) 0x9000 + e.getReason()));
          }

          if (bArray[bOffset] == 0) 
          {
               register();
          }
          else 
          {
               register(bArray, (short)(bOffset+1), bArray[bOffset]);
          }
     }

     // install
     public static void install(byte bArray[], short bOffset, byte bLength)
     {
          new JCard_DES(bArray, bOffset, bLength);
     }

     public void process(APDU apdu)
     {
          byte[] buf = apdu.getBuffer();

          if (selectingApplet())
          {
               return;
          }

          doTrippeDES(apdu);
     }

     // DES encryption
     private void doTrippeDES(APDU apdu)
     {
          byte a[] = apdu.getBuffer();

          short incomingLength = (short) (apdu.setIncomingAndReceive());
          if (incomingLength != 24) ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);

          deskey.setKey(TrippleDESKey, (short)0);
          cipherCBC.init(deskey, Cipher.MODE_ENCRYPT,new byte[]{0,0,0,0,0,0,0,0},(short)0,(short)8);
          cipherCBC.doFinal(a, (short) dataOffset, incomingLength, a, (short) (dataOffset + 24));
          cipherCBC.init(deskey, Cipher.MODE_DECRYPT,new byte[]{0,0,0,0,0,0,0,0},(short)0,(short)8);
          cipherCBC.doFinal(a, (short) (dataOffset + 24), incomingLength, a, (short) (dataOffset + 48));

          // send results
          apdu.setOutgoing();
          apdu.setOutgoingLength((short) 72);
          apdu.sendBytesLong(a, (short) dataOffset, (short) 72);
     }
}

这很奇怪,因为没有try / catch块中的初始化代码,选择applet没有问题。

我的脚本文件(APDU脚本)是

powerup;
// Select JCard_DES
0x00 0xA4 0x04 0x00 0X06 0XFC 0X74 0X41 0XA1 0X9B 0X63 0x7F;

因为我是Java Card Programming的新手,所以请指导我哪里出错了

由于

2 个答案:

答案 0 :(得分:0)

您的命令长度(Lc)不正确。它应该是0x07而不是0x06

0x00 0xA4 0x04 0x00 0X07 0XFC 0X74 0X41 0XA1 0X9B 0X63 0x7F

答案 1 :(得分:0)

错误代码0x6999表示applet选择失败。要确定你的援助的正确性和唯一性。注意内部规则有时选择援助会在完成概念时自动出现。