【问题标题】:C# - Write in Smartcard using PCSC-SharpC# - 使用 PCSC-Sharp 写入智能卡
【发布时间】:2016-09-28 21:21:57
【问题描述】:

大家好,我想问一下如何用智能卡写。我只是依赖文档中给出的示例,但它只有读取标签。 我按照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];
        }
    }
}

我阅读了文档,但我无法完全理解如何使用 CommandAdpu 写入数据。如果有人可以为我提供有关如何在智能卡中写入的代码 sn-p,我将不胜感激。非常感谢!

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

【问题讨论】:

  • 你到底在找什么???
  • 您好,先生。我正在寻找有关如何使用我的智能卡读卡器使用 PCSC-sharp 在智能卡上写入数据的代码 sn-p。非常感谢。
  • 您的密码读卡成功了吗???您是否在最后成功构建/运行了 pcsc-sharp 项目。
  • 您好,先生,是的,我可以使用 pcsc-sharp 读取智能卡的 ID。我试图关注danm.de/docs/pcsc-sharp,但我迷路了。
  • 您正在尝试读/写的卡。您到目前为止发送了什么命令并成功了。别说先生,你可以叫我的名字。

标签: c# smartcard reader pcsc


【解决方案1】:

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

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

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

如果您有单片机阅读器,那么This 文档将帮助您了解需要在 Mifare 经典卡上发送的命令。

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

希望对你有帮助..

【讨论】:

  • 投了反对票,因为更重要的链接被破坏了,你的回答基本上等同于“谷歌搜索”和“阅读它”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多