传输智能卡数据时出错 [英] Error Transmiting smart card data

查看:74
本文介绍了传输智能卡数据时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是智能阅读器技术的新手.我正在开发一个简单的应用程序,以使用.NET和c#从智能卡读取数据.电话是带有生物识别附件的MC75A,而sdk是BSCR sdk 2.2版.我正在使用sdk提供的WinSCard,后者是winscard.dll的包装.

在下面发布的代码中,我正在连接至卡并尝试从智能卡获取ID.我得到了apdu命令,可从Cardman_5x21-CL_Reader_Developers_Guide的card中检索UID.提到此功能适用于所有符合ISO 14443 A/B或ISO 15693的卡.我正在使用的卡可以进行PIV互操作.我已经尝试过各种方法,但是我一直停留在WinSCard.Transmit命令的位置,并给出错误代码:80100069.我无法弄清楚.有人请指导我我做错了什么吗?该代码粘贴在下面.提前谢谢.

I am new to smart reader tech. I am developing a simple application to read data from smart card using .NET and c#. The phone is MC75A with biometric attachment and sdk is BSCR sdk version 2.2. I am using the WinSCard provided by the sdk which is a wrapper for winscard.dll.

In the code posted below, I am connecting to the card and trying to get the id from the smart card. I got the apdu command to retrieve UID from card from the Cardman_5x21-CL_Reader_Developers_Guide. It is mentioned that this works with every ISO 14443 A/B or ISO 15693 compliant card. The card I am using is PIV-Interoperable. I have tried various things but I am stuck at the point where it goes to WinSCard.Transmit command and gives error code: 80100069. I am not able to figure it out. Anyone please direct me what I am doing wrong? The code is pasted below. Thanks in advance.

private void ConnectCard()
        {
             try
              {
                  IntPtr hContext = IntPtr.Zero;
                  uint scardError = 0;
                  UpdateSCStatus("Establishing Context ...");
                  scardError = WinSCard.EstablishContext((uint)WinSCard.SCOPE.USER, IntPtr.Zero, IntPtr.Zero, out saved_hContext);
                  if (scardError != (uint)WinSCard.STATUS_RESULT.S_SUCCESS)
                  {
                      UpdateSCStatus("failed: 0x" + scardError.ToString("X8"));
                      return;
                  }
                  saved_Reader = (string)scComboBox2.SelectedItem;
                  UpdateSCStatus("Connecting ...");
                  MessageBox.Show(saved_Reader);
                  scardError = WinSCard.Connect(saved_hContext, saved_Reader, (uint)WinSCard.SHARE.EXCLUSIVE, (uint)WinSCard.PROTOCOL.T0, out saved_card, out saved_protocol);
                  if (scardError != 0)
                  {
                      saved_Reader = null;
                      saved_card = IntPtr.Zero;
                      saved_protocol = 0;
                      UpdateSCStatus("failed: 0x" + scardError.ToString("X8"));
                  }
                  else
                  {
                      UpdateSCStatus("Connected to Card.");
                      MessageBox.Show("Before calling submitIC(), saved_card: " + saved_card);
                      MessageBox.Show("Before calling submitIC(), saved_protocol: " + saved_protocol);
                      scardError = WinSCard.BeginTransaction(saved_card);
                      if (scardError == 0)
                      {
                          MessageBox.Show("Transaction started.");
                          SubmitIC();
                          UpdateSCStatus("Disconnecting Card....");
                          WinSCard.EndTransaction(saved_card, 0);
                          WinSCard.Disconnect(saved_card, (uint)WinSCard.CLOSE.UNPOWER_CARD);
                      }
                      else
                      {
                          UpdateSCStatus("failed: 0x" + scardError.ToString("X8"));
                      }
                  }
                  return;
              }
              catch (Exception ex)
              {
                  UpdateSCStatus(string.Format("Exception:\r\n" + ex.Message));
                  return;
              }
        }
         
    public int SubmitIC()
    {
        MessageBox.Show("in submitIC");
      int nResult = 1;
      try
      {
          byte[] defaultIssuerCode = { 0x5C, 0x03, 0x5F, 0xC1, 0x09, 0x00 };
          nResult = ExecuteCommand_Class2(0xFF, 0xCA, 0x00, 0x00, 0x00, defaultIssuerCode);
      }
      catch (Exception generalException)
      {
          LogError(1, generalException.Message);
      }
      return nResult;
    }
    private int ExecuteCommand_Class2(byte CLA, byte INS, byte P1, byte P2, byte Lc, byte[] Data)
    {
        MessageBox.Show("ExecuteCommand_Class2");
      int nResult = 0;
      if (saved_card != IntPtr.Zero)
      {
        byte[] bCommand = new byte[0x05 + Lc];
        byte[] bResponse = new byte[512];
        uint dwResponseLength = 512;
        WinSCard.IO_REQUEST cardIORequest = new WinSCard.IO_REQUEST();
        WinSCard.IO_REQUEST receiveIORequest = new WinSCard.IO_REQUEST();
    
        bCommand[0] = CLA;
        bCommand[1] = INS;
        bCommand[2] = P1;
        bCommand[3] = P2;
        bCommand[4] = Lc;
        uint blnCommand = 5;
        for (byte i = 0x00; i < Lc; i++)
          bCommand[i + 0x05] = Data[i];
        cardIORequest.dwProtocol = saved_protocol;
        cardIORequest.cbPciLength = (uint)(0x05 + Lc);
        receiveIORequest.dwProtocol = saved_protocol;
        receiveIORequest.cbPciLength = 512;
          nResult = (int)WinSCard.Transmit(saved_card, ref cardIORequest, bCommand, blnCommand, ref receiveIORequest, bResponse, ref dwResponseLength);
          UpdateSCStatus("result after Transmit: " + nResult.ToString("X8"));
          if (nResult == 0)
        {
            MessageBox.Show("in nResult block");
          if (bResponse[dwResponseLength - 2] == 0x90 && bResponse[dwResponseLength - 1] == 0x00)
          {
          }
          else if (bResponse[dwResponseLength - 2] == 0x61)
          {
          }
          else if (bResponse[dwResponseLength - 2] == 0x91)
          {
            // User Data File has been selected
          }
          else if (bResponse[dwResponseLength - 2] == 0x63)
          {
            nResult = 1;
          }
          else if (bResponse[dwResponseLength - 2] == 0x67)
          {
            nResult = 1;
          }
          else if (bResponse[dwResponseLength - 2] == 0x69 && bResponse[dwResponseLength - 1] == 0x66)
          {
            nResult = 1;
          }
          else if (bResponse[dwResponseLength - 2] == 0x69 && bResponse[dwResponseLength - 1] == 0x81)
          {
            nResult = 1;
          }
        }
        else
          LogError((uint)nResult, "Transmit");
      }
      return nResult;
    }

推荐答案

您看过这里吗? http://www.codeguru.com/forum/showthread.php?t=450642(尽管在VB中.)

他们讨论了SCARD_W_REMOVED_CARD错误,以及由于调用函数所使用的值类型不正确而可能导致的错误.
Have you looked here? http://www.codeguru.com/forum/showthread.php?t=450642 (Though it''s in VB. )

They talk about the SCARD_W_REMOVED_CARD error and how it might be because of the value types used in calling the functions are not correct.


这篇关于传输智能卡数据时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆