如何在Mifae卡中编写UTF-8

时间:2017-12-21 06:40:32

标签: c# utf-8 rfid mifare

我尝试使用MifareCard& amp; NFC阅读器。

在这段代码中,英语通常是,但其他语言都被破坏了。

喜欢这个 将数据写入卡“北京”,读写数据“yyyy” 将数据写入卡“서울”,读写数据“yyyy” 写数据到卡“さくら”,读写数据“yyyyyy”

如何使用UTF8编码将数据写入MifareCard?

Main.CS

#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

1 个答案:

答案 0 :(得分:1)

您可以将string从一种编码转换为另一种编码,就像这样

byte[] bytes = Encoding.UTF8.GetBytes(myString);
myString = Encoding.UTF8.GetString(bytes); 

查看文档here.