问题
Good day everyone, I would like to ask on how to write in Smartcard. I just rely on the example given on the documentation but it only has read tag. I follow the examples here in 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];
}
}
}
I read the documentation but I cannot fully understand on how to CommandAdpu of writing data. I will gladly appreciate if someone can provide me a code snippet on how to write in smart card. Thank you very much!
https://danm.de/docs/pcsc-sharp/
回答1:
Before starting anything, You should read about Mifare card first, can get the document Here.
And then try to communicate with the card by any APDU tool.
You can use pyApduTool to send commands to the cards if you don't have any such tool.
If you have SCM reader then This document will help you to understand about commands need to send on Mifare classic card.
Also check this and search other Mifare topic to learn about Mifare cards. With all this links you will get to know what commands need to send to write/read Mifare cards and once you will know about APDU/Commands to fire, you can build the same in your code as you said you already read mifare with your code. Just replace write command in your code and if everything fine you can write as you looking for.
Hope it helps..
来源:https://stackoverflow.com/questions/37535943/c-sharp-write-in-smartcard-using-pcsc-sharp