Sakthis

Sakthis

  • NA
  • 50
  • 18.6k

smart card write error

Mar 27 2014 2:47 AM
hi,
  i am working in smart card development. i  have created MF (Master file), DF (dedicated File), EF (Elementary file) in smart card. EF file is used to store the data. This EF may be transparent file or record oriented file. i have written the data to transparent file using  00D1000008 540100 5303 010203 this command.i am also try to write the record oriented file using 00DD000008 540100 5303 010203 this command. but i got the error (6700 error code) wrong length. i need solution to write the smart card EF record oriented file. please  guide me.

Screen shot:

 

i have used 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);
  }