2014-03-27 86 views
0

我正在開發智能卡。我在智能卡中創建了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); 
}  

編輯:

讀命令工作正常。看到屏幕截圖

enter image description here

+1

兩個想法:新創建的記錄爲空,您需要先使用APPEND RECORD,然後才能使用UPDATE RECORD對其進行修改。在智能卡上有文件類型,記錄大小是固定的。如果是這種情況,並且您的數據與文件的記錄長度不匹配,那麼也將預計爲67 00。 – guidot

+0

APPEND RECORD命令也返回相同的錯誤代碼6700 –

回答

3

看來你使用P2的一個未定義的編碼與奇指令UPDATE RECORD命令。另外,您必須在P1中指定記錄編號。對於這三個最顯著位使用

  • 100更換
  • 101-邏輯與
  • 110-邏輯或
  • 111-邏輯異

如果你的目標寫/更新完整的小長度記錄,你可以考慮在even指令下使用這些命令。然後,您可以刪除偏移量DO(0x54),並僅傳輸完整記錄(任意數據DO(0x53)的值)。

+0

P1和P2值00提到當前的EF。我試過偶指令命令,也返回相同的錯誤代碼6700和6A81(P1 = 01(記錄號)P2 = 05(邏輯與))。 –

+0

您可以提供用於創建面向記錄的文件的CREATE FILE命令的APDU嗎? – ALe

+0

00E0000033 6231 83020101 8A0101 A50381010A 8002035A 8618E2000000FFFFDD000000FFFFB2000000FFFFA4000000FFFF 8205064101AD02 –