C# 如何在Mifae卡中写入UTF-8
我尝试使用MifareCard和NFC阅读器制作会员会员管理程序 在这段代码中,英语是正常的,但其他语言是坏的 像这样 “将数据写入卡”北京", 读写数据“yyy” “将数据写入卡”서울,读写数据“yyyy” “将数据写入卡”さくら,读写数据“yyyyy” 如何使用UTF8编码将数据写入MifareCard Main.CSC# 如何在Mifae卡中写入UTF-8,c#,utf-8,rfid,mifare,C#,Utf 8,Rfid,Mifare,我尝试使用MifareCard和NFC阅读器制作会员会员管理程序 在这段代码中,英语是正常的,但其他语言是坏的 像这样 “将数据写入卡”北京", 读写数据“yyy” “将数据写入卡”서울,读写数据“yyyy” “将数据写入卡”さくら,读写数据“yyyyy” 如何使用UTF8编码将数据写入MifareCard Main.CS #region Write_Data private void Button_Click_2(object sender, RoutedEventArgs e) {
#region Write_Data
private void Button_Click_2(object sender, RoutedEventArgs e)
{
if (connectCard())// establish connection to the card: you've declared this from previous post
{
byte[] bytes = Encoding.Default.GetBytes(textBox2.Text);
String insert_Value = Encoding.UTF8.GetString(bytes);
submitText(insert_Value, Combo_W.Text); // 5 - is the block we are writing data on the card
Close();
}
}
//submit data method
public void submitText(String Text, String Block)
{
String tmpStr = Text;
int indx;
if (authenticateBlock(Block))
{
ClearBuffers();
SendBuff[0] = 0xFF; // CLA
SendBuff[1] = 0xD6; // INS
SendBuff[2] = 0x00; // P1
SendBuff[3] = (byte)int.Parse(Block); // P2 : Starting Block No.
SendBuff[4] = (byte)int.Parse("16"); // P3 : Data length
for (indx = 0; indx <= (tmpStr).Length - 1; indx++)
{
SendBuff[indx + 5] = (byte)tmpStr[indx];
}
SendLen = SendBuff[4] + 5;
RecvLen = 0x02;
retCode = SendAPDUandDisplay(2);
if (retCode != Card.SCARD_S_SUCCESS)
{
MessageBox.Show("fail write");
}
else
{
MessageBox.Show("write success");
}
}
else
{
MessageBox.Show("FailAuthentication");
}
}
// block authentication
private bool authenticateBlock(String block)
{
ClearBuffers();
SendBuff[0] = 0xFF; // CLA
SendBuff[2] = 0x00; // P1: same for all source types
SendBuff[1] = 0x86; // INS: for stored key input
SendBuff[3] = 0x00; // P2 : Memory location; P2: for stored key input
SendBuff[4] = 0x05; // P3: for stored key input
SendBuff[5] = 0x01; // Byte 1: version number
SendBuff[6] = 0x00; // Byte 2
SendBuff[7] = (byte)int.Parse(block); // Byte 3: sectore no. for stored key input
SendBuff[8] = 0x60; // Byte 4 : Key A for stored key input
SendBuff[9] = (byte)int.Parse("1"); // Byte 5 : Session key for non-volatile memory
SendLen = 0x0A;
RecvLen = 0x02;
retCode = SendAPDUandDisplay(0);
if (retCode != Card.SCARD_S_SUCCESS)
{
//MessageBox.Show("FAIL Authentication!");
return false;
}
return true;
}
// clear memory buffers
private void ClearBuffers()
{
long indx;
for (indx = 0; indx <= 262; indx++)
{
RecvBuff[indx] = 0;
SendBuff[indx] = 0;
}
}
// send application protocol data unit : communication unit between a smart card reader and a smart card
private int SendAPDUandDisplay(int reqType)
{
int indx;
string tmpStr = "";
pioSendRequest.dwProtocol = Aprotocol;
pioSendRequest.cbPciLength = 8;
//Display Apdu In
for (indx = 0; indx <= SendLen - 1; indx++)
{
tmpStr = tmpStr + " " + string.Format("{0:X2}", SendBuff[indx]);
}
retCode = Card.SCardTransmit(hCard, ref pioSendRequest, ref SendBuff[0],
SendLen, ref pioSendRequest, ref RecvBuff[0], ref RecvLen);
if (retCode != Card.SCARD_S_SUCCESS)
{
return retCode;
}
else
{
try
{
tmpStr = "";
switch (reqType)
{
case 0:
for (indx = (RecvLen - 2); indx <= (RecvLen - 1); indx++)
{
tmpStr = tmpStr + " " + string.Format("{0:X2}", RecvBuff[indx]);
}
if ((tmpStr).Trim() != "90 00")
{
//MessageBox.Show("Return bytes are not acceptable.");
return -202;
}
break;
case 1:
for (indx = (RecvLen - 2); indx <= (RecvLen - 1); indx++)
{
tmpStr = tmpStr + string.Format("{0:X2}", RecvBuff[indx]);
}
if (tmpStr.Trim() != "90 00")
{
tmpStr = tmpStr + " " + string.Format("{0:X2}", RecvBuff[indx]);
}
else
{
tmpStr = "ATR : ";
for (indx = 0; indx <= (RecvLen - 3); indx++)
{
tmpStr = tmpStr + " " + string.Format("{0:X2}", RecvBuff[indx]);
}
}
break;
case 2:
for (indx = 0; indx <= (RecvLen - 1); indx++)
{
tmpStr = tmpStr + " " + string.Format("{0:X2}", RecvBuff[indx]);
}
break;
}
}
catch (IndexOutOfRangeException)
{
return -200;
}
}
return retCode;
}
//disconnect card reader connection
public void Close()
{
if (connActive)
{
retCode = Card.SCardDisconnect(hCard, Card.SCARD_UNPOWER_CARD);
}
//retCode = Card.SCardReleaseContext(hCard);
}
#endregion
#region Read_Data
private void Button_Click_1(object sender, RoutedEventArgs e)
{
string text = verifyCard(Combo_R.Text); // 5 - is the block we are reading
byte[] bytes = Encoding.Default.GetBytes(text);
textBox1.Text = Encoding.UTF8.GetString(bytes);
}
public string verifyCard(String Block)
{
string value = "";
if (connectCard())
{
value = readBlock(Block);
}
value = value.Split(new char[] { '\0' }, 2, StringSplitOptions.None)[0].ToString();
return value;
}
public string readBlock(String Block)
{
string tmpStr = "";
int indx;
if (authenticateBlock(Block))
{
ClearBuffers();
SendBuff[0] = 0xFF; // CLA
SendBuff[1] = 0xB0;// INS
SendBuff[2] = 0x00;// P1
SendBuff[3] = (byte)int.Parse(Block);// P2 : Block No.
SendBuff[4] = (byte)int.Parse("16");// Le
SendLen = 5;
RecvLen = SendBuff[4] + 2;
retCode = SendAPDUandDisplay(2);
if (retCode == -200)
{
return "outofrangeexception";
}
if (retCode == -202)
{
return "BytesNotAcceptable";
}
if (retCode != Card.SCARD_S_SUCCESS)
{
return "FailRead";
}
// Display data in text format
for (indx = 0; indx <= RecvLen - 1; indx++)
{
tmpStr = tmpStr + Convert.ToChar(RecvBuff[indx]);
}
return (tmpStr);
}
else
{
return "FailAuthentication";
}
}
#endregion
#区域写入#u数据
私有无效按钮\u单击\u 2(对象发送方,路由目标)
{
if(connectCard())//建立到卡的连接:您已经在上一篇文章中声明了这一点
{
byte[]bytes=Encoding.Default.GetBytes(textBox2.Text);
String insert_Value=Encoding.UTF8.GetString(字节);
submitText(insert_Value,Combo_W.Text);//5-是我们在卡上写入数据的块
Close();
}
}
//提交数据方法
公共void submitText(字符串文本、字符串块)
{
字符串tmpStr=文本;
int indx;
if(authenticateBlock(块))
{
ClearBuffers();
SendBuff[0]=0xFF;//CLA
SendBuff[1]=0xD6;//INS
SendBuff[2]=0x00;//P1
SendBuff[3]=(字节)int.Parse(块);//P2:起始块编号。
SendBuff[4]=(byte)int.Parse(“16”);//P3:数据长度
对于(indx=0;indx您可以像这样将字符串从一种编码转换为另一种编码
byte[] bytes = Encoding.UTF8.GetBytes(myString);
myString = Encoding.UTF8.GetString(bytes);
参见文件I在数据写入字节[]tmpStr=Encoding.Default.GetBytes(Text);for(indx=0;indx使用到字节[]bytes=Encoding.UTF8.GetBytes(myString);myString=Encoding.UTF8.GetString(bytes);已解决