【问题标题】:how to write a data to the mifare classic tag如何将数据写入 mifare 经典标签
【发布时间】:2015-06-11 03:57:17
【问题描述】:

我正在研究 NFC(Mifare 经典标签)。我想知道如何将数据写入 mi fare classic 标签。我有空标签。请告诉我需要用于 mifare 经典标签的 APDU 命令。

我发送 APDU 命令如下:

\x00','\xA4', '\x04', '\x00','\x07','\xd2','\x76','\x00','\x00','\x85','\x01','\x00

如果我使用这个 apdu 命令,它不会给出任何成功响应(因为每个 APDU 命令都会有 9000),它会进入 else 部分。请告诉我正确的 apdu 命令。我正在用 C 语言编写代码。我的阅读器是 MFC 523。需要遵循哪些步骤?

// code i am using its for the Mobile POS 

   int ret;
   sendtoUSB("going to the auth",17); // for display purpose 
   // for authentication we will use the basic  authentication between the reader and writer 
      Sys_ClearScreen();
      char cRecvBuf[128];
      int  i,j;      
      char cSendBuf[10]= {0xFF,0x86,0x00,0x00,0x05,0x01,0x00,0x05,0x60,0x01};     // authenticaton key 
      char msg[128];
      u32  rlen; // response length 
      Sys_PcdOpen();
      while(1)
      {
          sendtoUSB("in while",8);
          if(Sys_Kbhit()==KEY_CANCEL)
             break;

          ret = Sys_PiccPresent();

          // activation of the NFC
          ret = Sys_PiccActivate();


          uchar key[16]= {0xFF,0x82,0x20,0x01,0x06,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF} ; //Load keys command 

          char cSendBuf[10]= {0xFF,0x86,0x00,0x00,0x05,0x01,0x00,0x05,0x60,0x01};   // authentication command 

          ret = Sys_PiccCommand(key,11,&rlen,cRecvBuf); // sending the command 
          sendtoUSB("In send cmd",11);

          sendtoUSB(key,strlen(key)); // here i am getting the key as i am sending 


          sendtoUSB(cRecvBuf,rlen);  // in recvbuf i am agetting as 6E00



          if(ret==0) // checking the returing value 
          {
              // its coming into the if also 
              sendtoUSB("sucess",6);
              if(rlen>=0)
              {
                 if((cRecvBuf[rlen-2]==0x90) && (cRecvBuf[rlen-1]==0x00)) // checking the 90 00 response 

                 {
                      // not coming here
                      sendtoUSB("in if",5); 
                    strcpy(msg, "ankita"); 
                    for(i=0;i<rlen-2;i++)
                      sprintf(msg+strlen(msg), "%02X", cRecvBuf[i]);

                    Sys_DisplayLine( 2, msg, 0, ALIGN_LEFT) ;

                 }
                 else
                 {
                     // its coming here 

                    sendtoUSB("In else",7);



                    sprintf(msg, "In else 02X%02X", cRecvBuf[rlen-2],cRecvBuf[rlen-1]);


                    Sys_DisplayLine( 2, msg, 1, ALIGN_CENTER) ;
                    sendtoUSB ("fail data",9);
                    sendtoUSB(cRecvBuf,rlen);
                    sendtoUSB(msg,strlen(msg));

                }
            }

        }
        Sys_PiccDeselect(RM_WAIT);
        break;
    }
    Sys_PcdClose();
    Sys_WaitReturn();

    Sys_Buzzer(KB_BEEP_DURATION);



}


// i am following the same steps for the authentication also


//open NFC
// check NFC is there or not 
//activate 
// send command 

【问题讨论】:

  • 这个命令返回什么状态字。
  • @vikky 谢谢你这么多 vikky 你给我回复...
  • @vikky 我收到的响应是 6E00 类错误,我收到了
  • @vikky 请告诉我我在做什么错误。我也给了身份验证密钥
  • 从哪里得到这个命令。

标签: c nfc mifare apdu


【解决方案1】:

我认为,“加载键命令”有问题。在这里,我发布了加载键命令结构。

【讨论】:

  • 在身份验证之后我遇到了问题
  • 那么问题出在哪里?
  • 认证 APDu 命令进入失败状态。并且在加载关键 APDU 命令时也给出了相同的响应。 (6E00)。我已经在聊天中提到了
  • 我现在也没有得到可能是什么问题。那就是我在问你。我有 ISO 14443 标准...因此,APDU 也将不同或相同???我也搜索..但我得到相同的响应。是 APDU 有问题还是其他问题...
  • 你有没有用其他“MFC 523”检查这个代码。可能是阅读器有问题。你有阅读器的规格吗。
【解决方案2】:

您是否以这种格式发送身份验证 ADPU 命令?如果没有,请尝试以下操作。它在 MIFARE Classic 标签身份验证中为我工作。

GENERAL AUTHENTICATE command APDU

    CLA     INS     P1      P2      Lc      DATAin          Le
    0xFF    h86     00      00      05      seebelow        -
    (def.)  (for 
            authent-
            ication 
            cmmd)

            GENERAL AUTHENTICATE Data In bytes

    Byte 0          Byte 1          Byte2           Byte3           Byte4
    h01             h00             Block            key            key     
                                    number          location        index
                                                    or Key 
                                                    type 

块号(BYTE 2)是卡上的地址,我们尝试在其中进行身份验证(块号)

密钥位置或密钥类型 (BYTE 3) 必须是:

  • h60 用于使用 CRYPTO1“A”密钥(标准 PC/SC 定义的值)进行身份验证,
  • h61 用于使用 CRYPTO1“B”密钥(标准 PC/SC 定义的值)进行身份验证,

与 LOAD KEY 指令中使用的 P1 参数值相同:h00 或 h20(SpringCard 具体值)。

键索引(字节4)定义如下:

  • 如果密钥类型(字节 3)是 h60,则使用值 h00 到 h03 选择存储在阅读器中的“A”密钥之一 易失性内存,值 h20 到 h2F 以选择存储在阅读器非易失性内存(如果可用)中的“A”键之一。

  • 如果密钥类型(字节 3)是 h61,则使用值 h00 到 h03 选择存储在阅读器中的“B”密钥之一 易失性内存,值 h20 到 h2F 以选择存储在阅读器非易失性内存(如果可用)中的“B”键之一。


int PCSC_Mifare_Read (unsigned char *data, unsigned int read_size, unsigned char block_num)
{
  unsigned char TxBuffer[5]= {0xFF, 0xB0};
  unsigned char RxBuffer[42]= {0};
  int Response = -1;
  int SendLength = 0;
  unsigned long RecvLength = 0;

  if (data == NULL)
  {
        Debug(__func__,"Invalid Param");
    return Response;
  }

  if (0xFF > block_num)
  {
    TxBuffer[2] = 0x00;
    TxBuffer[3] = block_num;
  }
  else
  {
    /* If the block number is greater than 0xFF then store the block number's MSB in P1 field
       and LSB in P2 field of APDU*/
    TxBuffer[2] = (block_num % 0xFF);
    TxBuffer[3] = 0xFF;
  }

  TxBuffer[4] = read_size; //or 0x00 or 0x20 if card supports 32 BYTE data read

  SendLength = sizeof (TxBuffer);
  RecvLength = sizeof (RxBuffer);

  Response = SCardTransmit(hCard, pioSendPci, TxBuffer, SendLength,
                           &pioRecvPci, RxBuffer, &RecvLength);
  if (Response != SCARD_S_SUCCESS)
  {
    Debug (__func__,"SCardTransmit Failed: %s 0x%x",pcsc_stringify_error(Response), Response);
    return Response;
  }

  if ((RxBuffer[RecvLength-2] == 0x90) && (RxBuffer[RecvLength-1] == 0x00))
  {
    Debug (__func__,"Succeeded : %02x %02x",RxBuffer[RecvLength-2], RxBuffer[RecvLength-1]);

    if (RecvLength > 0x02)
    {
        memcpy (data, RxBuffer, (RecvLength-2));
                Debug(__func__,"Read passed");
    }
    else
    {
                Debug(__func__,"Read Failed");
                return -1;
    }
  }
  else
  {
     Debug(__func__,"Failed : %02x %02x",RxBuffer[RecvLength-2], RxBuffer[RecvLength-1]);
     return -1;
  }

  return Response;
}


int PCSC_Mifare_Write (unsigned char block_num, unsigned char *data,unsigned int size)
{
  unsigned char TxBuffer[21] = {0xFF, 0xD6};
  unsigned char RxBuffer[2] = {0};
  int Response = -1;
  int SendLength = 0;
  unsigned long RecvLength = 0;

  if (0xFF > block_num)
  {
    TxBuffer[2] = 0x00;
    TxBuffer[3] = block_num;
  }
  else
  {
    /* If the block number is greater than 0xFF then store the block number's MSB in P1 field
       and LSB in P2 field of APDU*/
    TxBuffer[2] = (block_num % 0xFF);
    TxBuffer[3] = 0xFF;
  }

  TxBuffer[4] = size; //This is the block size to be written

  //copy the 0x08 BYTE data to be writen to the card
  memcpy ((TxBuffer + 5), data, size);

  SendLength = 5+size;//sizeof (TxBuffer);
  RecvLength = sizeof (RxBuffer);

//  HexDump("PCSC_Mifare_Write", TxBuffer, SendLength);

  Response = SCardTransmit(hCard, pioSendPci, TxBuffer, SendLength,
                           &pioRecvPci, RxBuffer, &RecvLength);
  if (Response != SCARD_S_SUCCESS)
  {
    Debug (__func__,"SCardTransmit Failed: %s 0x%x",pcsc_stringify_error(Response), Response);
    return Response;
  }

  if ((RxBuffer[0] == 0x90) && (RxBuffer[1] == 0x00))
  {
    Debug (__func__,"Succeeded : %02x %02x",RxBuffer[0],RxBuffer[1]);
  }
  else
  {
    Debug (__func__,"Failed : %02x %02x",RxBuffer[0],RxBuffer[1]);
    return -1;
  }

  return Response;
}

【讨论】:

  • 我尝试使用相同的命令。我收到了 6E00 的回复..为什么我阅读了很多文档..但是我得到了同样的回复
  • 你想要对你的应用程序做的是你想要读取卡的扇区并根据你使用的访问条件写入一些数据 ....
  • 我也想从卡上读取。并将数据也写入卡
  • kk,这一切你可以用一个名为“MIFARE CLASSIC TOOL”的安卓应用程序来做,如果你在你的手机上激活了 NFC,然后把 MIFARE 卡放回你,你是否有任何支持 NFC 的手机手机,然后您的手机中的 NFC 读卡器将检测到该卡,并且在您的应用程序中,您可以读取、写入等任何您想要的东西。
  • 即使你想让应用程序写入或读取 MIFARE 智能卡的扇区然后使用 PCSC 命令和 ADPU 命令,我已经通过使用这些命令创建了相同的应用程序它正在读取和写入两者c 中的一个程序。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多