在智能卡中读写数据 [英] read and write data in smartcard

查看:72
本文介绍了在智能卡中读写数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的先生/女士





i使用acr38的sdk并使用类ModWinsCard.cs来写入和读取数据但是当我写完后读数据只显示'n'



从下面的评论中复制的其他信息

Dear Sir/Madam


i am using sdk of acr38 and using the class ModWinsCard.cs to write and read data but when i read data after writing it show only 'n'

additional information copied from comment below

private void btnWrite_Click(object sender, System.EventArgs e)
{
   // Validate input template
   if (txtData.Text == "") 
   {
      txtData.Focus();
   }

   //Check User File selected by user
   if (RBAA11.Checked == true) 
   {
      HiAddr = 0xAA;
      LoAddr = 0x11;
      dataLen = 0x0A;
      //ChkStr = "91 00";
   }

   if (RBBB22.Checked == true)
   {
      HiAddr = 0xBB;
      LoAddr = 0x22;
      dataLen = 0x10;
      //ChkStr = "91 01";
   }

   if (RBCC33.Checked == true) 
   {	
      HiAddr = 0xCC;
      LoAddr = 0x33;
      dataLen = 0x20;
      //ChkStr = "91 02";
   }

   // Select User File
   SelectFile(HiAddr, LoAddr);
   if (retcode != ModWinsCard.SCARD_S_SUCCESS) 
   {
      return;
   }
			
   tmpStr = ""; 
   tmpStr = txtData.Text;
			
   //Clear the data first
   for (indx = 0; indx < tmpArray.Length; indx++)
      tmpArray[indx] = 0x00;

   writeRecord(0x01, 0x00, (byte)tmpArray.Length, (byte)tmpArray.Length, ref tmpArray);			
   //Now write the data to the card
   for(indx=0; indx < tmpStr.Length; indx++)
      tmpArray[indx] = (byte)Asc(tmpStr.Substring(indx,1));
			 		
   writeRecord(0x01, 0x00, dataLen, dataLen, ref tmpArray);			

   if (retcode != ModWinsCard.SCARD_S_SUCCESS) 
   {
      return;
   }

   lstOutput.Items.Add("Data read from Text Box is written to card.");
   lstOutput.SelectedIndex = lstOutput.Items.Count - 1;  
   }

   int Asc(string character)
   {
      if (character.Length == 1)
      {
         System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
         int intAsciiCode = (int)asciiEncoding.GetBytes(character)[0];
         return (intAsciiCode);
      }
      else
      {
         throw new Exception("Character is not valid.");
      }
   }

private void  writeRecord(int caseType, byte RecNo, byte maxLen, byte DataLen, ref byte[] ApduIn)
{
   if (caseType == 1)    // If card data is to be erased before writing new data. Re-initialize card values to $00
   {
      apdu.bCLA = 0x80;        // CLA
      apdu.bINS = 0xD2;        // INS
      apdu.bP1 = RecNo;		 // Record No
      apdu.bP2 = 0x00;         // P2
      apdu.bP3 = maxLen;        // Length of Data

      apdu.IsSend = true;

      for(i=0; i< maxLen; i++) 
         apdu.Data[i] = ApduIn[i];
           
      PerformTransmitAPDU(ref apdu);
   }

   //Write data to card
   apdu.bCLA = 0x80;       // CLA
   apdu.bINS = 0xD2;       // INS
   apdu.bP1 = RecNo;       // Record No
   apdu.bP2 = 0x00;        // P2
   apdu.bP3 = DataLen;     // Length of Data

   apdu.IsSend = true;

   for(i=0; i< maxLen; i++) 
      apdu.Data[i] = ApduIn[i];
        
   lstOutput.Items.Add("Write to FF 02");
   PerformTransmitAPDU(ref apdu);
}

private void RBAA11_Click(object sender, System.EventArgs e)
{
   txtData.Text = "";
   txtData.MaxLength = 10;
}

private void RBBB22_Click(object sender, System.EventArgs e)
{
   txtData.Text = "";
   txtData.MaxLength = 16;
}

private void RBCC33_Click(object sender, System.EventArgs e)
{
   txtData.Text = "";
   txtData.MaxLength = 32;
}

private void btnRead_Click(object sender, System.EventArgs e)
{
   int indx; 
   string ChkStr;

   // Check User File selected by user
   if (RBAA11.Checked == true) 
   {
      HiAddr = 0xAA;
      LoAddr = 0x11;
      dataLen = 0x0A;
      //ChkStr = "91 00";
   }

   if (RBBB22.Checked == true) 
   {
      HiAddr = 0xBB;
      LoAddr = 0x22;
      dataLen = 0x10;
      //ChkStr = "91 01";
   }

   if (RBCC33.Checked == true)
   {
      HiAddr = 0xCC;
      LoAddr = 0x33;
      dataLen = 0x20;
      //ChkStr = "91 02";
   }

   // Select User File
   SelectFile(HiAddr, LoAddr);

   if (retcode != ModWinsCard.SCARD_S_SUCCESS)
   {
      return;
   }
			
   // Read First Record of User File selected
   readRecord(0x00, dataLen);

   if (retcode != ModWinsCard.SCARD_S_SUCCESS)
   {
      return;
   }
			
   // Display data read from card to textbox
   tmpStr = "";
   indx = 0;

   while (RecvBuff[indx] != 0x00)
   {
      if (indx < txtData.M

推荐答案

00
{
apdu.bCLA = 0x80; // CLA
apdu.bINS = 0xD2; // INS
apdu.bP1 = RecNo; // 记录号
apdu.bP2 = 0x00; // P2
apdu.bP3 = maxLen; // 数据长度

apdu.IsSend = true ;

for (i = 0 ;我< maxLen; i ++)
apdu.Data [i] = ApduIn [i];

PerformTransmitAPDU( ref apdu);
}

// 将数据写入卡
apdu.bCLA = 0x80; // CLA
apdu.bINS = 0xD2; // INS
apdu.bP1 = RecNo; // 记录号
apdu.bP2 = 0x00; // P2
apdu.bP3 = DataLen; // 数据长度

apdu.IsSend = ;

for (i = 0 ; i< maxLen; i ++)
apdu.Data [i] = ApduIn [i];

lstOutput.Items.Add( 写入FF 02);
PerformTransmitAPDU( ref apdu);
}

private void RBAA11_Click( object sender,System.EventArgs e)
{
txtData.Text = ;
txtData.MaxLength = 10 ;
}

private void RBBB22_Click( object sender,System.EventArgs e)
{
txtData.Text = ;
txtData.MaxLength = 16 ;
}

private void RBCC33_Click( object sender,System.EventArgs e)
{
txtData.Text = ;
txtData.MaxLength = 32 ;
}

private void btnRead_Click( object sender,System.EventArgs e)
{
int indx;
string ChkStr;

// 检查用户选择的用户文件
if (RBAA11.Checked == true
{
HiAddr = 0xAA;
LoAddr = 0x11;
dataLen = 0x0A;
// ChkStr =91 00;
}

if (RBBB22.Checked == true
{
HiAddr = 0xBB;
LoAddr = 0x22;
dataLen = 0x10;
// ChkStr =91 01;
}

if (RBCC33.Checked == true
{
HiAddr = 0xCC;
LoAddr = 0x33;
dataLen = 0x20;
// ChkStr =91 02;
}

// 选择用户文件
SelectFile(HiAddr,LoAddr);

if (retcode!= ModWinsCard.SCARD_S_SUCCESS)
{
返回;
}

// 读取所选用户文件的第一条记录
readRecord(0x00,dataLen);

if (retcode!= ModWinsCard.SCARD_S_SUCCESS)
{
返回;
}

// 显示从卡到文本框读取的数据
tmpStr = ;
indx = 0 ;

while (RecvBuff [indx]!= 0x00)
{
if (indx < txtData.M
00 { apdu.bCLA = 0x80; // CLA apdu.bINS = 0xD2; // INS apdu.bP1 = RecNo; // Record No apdu.bP2 = 0x00; // P2 apdu.bP3 = maxLen; // Length of Data apdu.IsSend = true; for(i=0; i< maxLen; i++) apdu.Data[i] = ApduIn[i]; PerformTransmitAPDU(ref apdu); } //Write data to card apdu.bCLA = 0x80; // CLA apdu.bINS = 0xD2; // INS apdu.bP1 = RecNo; // Record No apdu.bP2 = 0x00; // P2 apdu.bP3 = DataLen; // Length of Data apdu.IsSend = true; for(i=0; i< maxLen; i++) apdu.Data[i] = ApduIn[i]; lstOutput.Items.Add("Write to FF 02"); PerformTransmitAPDU(ref apdu); } private void RBAA11_Click(object sender, System.EventArgs e) { txtData.Text = ""; txtData.MaxLength = 10; } private void RBBB22_Click(object sender, System.EventArgs e) { txtData.Text = ""; txtData.MaxLength = 16; } private void RBCC33_Click(object sender, System.EventArgs e) { txtData.Text = ""; txtData.MaxLength = 32; } private void btnRead_Click(object sender, System.EventArgs e) { int indx; string ChkStr; // Check User File selected by user if (RBAA11.Checked == true) { HiAddr = 0xAA; LoAddr = 0x11; dataLen = 0x0A; //ChkStr = "91 00"; } if (RBBB22.Checked == true) { HiAddr = 0xBB; LoAddr = 0x22; dataLen = 0x10; //ChkStr = "91 01"; } if (RBCC33.Checked == true) { HiAddr = 0xCC; LoAddr = 0x33; dataLen = 0x20; //ChkStr = "91 02"; } // Select User File SelectFile(HiAddr, LoAddr); if (retcode != ModWinsCard.SCARD_S_SUCCESS) { return; } // Read First Record of User File selected readRecord(0x00, dataLen); if (retcode != ModWinsCard.SCARD_S_SUCCESS) { return; } // Display data read from card to textbox tmpStr = ""; indx = 0; while (RecvBuff[indx] != 0x00) { if (indx < txtData.M


这篇关于在智能卡中读写数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆