【发布时间】:2015-05-21 14:55:43
【问题描述】:
我有一个应用程序使用智能卡读卡器允许用户访问系统的某些部分。在一个位置我没有问题。但是另一个,拥有不同类型的卡制造商有很多问题。它不断得到一个零的ID。然后查看事件日志,我看到了这个: 这是代码:
card.Connect(reader, SHARE.Shared, PROTOCOL.T0orT1);
var apduGetID = new APDUCommand(0xFF, 0xCA, 0, 0, null, 4);
var apduRespGetID = card.Transmit(apduGetID);
long cardId = BitConverter.ToUInt32(apduRespGetID.Data.Reverse().ToArray(), 0);
第二个问题是,然后尝试调试代码,它运行良好,只有删除断点才能看到问题,但看不到问题在哪里。有人可以帮我吗?
附:我找到了这个帖子,但它不起作用:https://superuser.com/questions/715688/smart-card-errors
更新:这是 Transmit 类
public override APDUResponse Transmit(APDUCommand ApduCmd)
{
var RecvLength = (uint)(ApduCmd.Le + APDUResponse.SW_LENGTH);
byte[] ApduBuffer;
var ApduResponse = new byte[ApduCmd.Le + APDUResponse.SW_LENGTH];
var ioRequest = new SCard_IO_Request
{
m_dwProtocol = m_nProtocol,
m_cbPciLength = 8
};
// Build the command APDU
if (ApduCmd.Data == null)
{
ApduBuffer = new byte[APDUCommand.APDU_MIN_LENGTH + ((ApduCmd.Le != 0) ? 1 : 0)];
if (ApduCmd.Le != 0)
{
ApduBuffer[4] = ApduCmd.Le;
}
}
else
{
ApduBuffer = new byte[APDUCommand.APDU_MIN_LENGTH + 1 + ApduCmd.Data.Length];
for (var nI = 0; nI < ApduCmd.Data.Length; nI++)
{
ApduBuffer[APDUCommand.APDU_MIN_LENGTH + 1 + nI] = ApduCmd.Data[nI];
}
ApduBuffer[APDUCommand.APDU_MIN_LENGTH] = (byte)ApduCmd.Data.Length;
}
ApduBuffer[0] = ApduCmd.Class;
ApduBuffer[1] = ApduCmd.Ins;
ApduBuffer[2] = ApduCmd.P1;
ApduBuffer[3] = ApduCmd.P2;
m_nLastError = SCardTransmit(m_hCard, ref ioRequest, ApduBuffer, (uint)ApduBuffer.Length, IntPtr.Zero, ApduResponse, out RecvLength);
if (m_nLastError != 0)
{
var msg = "SCardTransmit error: " + m_nLastError;
throw new SmartCardException(msg, m_nLastError);
}
var apduData = new byte[RecvLength];
for (var nI = 0; nI < RecvLength; nI++)
{
apduData[nI] = ApduResponse[nI];
}
return new APDUResponse(apduData);
}
更新 2:我也尝试过放一些 Thread.Sleep()
【问题讨论】:
-
card.Transmit是做什么的?也就是说,读卡器或卡的实际响应是什么?您使用的是哪种卡? -
你能在你的命令之间放一些延迟,然后再试一次吗?读卡器或卡可能没有你的程序运行时间那么快?
-
您自己编写了安装在智能卡上的程序吗?您可以将
00A404000004发送到这两种卡片并将卡片的响应添加到问题中吗? (使用此工具向卡发送 APDU 命令:github.com/OpenSC/OpenSC/wiki) -
暂时关闭所有防火墙和杀毒应用程序,然后再次检查问题。
-
您使用的是什么版本的 Windows?