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

时间:2016-05-31 02:32:01

标签: c# smartcard reader pcsc

大家好日子,我想询问如何用智能卡写字。我只是依赖于文档中给出的示例,但它只有读取标记。 我按照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数据。如果有人能为我提供如何在智能卡上书写的代码片段,我将很高兴。非常感谢你!

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

1 个答案:

答案 0 :(得分:1)

在开始之前,你应该首先阅读Mifare卡,可以获得文件Here.

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

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

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

同时检查this并搜索其他Mifare主题以了解Mifare卡。通过所有这些链接,您将了解需要发送哪些命令来编写/读取Mifare卡,一旦您了解要触发的APDU /命令,您可以在代码中构建相同的代码,就像您已经阅读过mifare代码一样。只需在代码中替换write命令,如果一切正常,您可以按照要求编写。

希望有所帮助......