简体   繁体   English

Java卡-Applet选择失败

[英]Java Card - Applet selection failed

Below is DES encryption code. 以下是DES加密代码。 I am getting error 0x6999: "Applet selection failed". 我收到错误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);
     }
}

That's strange, because without the initialization codes in try/catch block, there is no problem to select the applet. 这很奇怪,因为在try / catch块中没有初始化代码,选择小程序没有问题。

My script file (APDU script) is 我的脚本文件(APDU脚本)是

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

Please guide me where i'm going wrong since i'm new to Java Card Programming 由于我是Java Card编程的新手,请指导我哪里出错了

Thanks 谢谢

Your command length (Lc) is incorrect. 您的命令长度(Lc)不正确。 It should be 0x07 instead of 0x06 . 它应该是0x07而不是0x06

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM