智能卡写错误

时间:2014-03-27 07:24:14

标签: c#-4.0 smartcard windows-applications

我正在从事智能卡开发工作。我在智能卡中创建了MF(主文件),DF(专用文件),EF(基本文件)。 EF文件用于存储数据。该EF可以是透明文件或面向记录的文件。我已经使用00D1000008 540100 5303 010203这个命令将数据写入透明文件。我也尝试使用00DD000008 540100 5303 010203这个命令编写面向记录的文件。但我得到错误(6700错误代码)错误的长度。我需要解决方案来编写智能卡EF记录导向文件。请指导我。

屏幕截图:

enter image description here

我的代码:

我使用了winscard.dll

     private void button_Transmit_Click(object sender, EventArgs e)
         {
        Status.Text = "";
        byte[] baData = null;
        string sClass = textBox_Class.Text;
        string sIns = textBox_CLA.Text;
        string sP1 = textBox_P1.Text;
        string sP2 = textBox_P2.Text;
        string sP3 = textBox_P3.Text;
        sP3 = sP3.ToUpper();
        int k1 = 70;
        string sData = textBox1.Text;
        byte bP1 = 0;
        byte bP2 = 0;
        byte bP3 = 0;
        byte bClass = byte.Parse(sClass, NumberStyles.AllowHexSpecifier);
        byte bIns = byte.Parse(sIns, NumberStyles.AllowHexSpecifier);
        if (sP1 != "" && sP1 != "@")
            bP1 = byte.Parse(sP1, NumberStyles.AllowHexSpecifier);
        if (sP2 != "" && sP2 != "@")
            bP2 = byte.Parse(sP2, NumberStyles.AllowHexSpecifier);

        int integer = int.Parse(sP3, NumberStyles.AllowHexSpecifier);
        byte bLe = (byte)k1;

        if (integer != 0 && sData.Length != 0)
        {
            baData = new byte[integer];

            for (int nJ = 0; nJ < sData.Length; nJ += 2)
                baData[nJ / 2] = byte.Parse(sData.Substring(nJ, 2), NumberStyles.AllowHexSpecifier);
            bLe = 0;

        }
        UInt32 m_nProtocol = (uint)PROTOCOL.Undefined;
        uint RecvLength = 0;
        byte[] ApduBuffer = null;
        IntPtr ApduResponse = IntPtr.Zero;
        SCard_IO_Request ioRequest = new SCard_IO_Request();
        ioRequest.m_dwProtocol = m_nProtocol;
        ioRequest.m_cbPciLength = 8;
        if (baData == null)
        {
            ApduBuffer = new byte[4 + ((bLe != 0) ? 1 : 0)];
            if (bLe != 0)
            {
                ApduBuffer[4] = (byte)bLe;
            }
        }
        else
        {

            if (textBox1.Text.Length > 8)
            {
                ApduBuffer = new byte[5 + baData.Length];
                Buffer.BlockCopy(baData, 0, ApduBuffer, 5, baData.Length);
                ApduBuffer[4] = (byte)(baData.Length);

            }
            //read binary
            else
            {
                ApduBuffer = new byte[5 + baData.Length + 1];
                Buffer.BlockCopy(baData, 0, ApduBuffer, 5, baData.Length);
                ApduBuffer[4] = (byte)(baData.Length);
                ApduBuffer[5 + baData.Length] = 255;
            }
        }
        ApduBuffer[0] = bClass;
        ApduBuffer[1] = bIns;
        ApduBuffer[2] = bP1;
        ApduBuffer[3] = bP2;

        m_nLastError = SCardTransmit(scard.m_hCard, ref ioRequest, ApduBuffer, (uint)ApduBuffer.Length, ref ioRequest, ApduResponse, ref RecvLength);

        textBox2.Text = "";
        byte[] caReadersData = new byte[RecvLength];
        if (m_nLastError == 0)
        {
            ApduResponse = Marshal.AllocHGlobal((int)RecvLength);

            if (m_nLastError == 0)
            {
                m_nLastError = SCardTransmit(scard.m_hCard, ref ioRequest, ApduBuffer, (uint)ApduBuffer.Length, ref ioRequest, ApduResponse, ref RecvLength);
                if (RecvLength > 2)
                {
                    for (int nI = 0; nI < RecvLength - 2; nI++)
                    {
                        caReadersData[nI] = Marshal.ReadByte(ApduResponse, nI);
                        //kl[nI] = Marshal.ReadByte(ApduResponse, nI);
                        //result = string.Format("{0:X02}", caReadersData[nI]);
                        //Status.Text += string.Format("{0:X02}", caReadersData[nI]) + " ";
                        textBox2.Text += string.Format("{0:X02}", caReadersData[nI]) + " ";
                        //result = Status.Text;
                    }

                }
                else
                {
                    for (int nI = 0; nI < RecvLength; nI++)
                    {
                        caReadersData[nI] = Marshal.ReadByte(ApduResponse, nI);
                        Status.Text += string.Format("{0:X02}", caReadersData[nI]) + " ";

                    }
                }
            }
        }

        Marshal.FreeHGlobal(ApduResponse);
}      

编辑:

READ COMMAND工作正常。看屏幕截图

enter image description here

1 个答案:

答案 0 :(得分:3)

似乎您使用未定义的P2编码和奇数指令UPDATE RECORD命令。此外,您必须在P1中指定记录编号。对于三个最低有效位,使用

  • 100 - 替换
  • 101-Logical AND
  • 110-Logical OR
  • 111-Logical XOR

如果您的目标是编写/更新小长度的完整记录,您可以考虑使用带有偶数指令的命令。然后你可以删除偏移量DO(0x54)并仅传输完整记录(自主数据DO(0x53)的值)。