Javacard,所有命令返回6E 00

如何解决Javacard,所有命令返回6E 00

当我使用javacardos在调试模式下测试applet时,一切正常,但是当我使用pyAdpuTool下载,安装并选择applet时,所有命令始终返回6E00。我有一个J2A040卡,它是预先个性化的,没有保险丝(00000000),没有保护(00100000)。 小程序已经运行了一两次,然后不再运行。没有命令。

package PackageWallet ;

导入javacard.framework。*;

公共类AppletWallet扩展了Applet {

/* 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;
final static byte CHANGE = (byte) 0x60;

// 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)0x05;
// maximum size PIN
final static byte MAX_PIN_SIZE =(byte)8;
final static byte MIN_PIN_SIZE = (byte)4;

// 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;
// pin is locked
final static short SW_CARD_IS_LOCKED = 0x6304; 
final static short SW_NEW_PIN_TOO_LONG = 0x6307;
final static short SW_NEW_PIN_TOO_SHORT = 0x6308;  
                                        
// 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 AppletWallet (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
    
    pin = new OwnerPIN(PIN_TRY_LIMIT,MAX_PIN_SIZE);
    pin.update(bArray,(short)(bOffset + 1),aLen);
    // Above command causes error.
            
    register();

} // end of the constructor

public static void install(byte[] bArray,byte bLength) {
    // create a Wallet applet instance
    new AppletWallet(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;
    case CHANGE:
        change(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)4);

} // end of getBalance method

private void verify(APDU apdu) {
    
    byte[] buffer = apdu.getBuffer();
     if(pin.getTriesRemaining() == (byte)0)
       ISOException.throwIt(SW_CARD_IS_LOCKED);
       
    // 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

private void change(APDU apdu) {
    
   byte[] buffer = apdu.getBuffer();
   if(pin.getTriesRemaining() == (byte)0)
       ISOException.throwIt(SW_CARD_IS_LOCKED);
       
   if ( ! pin.isValidated() )
       ISOException.throwIt(SW_PIN_VERIFICATION_REQUIRED);
       
    // retrieve the PIN data for validation.
    byte byteRead = (byte)(apdu.setIncomingAndReceive());
    if(byteRead > MAX_PIN_SIZE)
       ISOException.throwIt(SW_NEW_PIN_TOO_LONG);
       
   if(byteRead < MIN_PIN_SIZE)
       ISOException.throwIt(SW_NEW_PIN_TOO_SHORT);
    
   pin.update(buffer,(short) ISO7816.OFFSET_CDATA,(byte)byteRead); 
    
} // end of validate method

} //电子钱包课程结束

你有个主意吗? 谢谢您的帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-