2

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/

Paulo Basilio
  • 23
  • 1
  • 1
  • 4
  • what exactly you are looking for??? – Arjun May 31 '16 at 12:41
  • Hi Sir. I was looking for a code snippet on how to write a data on smart card using my smart card reader using PCSC-sharp. Thank you very much. – Paulo Basilio Jun 01 '16 at 01:43
  • Did you got success for reading card with the code you have??? Did you successfully build/run the project pcsc-sharp at your end. – Arjun Jun 01 '16 at 05:16
  • Hello sir, yes i can read the id of the smart card using pcsc-sharp. I tried to follow the https://danm.de/docs/pcsc-sharp/ and I got lost. – Paulo Basilio Jun 01 '16 at 06:48
  • what card you are trying to read/write. what command you sent till now and got succeeded. don't say sir, you can call my name. – Arjun Jun 03 '16 at 06:52
  • Hello Arjun, I am trying to write in Mifare 1k smart card up until now I only succeeded in reading data. – Paulo Basilio Jun 03 '16 at 08:07
  • mifare reading/writing require to pass 3 pass authentication first. Did you passed it? do you know KEY A and Key B, could you share what command you successfully passed till now – Arjun Jun 03 '16 at 09:11
  • Hello Arjun, Sorry I reply too late. I am not familiar with the 3 pass authentication and I am not also familiar with the command. Thank you very much. – Paulo Basilio Jun 07 '16 at 03:59
  • Hi @PauloBasilio, do you still need solution to your question, or you've solved it already? – Mr Heart Jun 07 '16 at 11:41
  • Hi Mr Heart, I still need solution to my question. may I request for your wisdom. thank you very much! – Paulo Basilio Jun 08 '16 at 01:59

1 Answers1

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..

Community
  • 1
  • 1
Arjun
  • 3,491
  • 4
  • 25
  • 47
  • 5
    Downvoted because the more important links are broken and your answer basically equates to "google it", and "read about it". – BrainSlugs83 Jan 18 '17 at 00:55