C# 4.0 智能卡写入错误

C# 4.0 智能卡写入错误,c#-4.0,smartcard,windows-applications,C# 4.0,Smartcard,Windows Applications,我从事智能卡开发工作。我在智能卡中创建了MF(主文件)、DF(专用文件)、EF(基本文件)。EF文件用于存储数据。此EF可以是透明文件或面向记录的文件。我已经使用00D1000008 540100 5303 010203此命令将数据写入透明文件。我还尝试使用00DD000008 540100 5303 010203此命令写入面向记录的文件。但我得到的错误(6700错误代码)长度错误。我需要解决方案来写智能卡EF记录导向文件。请引导我 屏幕截图: 我的代码: 我使用了winscard.dll

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

屏幕截图:

我的代码:

我使用了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);
}      
private void按钮\u传输\u单击(对象发送者,事件参数e)
{
状态。Text=“”;
字节[]baData=null;
字符串sClass=textBox\u Class.Text;
字符串sIns=textBox\u CLA.Text;
字符串sP1=textBox\u P1.Text;
字符串sP2=textBox\u P2.Text;
字符串sP3=textBox_P3.Text;
sP3=sP3.ToUpper();
int k1=70;
字符串sData=textBox1.Text;
字节bP1=0;
字节bP2=0;
字节bP3=0;
byte bClass=byte.Parse(sClass,NumberStyles.allowehexspecifier);
byte bIns=byte.Parse(sIns,NumberStyles.allowehexspecifier);
如果(sP1!=”&&sP1!=“@”)
bP1=byte.Parse(sP1,NumberStyles.AllowHexSpecifier);
如果(sP2!=”&&sP2!=“@”)
bP2=byte.Parse(sP2,NumberStyles.allowehexspecifier);
int integer=int.Parse(sP3,NumberStyles.allowehexspecifier);
字节bLe=(字节)k1;
if(整数!=0&&sData.Length!=0)
{
baData=新字节[整数];
对于(int nJ=0;nJ8)
{
ApduBuffer=新字节[5+baData.Length];
块复制(baData,0,ApduBuffer,5,baData.Length);
ApduBuffer[4]=(字节)(baData.Length);
}
//读取二进制文件
其他的
{
ApduBuffer=新字节[5+baData.Length+1];
块复制(baData,0,ApduBuffer,5,baData.Length);
ApduBuffer[4]=(字节)(baData.Length);
ApduBuffer[5+baData.Length]=255;
}
}
ApduBuffer[0]=b类;
ApduBuffer[1]=料仓;
ApduBuffer[2]=bP1;
ApduBuffer[3]=bP2;
m_nLastError=SCardTransmit(scard.m_hCard,参考文献,参考文献,(uint)参考文献,长度,参考文献,响应文献,参考文献长度);
textBox2.Text=“”;
byte[]caReadersData=新字节[RecvLength];
如果(m_nLastError==0)
{
ApduResponse=Marshal.AllocHGlobal((int)RecvLength);
如果(m_nLastError==0)
{
m_nLastError=SCardTransmit(scard.m_hCard,参考文献,参考文献,(uint)参考文献,长度,参考文献,响应文献,参考文献长度);
如果(接收长度>2)
{
对于(int-nI=0;nI
编辑:

读取命令工作正常。看到屏幕截图了吗


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

  • 100更换
  • 101逻辑与
  • 110逻辑或
  • 111逻辑异或

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

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

  • 100更换
  • 101逻辑与
  • 110逻辑或
  • 111逻辑异或

如果你想编写/更新完整的小长度记录,你可以考虑使用指令连指令。然后您可以删除偏移量DO(0x54),只传输完整的记录(任意数据DO的值(0x53))。

似乎您使用了带有奇数指令的未定义的P2编码