本文介绍了在智能卡中选择DF(专用文件),返回错误6981的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个与智能卡通信的程序(Gemalto Company MPCOS applet).我可以成功连接到卡并传输命令并获取数据.

I have written a program to communicate with a smart card (Gemalto Company MPCOS applet).I could successfully connect to card and transmit commands and fetch data.

但是我有一个问题:当我使用00 A4 01 00 02 02 00命令选择DF(专用文件)时,返回错误69 81(文件指示符不正确).

However I have a problem:When I used 00 A4 01 00 02 02 00 command to select DF(Dedicated File),It returned error 69 81 (file indicator is incorrect).

这太奇怪了,因为在此命令之后,我使用了另一个命令来获取该DF的子文件,并且它返回成功61 12.

This is so weird because after this command I used another command to fetch sub-file of this DF and it returned success 61 12.

command1(Select MPCOS Applet): 00 A4 04 00 10 A0 00 00 00 18 30 03 01 00 00 00 00 00 00 00 00
-> response: [97,18] (in decimal) or 6112 (in hex)

command2: 00 C0 00 00 12
-> response: [105,133,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] (in decimal) or
             69 85 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 (in hex)

command3(Select Root): 00 A4 00 00 02 3f 00
-> response: [97,18] (in decimal) or 6112 (in hex)

command4: 00 C0 00 00 12
-> response: [105,133,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] (in decimal) or
             69 85 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 (in hex)

command5(Select DF): 00 A4 01 00 02 02 00
-> response: [105,129] (in decimal) or 6981 (in hex)

command6(Select EF): 00 A4 02 00 02 02 01
-> response: [97,18] (in decimal) or 6112 (in hex)

command7: 00 C0 00 00 12
-> response: [105,133,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] (in decimal) or
             69 85 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 (in hex)

推荐答案

我发现了问题:

该问题是由于两次调用SCardTransmit函数引起的.确实,一次获得响应长度,第二次执行命令并获得响应.

The problem was due to invoking the SCardTransmit function for two times. Indeed, one time to get response length and the second time for execute the command and getting response.

此双重调用导致错误6981:

This dual invoke lead to error 6981:

function SCardTransmitFunc(aCallbackName, myCommand){
    var _SCARD_IO_REQUEST = new CONST.SCARD_IO_REQUEST;
    _SCARD_IO_REQUEST.dwProtocol = AProtocol;
    _SCARD_IO_REQUEST.cbPciLength =  CONST.SCARD_IO_REQUEST.size;
    var myArrayCommand = hex2Dec(myCommand);
    var command = TYPES.LPBYTE.targetType.array(myArrayCommand.length)(myArrayCommand);
    var commandLength = command.length;
    var responseLength = TYPES.DWORD();
    var rez_SCT = SCardTransmit(cardHandle, _SCARD_IO_REQUEST.address(), command, commandLength, null, null, responseLength.address());
    var response = TYPES.LPBYTE.targetType.array(parseInt(responseLength.value))();
    var rez_SCT = SCardTransmit(cardHandle, _SCARD_IO_REQUEST.address(), command, commandLength, null, response, responseLength.address());
    var myResponse = "";//new Array();
    for(i = response.length - 2; i < response.length ; i++)
    {
        myResponse += dec2Hex(response[i]);
    }
}

,更正后的代码是这样:

and the corrected code is this:

function SCardTransmitFunc(aCallbackName, myCommand){
    var _SCARD_IO_REQUEST = new CONST.SCARD_IO_REQUEST;
    _SCARD_IO_REQUEST.dwProtocol = AProtocol;
    _SCARD_IO_REQUEST.cbPciLength =  CONST.SCARD_IO_REQUEST.size;
    var myArrayCommand = hex2Dec(myCommand);
    var command = TYPES.LPBYTE.targetType.array(myArrayCommand.length)(myArrayCommand);
    var commandLength = command.length;
    var responseLength = TYPES.DWORD(1024);
    var response = TYPES.BYTE.array(parseInt(1024))();
    var rez_SCT = SCardTransmit(cardHandle, _SCARD_IO_REQUEST.address(), command, commandLength, null, response, responseLength.address());
    var myResponse = "";//new Array();
    var myLength = parseInt(responseLength.value);
    for(i = myLength - 2; i < myLength ; i++)
    {
        myResponse += dec2Hex(response[i]);
    }
}

我真的非常感谢@guidot的良好暗示和亲爱的@vlp的帮助

I really thanks @guidot for his good hint and dear @vlp for his helps

这篇关于在智能卡中选择DF(专用文件),返回错误6981的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 01:49