1

我正在开发一个使用 C# 读取 EMV 卡的应用程序。我使用 Winscard.dll 。我建立了连接 - 选择了读卡器 - 连接到卡并获得 ATR。但是当使用 AID 列表发送命令 Slecetd Application 时,我没有收到任何数据,我希望收到 SW1SW2 为 61XX(其中 XX 是数据长度)我发送的命令为 00A4040007A0000000031010 。

代码是:

    // Private/Internal Types
    [StructLayout(LayoutKind.Sequential)]
    internal class SCARD_IO_REQUEST
    {
        internal uint dwProtocol;
        internal int cbPciLength;
        public SCARD_IO_REQUEST()
        {
            dwProtocol = 0;
        }
    }

    public static IntPtr GetPciT0()
    {
        IntPtr handle = LoadLibrary("Winscard.dll");
        IntPtr pci = GetProcAddress(handle, "g_rgSCardT0Pci");
        FreeLibrary(handle);
        return pci;
    }
   // public static byte[] GetSendBuffer(string cla, string ins , string p1 ,string p2 , string lc,string body )

    public byte[] GetSendBuffer()
     {
        // Select application 00A4040007A0000000031010
        string cla = "00";
        string ins = "A4";
        string p1 =  "04";
        string p2 =  "00";
        string lc =  "07";
        string body = "A0000000031010";
        string script = String.Format("{0}{1}{2}{3}{4}{5}", cla, ins, p1, p2,
        lc, body);
        byte[] buffer = new byte[ script.Length / 2];
        //Console.WriteLine("buffer {0}", buffer);
        for (int i = 0; i < script.Length; i = i + 2)
        {

            string temp = script.Substring(i, 2);
            buffer[i / 2] = byte.Parse(temp,
            System.Globalization.NumberStyles.HexNumber);
        }
        return buffer;

    }

//====================================== public void ScardTransmit() { SCARD_IO_REQUEST ioRecv = new SCARD_IO_REQUEST (); 字节[] pbRecvBuffer = 新字节[255]; int pcbRecvLength = 255; byte[] pbsendBuffer = this.GetSendBuffer();

 int pbsendBufLen = pbsendBuffer.Length;
 ioRecv.cbPciLength = ioRecv.ToString().Length;
 IntPtr SCARD_PCI_T0 = GetPciT0();
 uint uintret = SCardTransmit(pCardHandle, ref ioRecv, pbsendBuffer,
 pbsendBufLen, SCARD_PCI_T0, pbRecvBuffer, ref pcbRecvLength);

}

4

1 回答 1

0

尝试仅发送以下内容:

0x00A4040000

那至少应该返回一些东西。最后的字节是可选的。它们确实是有目的的,但只是看看你能不能先从中得到一些东西。

于 2012-04-18T06:13:18.533 回答