C# - 使用 PCSC-Sharp 在智能卡中写入



大家好,我想问一下如何在智能卡中写作。我只是依靠文档中给出的示例,但它只有读取标签。我遵循这里的例子 https://github.com/danm-de/pcsc-sharp/blob/master/Examples/Transmit/Program.cs

using System;
using PCSC;
using PCSC.Iso7816;
namespace Transmit
{
    public class Program
    {
        public static void Main() {
            using (var context = new SCardContext()) {
                context.Establish(SCardScope.System);
                var readerNames = context.GetReaders();
                if (readerNames == null || readerNames.Length < 1) {
                    Console.WriteLine("You need at least one reader in order to run this example.");
                    Console.ReadKey();
                    return;
                }
                var readerName = ChooseRfidReader(readerNames);
                if (readerName == null) {
                    return;
                }
                using (var rfidReader = new SCardReader(context)) {
                    var sc = rfidReader.Connect(readerName, SCardShareMode.Shared, SCardProtocol.Any);
                    if (sc != SCardError.Success) {
                        Console.WriteLine("Could not connect to reader {0}:n{1}",
                            readerName,
                            SCardHelper.StringifyError(sc));
                        Console.ReadKey();
                        return;
                    }
                    var apdu = new CommandApdu(IsoCase.Case2Short, rfidReader.ActiveProtocol) {
                        CLA = 0xFF,
                        Instruction = InstructionCode.GetData,
                        P1 = 0x00,
                        P2 = 0x00,
                        Le = 0  // We don't know the ID tag size
                    };
                    sc = rfidReader.BeginTransaction();
                    if (sc != SCardError.Success) {
                        Console.WriteLine("Could not begin transaction.");
                        Console.ReadKey();
                        return;
                    }
                    Console.WriteLine("Retrieving the UID .... ");
                    var receivePci = new SCardPCI(); // IO returned protocol control information.
                    var sendPci = SCardPCI.GetPci(rfidReader.ActiveProtocol);
                    var receiveBuffer = new byte[256];
                    var command = apdu.ToArray();
                    sc = rfidReader.Transmit(
                        sendPci,            // Protocol Control Information (T0, T1 or Raw)
                        command,            // command APDU
                        receivePci,         // returning Protocol Control Information
                        ref receiveBuffer); // data buffer
                    if (sc != SCardError.Success) {
                        Console.WriteLine("Error: " + SCardHelper.StringifyError(sc));
                    }
                    var responseApdu = new ResponseApdu(receiveBuffer, IsoCase.Case2Short, rfidReader.ActiveProtocol);
                    Console.Write("SW1: {0:X2}, SW2: {1:X2}nUid: {2}", 
                        responseApdu.SW1, 
                        responseApdu.SW2, 
                        responseApdu.HasData ? BitConverter.ToString(responseApdu.GetData()) : "No uid received");
                    rfidReader.EndTransaction(SCardReaderDisposition.Leave);
                    rfidReader.Disconnect(SCardReaderDisposition.Reset);
                    Console.ReadKey();
                }
            }
        }
        private static string ChooseRfidReader(string[] readerNames) {
            // Show available readers.
            Console.WriteLine("Available readers: ");
            for (var i = 0; i < readerNames.Length; i++) {
                Console.WriteLine("[" + i + "] " + readerNames[i]);
            }
            // Ask the user which one to choose.
            Console.Write("Which reader is an RFID reader? ");
            var line = Console.ReadLine();
            int choice;
            if (!(int.TryParse(line, out choice)) || (choice < 0) || (choice > readerNames.Length)) {
                Console.WriteLine("An invalid number has been entered.");
                Console.ReadKey();
                return null;
            }
            return readerNames[choice];
        }
    }
}

我阅读了文档,但我无法完全了解如何命令Adpu写入数据。如果有人能为我提供有关如何在智能卡中编写的代码片段,我将不胜感激。谢谢!

https://danm.de/docs/pcsc-sharp/

在开始任何事情之前,您应该先阅读有关Mifare卡的信息,可以在此处获取文档。

然后尝试通过任何APDU工具与卡通信。

如果您没有任何此类工具,则可以使用 pyApduTool 向卡发送命令。

如果您有SCM阅读器,则本文档将帮助您了解需要在Mifare经典卡上发送的命令。

另请查看此内容并搜索其他 Mifare 主题以了解 Mifare 卡。通过所有这些链接,您将了解需要发送哪些命令来写入/读取Mifare卡,一旦您了解了要触发的APDU/命令,您就可以在代码中构建相同的命令,就像您说已经用代码读取mifare一样。只需替换代码中的 write 命令,如果一切正常,您可以根据需要编写。

希望对您有所帮助。

相关内容

  • 没有找到相关文章

最新更新